b4d29c577c97550663786d7ac85ef3b103f472d7
1 #include "reactos_support_code.h"
4 isohybrid_error(int eval
, const char* fmt
, ...)
6 fprintf(stderr
, "isohybrid: ");
10 vfprintf(stderr
, fmt
, ap
);
16 isohybrid_warning(const char *fmt
, ...)
18 fprintf(stderr
, "isohybrid: ");
22 vfprintf(stderr
, fmt
, ap
);
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 $ */
33 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
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.
52 * Copyright (c) 2000 The NetBSD Foundation, Inc.
53 * All rights reserved.
55 * This code is derived from software contributed to The NetBSD Foundation
56 * by Dieter Baron and Thomas Klausner.
58 * Redistribution and use in source and binary forms, with or without
59 * modification, are permitted provided that the following conditions
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.
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.
79 int opterr
= 1; /* if error message should be printed */
80 int optind
= 1; /* index into parent argv vector */
82 ///// REACTOS ONLY: optopt set to 0 by default, because isohybrid needs this
83 int optopt
= 0; /* character checked for validity */
85 int optreset
; /* reset getopt */
86 char *optarg
; /* argument associated with option */
88 #define PRINT_ERROR ((opterr) && (*options != ':'))
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 */
95 #define BADCH (int)'?'
96 #define BADARG ((*options == ':') ? (int)':' : (int)'?')
97 #define INORDER (int)1
101 static char *place
= EMSG
; /* option letter processing */
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) */
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";
116 * Compute the greatest common divisor of a and b.
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
139 permute_args(int panonopt_start
, int panonopt_end
, int opt_end
,
142 int cstart
, cyclelen
, i
, j
, ncycle
, nnonopts
, nopts
, pos
;
146 * compute lengths of blocks and number and size of cycles
148 nnonopts
= panonopt_end
- panonopt_start
;
149 nopts
= opt_end
- panonopt_end
;
150 ncycle
= gcd(nnonopts
, nopts
);
151 cyclelen
= (opt_end
- panonopt_start
) / ncycle
;
153 for (i
= 0; i
< ncycle
; i
++) {
154 cstart
= panonopt_end
+ i
;
156 for (j
= 0; j
< cyclelen
; j
++) {
157 if (pos
>= panonopt_end
)
162 /* LINTED const cast */
163 ((char **)nargv
)[pos
] = nargv
[cstart
];
164 /* LINTED const cast */
165 ((char **)nargv
)[cstart
] = swap
;
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.
176 parse_long_options(char * const *nargv
, const char *options
,
177 const struct option
*long_options
, int *idx
, int short_too
)
179 char *current_argv
, *has_equal
;
180 size_t current_argv_len
;
181 int i
, ambiguous
, match
;
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)
188 current_argv
= place
;
194 if ((has_equal
= strchr(current_argv
, '=')) != NULL
) {
195 /* argument found (--option=arg) */
196 current_argv_len
= has_equal
- current_argv
;
200 current_argv_len
= strlen(current_argv
);
202 for (i
= 0; long_options
[i
].name
; i
++) {
203 /* find matching long option */
204 if (strncmp(current_argv
, long_options
[i
].name
,
208 if (strlen(long_options
[i
].name
) == current_argv_len
) {
215 * If this is a known short option, don't allow
216 * a partial match of a single character.
218 if (short_too
&& current_argv_len
== 1)
221 if (match
== -1) /* partial match */
223 else if (!IDENTICAL_INTERPRETATION(i
, match
))
227 /* ambiguous abbreviation */
229 warnx(ambig
, (int)current_argv_len
,
234 if (match
!= -1) { /* option found */
235 if (long_options
[match
].has_arg
== no_argument
238 warnx(noarg
, (int)current_argv_len
,
241 * XXX: GNU sets optopt to val regardless of flag
243 if (long_options
[match
].flag
== NULL
)
244 optopt
= long_options
[match
].val
;
249 if (long_options
[match
].has_arg
== required_argument
||
250 long_options
[match
].has_arg
== optional_argument
) {
253 else if (long_options
[match
].has_arg
==
256 * optional argument doesn't use next nargv
258 optarg
= nargv
[optind
++];
261 if ((long_options
[match
].has_arg
== required_argument
)
262 && (optarg
== NULL
)) {
264 * Missing argument; leading ':' indicates no error
265 * should be generated.
271 * XXX: GNU sets optopt to val regardless of flag
273 if (long_options
[match
].flag
== NULL
)
274 optopt
= long_options
[match
].val
;
281 else { /* unknown option */
287 warnx(illoptstring
, current_argv
);
293 if (long_options
[match
].flag
) {
294 *long_options
[match
].flag
= long_options
[match
].val
;
298 return (long_options
[match
].val
);
299 #undef IDENTICAL_INTERPRETATION
304 * Parse argc/argv argument vector. Called by user level routines.
307 getopt_internal(int nargc
, char * const *nargv
, const char *options
,
308 const struct option
*long_options
, int *idx
, int flags
)
310 char *oli
; /* option letter list index */
311 int optchar
, short_too
;
312 static int posixly_correct
= -1;
318 * XXX Some GNU programs (like cvs) set optind to 0 instead of
319 * XXX using optreset. Work around this braindamage.
322 optind
= optreset
= 1;
325 * Disable GNU extensions if POSIXLY_CORRECT is set or options
326 * string begins with a '+'.
328 * CV, 2009-12-14: Check POSIXLY_CORRECT anew if optind == 0 or
329 * optreset != 0 for GNU compatibility.
331 if (posixly_correct
== -1 || optreset
!= 0)
332 posixly_correct
= (getenv("POSIXLY_CORRECT") != NULL
);
334 flags
|= FLAG_ALLARGS
;
335 else if (posixly_correct
|| *options
== '+')
336 flags
&= ~FLAG_PERMUTE
;
337 if (*options
== '+' || *options
== '-')
342 nonopt_start
= nonopt_end
= -1;
344 if (optreset
|| !*place
) { /* update scanning pointer */
346 if (optind
>= nargc
) { /* end of argument vector */
348 if (nonopt_end
!= -1) {
349 /* do permutation, if we have to */
350 permute_args(nonopt_start
, nonopt_end
,
352 optind
-= nonopt_end
- nonopt_start
;
354 else if (nonopt_start
!= -1) {
356 * If we skipped non-options, set optind
357 * to the first of them.
359 optind
= nonopt_start
;
361 nonopt_start
= nonopt_end
= -1;
364 if (*(place
= nargv
[optind
]) != '-' ||
365 (place
[1] == '\0' && strchr(options
, '-') == NULL
)) {
366 place
= EMSG
; /* found non-option */
367 if (flags
& FLAG_ALLARGS
) {
370 * return non-option as argument to option 1
372 optarg
= nargv
[optind
++];
375 if (!(flags
& FLAG_PERMUTE
)) {
377 * If no permutation wanted, stop parsing
378 * at first non-option.
383 if (nonopt_start
== -1)
384 nonopt_start
= optind
;
385 else if (nonopt_end
!= -1) {
386 permute_args(nonopt_start
, nonopt_end
,
388 nonopt_start
= optind
-
389 (nonopt_end
- nonopt_start
);
393 /* process next argument */
396 if (nonopt_start
!= -1 && nonopt_end
== -1)
400 * If we have "-" do nothing, if "--" we are done.
402 if (place
[1] != '\0' && *++place
== '-' && place
[1] == '\0') {
406 * We found an option (--), so if we skipped
407 * non-options, we have to permute.
409 if (nonopt_end
!= -1) {
410 permute_args(nonopt_start
, nonopt_end
,
412 optind
-= nonopt_end
- nonopt_start
;
414 nonopt_start
= nonopt_end
= -1;
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()
425 if (long_options
!= NULL
&& place
!= nargv
[optind
] &&
426 (*place
== '-' || (flags
& FLAG_LONGONLY
))) {
429 place
++; /* --foo long option */
430 else if (*place
!= ':' && strchr(options
, *place
) != NULL
)
431 short_too
= 1; /* could be short option too */
433 optchar
= parse_long_options(nargv
, options
, long_options
,
441 if ((optchar
= (int)*place
++) == (int)':' ||
442 (optchar
== (int)'-' && *place
!= '\0') ||
443 (oli
= strchr(options
, optchar
)) == NULL
) {
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 ':').
449 if (optchar
== (int)'-' && *place
== '\0')
454 warnx(illoptchar
, optchar
);
458 if (long_options
!= NULL
&& optchar
== 'W' && oli
[1] == ';') {
460 if (*place
) /* no space */
462 else if (++optind
>= nargc
) { /* no arg */
465 warnx(recargchar
, optchar
);
469 else /* white space */
470 place
= nargv
[optind
];
471 optchar
= parse_long_options(nargv
, options
, long_options
,
476 if (*++oli
!= ':') { /* doesn't take argument */
480 else { /* takes (optional) argument */
482 if (*place
) /* no white space */
484 else if (oli
[1] != ':') { /* arg not optional */
485 if (++optind
>= nargc
) { /* no arg */
488 warnx(recargchar
, optchar
);
493 optarg
= nargv
[optind
];
498 /* dump back option letter */
503 * getopt_long_only --
504 * Parse argc/argv argument vector.
507 getopt_long_only(int nargc
, char * const *nargv
, const char *options
,
508 const struct option
*long_options
, int *idx
)
511 return (getopt_internal(nargc
, nargv
, options
, long_options
, idx
,
512 FLAG_PERMUTE
| FLAG_LONGONLY
));
514 /////////////////////////////////////////////////////////////////////////////
520 HANDLE hFile
= (HANDLE
)_get_osfhandle(fd
);
521 if (hFile
== INVALID_HANDLE_VALUE
)
524 return !FlushFileBuffers(hFile
);
530 // Just return any nonzero value under Windows to enable isohybrid's usage
531 // as a part of srand initialization.