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