b4d29c577c97550663786d7ac85ef3b103f472d7
[reactos.git] / reactos / sdk / tools / isohybrid / reactos_support_code.c
1 #include "reactos_support_code.h"
2
3 void
4 isohybrid_error(int eval, const char* fmt, ...)
5 {
6 fprintf(stderr, "isohybrid: ");
7
8 va_list ap;
9 va_start(ap, fmt);
10 vfprintf(stderr, fmt, ap);
11 va_end(ap);
12 exit(eval);
13 }
14
15 void
16 isohybrid_warning(const char *fmt, ...)
17 {
18 fprintf(stderr, "isohybrid: ");
19
20 va_list ap;
21 va_start(ap, fmt);
22 vfprintf(stderr, fmt, ap);
23 va_end(ap);
24 }
25
26 /////////////////////////////////////////////////////////////////////////////
27 // getopt code from mingw-w64
28 /////////////////////////////////////////////////////////////////////////////
29 /* $OpenBSD: getopt_long.c,v 1.23 2007/10/31 12:34:57 chl Exp $ */
30 /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
31
32 /*
33 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies.
38 *
39 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
40 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
42 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
44 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
45 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46 *
47 * Sponsored in part by the Defense Advanced Research Projects
48 * Agency (DARPA) and Air Force Research Laboratory, Air Force
49 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
50 */
51 /*-
52 * Copyright (c) 2000 The NetBSD Foundation, Inc.
53 * All rights reserved.
54 *
55 * This code is derived from software contributed to The NetBSD Foundation
56 * by Dieter Baron and Thomas Klausner.
57 *
58 * Redistribution and use in source and binary forms, with or without
59 * modification, are permitted provided that the following conditions
60 * are met:
61 * 1. Redistributions of source code must retain the above copyright
62 * notice, this list of conditions and the following disclaimer.
63 * 2. Redistributions in binary form must reproduce the above copyright
64 * notice, this list of conditions and the following disclaimer in the
65 * documentation and/or other materials provided with the distribution.
66 *
67 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
68 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
69 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
70 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
71 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
72 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
73 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
74 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
75 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
76 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
77 * POSSIBILITY OF SUCH DAMAGE.
78 */
79 int opterr = 1; /* if error message should be printed */
80 int optind = 1; /* index into parent argv vector */
81
82 ///// REACTOS ONLY: optopt set to 0 by default, because isohybrid needs this
83 int optopt = 0; /* character checked for validity */
84
85 int optreset; /* reset getopt */
86 char *optarg; /* argument associated with option */
87
88 #define PRINT_ERROR ((opterr) && (*options != ':'))
89
90 #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
91 #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
92 #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
93
94 /* return values */
95 #define BADCH (int)'?'
96 #define BADARG ((*options == ':') ? (int)':' : (int)'?')
97 #define INORDER (int)1
98
99 #define EMSG ""
100
101 static char *place = EMSG; /* option letter processing */
102
103 /* XXX: set optreset to 1 rather than these two */
104 static int nonopt_start = -1; /* first non option argument (for permute) */
105 static int nonopt_end = -1; /* first option after non options (for permute) */
106
107 /* Error messages */
108 static const char recargchar[] = "option requires an argument -- %c";
109 static const char recargstring[] = "option requires an argument -- %s";
110 static const char ambig[] = "ambiguous option -- %.*s";
111 static const char noarg[] = "option doesn't take an argument -- %.*s";
112 static const char illoptchar[] = "unknown option -- %c";
113 static const char illoptstring[] = "unknown option -- %s";
114
115 /*
116 * Compute the greatest common divisor of a and b.
117 */
118 static int
119 gcd(int a, int b)
120 {
121 int c;
122
123 c = a % b;
124 while (c != 0) {
125 a = b;
126 b = c;
127 c = a % b;
128 }
129
130 return (b);
131 }
132
133 /*
134 * Exchange the block from nonopt_start to nonopt_end with the block
135 * from nonopt_end to opt_end (keeping the same order of arguments
136 * in each block).
137 */
138 static void
139 permute_args(int panonopt_start, int panonopt_end, int opt_end,
140 char * const *nargv)
141 {
142 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
143 char *swap;
144
145 /*
146 * compute lengths of blocks and number and size of cycles
147 */
148 nnonopts = panonopt_end - panonopt_start;
149 nopts = opt_end - panonopt_end;
150 ncycle = gcd(nnonopts, nopts);
151 cyclelen = (opt_end - panonopt_start) / ncycle;
152
153 for (i = 0; i < ncycle; i++) {
154 cstart = panonopt_end + i;
155 pos = cstart;
156 for (j = 0; j < cyclelen; j++) {
157 if (pos >= panonopt_end)
158 pos -= nnonopts;
159 else
160 pos += nopts;
161 swap = nargv[pos];
162 /* LINTED const cast */
163 ((char **)nargv)[pos] = nargv[cstart];
164 /* LINTED const cast */
165 ((char **)nargv)[cstart] = swap;
166 }
167 }
168 }
169
170 /*
171 * parse_long_options --
172 * Parse long options in argc/argv argument vector.
173 * Returns -1 if short_too is set and the option does not match long_options.
174 */
175 static int
176 parse_long_options(char * const *nargv, const char *options,
177 const struct option *long_options, int *idx, int short_too)
178 {
179 char *current_argv, *has_equal;
180 size_t current_argv_len;
181 int i, ambiguous, match;
182
183 #define IDENTICAL_INTERPRETATION(_x, _y) \
184 (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \
185 long_options[(_x)].flag == long_options[(_y)].flag && \
186 long_options[(_x)].val == long_options[(_y)].val)
187
188 current_argv = place;
189 match = -1;
190 ambiguous = 0;
191
192 optind++;
193
194 if ((has_equal = strchr(current_argv, '=')) != NULL) {
195 /* argument found (--option=arg) */
196 current_argv_len = has_equal - current_argv;
197 has_equal++;
198 }
199 else
200 current_argv_len = strlen(current_argv);
201
202 for (i = 0; long_options[i].name; i++) {
203 /* find matching long option */
204 if (strncmp(current_argv, long_options[i].name,
205 current_argv_len))
206 continue;
207
208 if (strlen(long_options[i].name) == current_argv_len) {
209 /* exact match */
210 match = i;
211 ambiguous = 0;
212 break;
213 }
214 /*
215 * If this is a known short option, don't allow
216 * a partial match of a single character.
217 */
218 if (short_too && current_argv_len == 1)
219 continue;
220
221 if (match == -1) /* partial match */
222 match = i;
223 else if (!IDENTICAL_INTERPRETATION(i, match))
224 ambiguous = 1;
225 }
226 if (ambiguous) {
227 /* ambiguous abbreviation */
228 if (PRINT_ERROR)
229 warnx(ambig, (int)current_argv_len,
230 current_argv);
231 optopt = 0;
232 return (BADCH);
233 }
234 if (match != -1) { /* option found */
235 if (long_options[match].has_arg == no_argument
236 && has_equal) {
237 if (PRINT_ERROR)
238 warnx(noarg, (int)current_argv_len,
239 current_argv);
240 /*
241 * XXX: GNU sets optopt to val regardless of flag
242 */
243 if (long_options[match].flag == NULL)
244 optopt = long_options[match].val;
245 else
246 optopt = 0;
247 return (BADARG);
248 }
249 if (long_options[match].has_arg == required_argument ||
250 long_options[match].has_arg == optional_argument) {
251 if (has_equal)
252 optarg = has_equal;
253 else if (long_options[match].has_arg ==
254 required_argument) {
255 /*
256 * optional argument doesn't use next nargv
257 */
258 optarg = nargv[optind++];
259 }
260 }
261 if ((long_options[match].has_arg == required_argument)
262 && (optarg == NULL)) {
263 /*
264 * Missing argument; leading ':' indicates no error
265 * should be generated.
266 */
267 if (PRINT_ERROR)
268 warnx(recargstring,
269 current_argv);
270 /*
271 * XXX: GNU sets optopt to val regardless of flag
272 */
273 if (long_options[match].flag == NULL)
274 optopt = long_options[match].val;
275 else
276 optopt = 0;
277 --optind;
278 return (BADARG);
279 }
280 }
281 else { /* unknown option */
282 if (short_too) {
283 --optind;
284 return (-1);
285 }
286 if (PRINT_ERROR)
287 warnx(illoptstring, current_argv);
288 optopt = 0;
289 return (BADCH);
290 }
291 if (idx)
292 *idx = match;
293 if (long_options[match].flag) {
294 *long_options[match].flag = long_options[match].val;
295 return (0);
296 }
297 else
298 return (long_options[match].val);
299 #undef IDENTICAL_INTERPRETATION
300 }
301
302 /*
303 * getopt_internal --
304 * Parse argc/argv argument vector. Called by user level routines.
305 */
306 static int
307 getopt_internal(int nargc, char * const *nargv, const char *options,
308 const struct option *long_options, int *idx, int flags)
309 {
310 char *oli; /* option letter list index */
311 int optchar, short_too;
312 static int posixly_correct = -1;
313
314 if (options == NULL)
315 return (-1);
316
317 /*
318 * XXX Some GNU programs (like cvs) set optind to 0 instead of
319 * XXX using optreset. Work around this braindamage.
320 */
321 if (optind == 0)
322 optind = optreset = 1;
323
324 /*
325 * Disable GNU extensions if POSIXLY_CORRECT is set or options
326 * string begins with a '+'.
327 *
328 * CV, 2009-12-14: Check POSIXLY_CORRECT anew if optind == 0 or
329 * optreset != 0 for GNU compatibility.
330 */
331 if (posixly_correct == -1 || optreset != 0)
332 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
333 if (*options == '-')
334 flags |= FLAG_ALLARGS;
335 else if (posixly_correct || *options == '+')
336 flags &= ~FLAG_PERMUTE;
337 if (*options == '+' || *options == '-')
338 options++;
339
340 optarg = NULL;
341 if (optreset)
342 nonopt_start = nonopt_end = -1;
343 start:
344 if (optreset || !*place) { /* update scanning pointer */
345 optreset = 0;
346 if (optind >= nargc) { /* end of argument vector */
347 place = EMSG;
348 if (nonopt_end != -1) {
349 /* do permutation, if we have to */
350 permute_args(nonopt_start, nonopt_end,
351 optind, nargv);
352 optind -= nonopt_end - nonopt_start;
353 }
354 else if (nonopt_start != -1) {
355 /*
356 * If we skipped non-options, set optind
357 * to the first of them.
358 */
359 optind = nonopt_start;
360 }
361 nonopt_start = nonopt_end = -1;
362 return (-1);
363 }
364 if (*(place = nargv[optind]) != '-' ||
365 (place[1] == '\0' && strchr(options, '-') == NULL)) {
366 place = EMSG; /* found non-option */
367 if (flags & FLAG_ALLARGS) {
368 /*
369 * GNU extension:
370 * return non-option as argument to option 1
371 */
372 optarg = nargv[optind++];
373 return (INORDER);
374 }
375 if (!(flags & FLAG_PERMUTE)) {
376 /*
377 * If no permutation wanted, stop parsing
378 * at first non-option.
379 */
380 return (-1);
381 }
382 /* do permutation */
383 if (nonopt_start == -1)
384 nonopt_start = optind;
385 else if (nonopt_end != -1) {
386 permute_args(nonopt_start, nonopt_end,
387 optind, nargv);
388 nonopt_start = optind -
389 (nonopt_end - nonopt_start);
390 nonopt_end = -1;
391 }
392 optind++;
393 /* process next argument */
394 goto start;
395 }
396 if (nonopt_start != -1 && nonopt_end == -1)
397 nonopt_end = optind;
398
399 /*
400 * If we have "-" do nothing, if "--" we are done.
401 */
402 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
403 optind++;
404 place = EMSG;
405 /*
406 * We found an option (--), so if we skipped
407 * non-options, we have to permute.
408 */
409 if (nonopt_end != -1) {
410 permute_args(nonopt_start, nonopt_end,
411 optind, nargv);
412 optind -= nonopt_end - nonopt_start;
413 }
414 nonopt_start = nonopt_end = -1;
415 return (-1);
416 }
417 }
418
419 /*
420 * Check long options if:
421 * 1) we were passed some
422 * 2) the arg is not just "-"
423 * 3) either the arg starts with -- we are getopt_long_only()
424 */
425 if (long_options != NULL && place != nargv[optind] &&
426 (*place == '-' || (flags & FLAG_LONGONLY))) {
427 short_too = 0;
428 if (*place == '-')
429 place++; /* --foo long option */
430 else if (*place != ':' && strchr(options, *place) != NULL)
431 short_too = 1; /* could be short option too */
432
433 optchar = parse_long_options(nargv, options, long_options,
434 idx, short_too);
435 if (optchar != -1) {
436 place = EMSG;
437 return (optchar);
438 }
439 }
440
441 if ((optchar = (int)*place++) == (int)':' ||
442 (optchar == (int)'-' && *place != '\0') ||
443 (oli = strchr(options, optchar)) == NULL) {
444 /*
445 * If the user specified "-" and '-' isn't listed in
446 * options, return -1 (non-option) as per POSIX.
447 * Otherwise, it is an unknown option character (or ':').
448 */
449 if (optchar == (int)'-' && *place == '\0')
450 return (-1);
451 if (!*place)
452 ++optind;
453 if (PRINT_ERROR)
454 warnx(illoptchar, optchar);
455 optopt = optchar;
456 return (BADCH);
457 }
458 if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
459 /* -W long-option */
460 if (*place) /* no space */
461 /* NOTHING */;
462 else if (++optind >= nargc) { /* no arg */
463 place = EMSG;
464 if (PRINT_ERROR)
465 warnx(recargchar, optchar);
466 optopt = optchar;
467 return (BADARG);
468 }
469 else /* white space */
470 place = nargv[optind];
471 optchar = parse_long_options(nargv, options, long_options,
472 idx, 0);
473 place = EMSG;
474 return (optchar);
475 }
476 if (*++oli != ':') { /* doesn't take argument */
477 if (!*place)
478 ++optind;
479 }
480 else { /* takes (optional) argument */
481 optarg = NULL;
482 if (*place) /* no white space */
483 optarg = place;
484 else if (oli[1] != ':') { /* arg not optional */
485 if (++optind >= nargc) { /* no arg */
486 place = EMSG;
487 if (PRINT_ERROR)
488 warnx(recargchar, optchar);
489 optopt = optchar;
490 return (BADARG);
491 }
492 else
493 optarg = nargv[optind];
494 }
495 place = EMSG;
496 ++optind;
497 }
498 /* dump back option letter */
499 return (optchar);
500 }
501
502 /*
503 * getopt_long_only --
504 * Parse argc/argv argument vector.
505 */
506 int
507 getopt_long_only(int nargc, char * const *nargv, const char *options,
508 const struct option *long_options, int *idx)
509 {
510
511 return (getopt_internal(nargc, nargv, options, long_options, idx,
512 FLAG_PERMUTE | FLAG_LONGONLY));
513 }
514 /////////////////////////////////////////////////////////////////////////////
515
516 #ifdef _WIN32
517 int
518 fsync(int fd)
519 {
520 HANDLE hFile = (HANDLE)_get_osfhandle(fd);
521 if (hFile == INVALID_HANDLE_VALUE)
522 return 1;
523
524 return !FlushFileBuffers(hFile);
525 }
526
527 int
528 getppid(void)
529 {
530 // Just return any nonzero value under Windows to enable isohybrid's usage
531 // as a part of srand initialization.
532 return 1;
533 }
534 #endif