aa9f2a4261c8cfd44d59f2cb1a8320b5a20cea4b
1 #include "reactos_support_code.h"
4 isohybrid_error(int eval
, const char* fmt
, ...)
8 fprintf(stderr
, "isohybrid: ");
9 vfprintf(stderr
, fmt
, ap
);
15 isohybrid_warning(const char *fmt
, ...)
19 fprintf(stderr
, "isohybrid: ");
20 vfprintf(stderr
, fmt
, ap
);
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 $ */
31 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
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.
50 * Copyright (c) 2000 The NetBSD Foundation, Inc.
51 * All rights reserved.
53 * This code is derived from software contributed to The NetBSD Foundation
54 * by Dieter Baron and Thomas Klausner.
56 * Redistribution and use in source and binary forms, with or without
57 * modification, are permitted provided that the following conditions
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.
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.
77 int opterr
= 1; /* if error message should be printed */
78 int optind
= 1; /* index into parent argv vector */
80 ///// REACTOS ONLY: optopt set to 0 by default, because isohybrid needs this
81 int optopt
= 0; /* character checked for validity */
83 int optreset
; /* reset getopt */
84 char *optarg
; /* argument associated with option */
86 #define PRINT_ERROR ((opterr) && (*options != ':'))
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 */
93 #define BADCH (int)'?'
94 #define BADARG ((*options == ':') ? (int)':' : (int)'?')
95 #define INORDER (int)1
99 static char *place
= EMSG
; /* option letter processing */
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) */
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";
114 * Compute the greatest common divisor of a and b.
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
137 permute_args(int panonopt_start
, int panonopt_end
, int opt_end
,
140 int cstart
, cyclelen
, i
, j
, ncycle
, nnonopts
, nopts
, pos
;
144 * compute lengths of blocks and number and size of cycles
146 nnonopts
= panonopt_end
- panonopt_start
;
147 nopts
= opt_end
- panonopt_end
;
148 ncycle
= gcd(nnonopts
, nopts
);
149 cyclelen
= (opt_end
- panonopt_start
) / ncycle
;
151 for (i
= 0; i
< ncycle
; i
++) {
152 cstart
= panonopt_end
+ i
;
154 for (j
= 0; j
< cyclelen
; j
++) {
155 if (pos
>= panonopt_end
)
160 /* LINTED const cast */
161 ((char **)nargv
)[pos
] = nargv
[cstart
];
162 /* LINTED const cast */
163 ((char **)nargv
)[cstart
] = swap
;
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.
174 parse_long_options(char * const *nargv
, const char *options
,
175 const struct option
*long_options
, int *idx
, int short_too
)
177 char *current_argv
, *has_equal
;
178 size_t current_argv_len
;
179 int i
, ambiguous
, match
;
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)
186 current_argv
= place
;
192 if ((has_equal
= strchr(current_argv
, '=')) != NULL
) {
193 /* argument found (--option=arg) */
194 current_argv_len
= has_equal
- current_argv
;
198 current_argv_len
= strlen(current_argv
);
200 for (i
= 0; long_options
[i
].name
; i
++) {
201 /* find matching long option */
202 if (strncmp(current_argv
, long_options
[i
].name
,
206 if (strlen(long_options
[i
].name
) == current_argv_len
) {
213 * If this is a known short option, don't allow
214 * a partial match of a single character.
216 if (short_too
&& current_argv_len
== 1)
219 if (match
== -1) /* partial match */
221 else if (!IDENTICAL_INTERPRETATION(i
, match
))
225 /* ambiguous abbreviation */
227 warnx(ambig
, (int)current_argv_len
,
232 if (match
!= -1) { /* option found */
233 if (long_options
[match
].has_arg
== no_argument
236 warnx(noarg
, (int)current_argv_len
,
239 * XXX: GNU sets optopt to val regardless of flag
241 if (long_options
[match
].flag
== NULL
)
242 optopt
= long_options
[match
].val
;
247 if (long_options
[match
].has_arg
== required_argument
||
248 long_options
[match
].has_arg
== optional_argument
) {
251 else if (long_options
[match
].has_arg
==
254 * optional argument doesn't use next nargv
256 optarg
= nargv
[optind
++];
259 if ((long_options
[match
].has_arg
== required_argument
)
260 && (optarg
== NULL
)) {
262 * Missing argument; leading ':' indicates no error
263 * should be generated.
269 * XXX: GNU sets optopt to val regardless of flag
271 if (long_options
[match
].flag
== NULL
)
272 optopt
= long_options
[match
].val
;
279 else { /* unknown option */
285 warnx(illoptstring
, current_argv
);
291 if (long_options
[match
].flag
) {
292 *long_options
[match
].flag
= long_options
[match
].val
;
296 return (long_options
[match
].val
);
297 #undef IDENTICAL_INTERPRETATION
302 * Parse argc/argv argument vector. Called by user level routines.
305 getopt_internal(int nargc
, char * const *nargv
, const char *options
,
306 const struct option
*long_options
, int *idx
, int flags
)
308 char *oli
; /* option letter list index */
309 int optchar
, short_too
;
310 static int posixly_correct
= -1;
316 * XXX Some GNU programs (like cvs) set optind to 0 instead of
317 * XXX using optreset. Work around this braindamage.
320 optind
= optreset
= 1;
323 * Disable GNU extensions if POSIXLY_CORRECT is set or options
324 * string begins with a '+'.
326 * CV, 2009-12-14: Check POSIXLY_CORRECT anew if optind == 0 or
327 * optreset != 0 for GNU compatibility.
329 if (posixly_correct
== -1 || optreset
!= 0)
330 posixly_correct
= (getenv("POSIXLY_CORRECT") != NULL
);
332 flags
|= FLAG_ALLARGS
;
333 else if (posixly_correct
|| *options
== '+')
334 flags
&= ~FLAG_PERMUTE
;
335 if (*options
== '+' || *options
== '-')
340 nonopt_start
= nonopt_end
= -1;
342 if (optreset
|| !*place
) { /* update scanning pointer */
344 if (optind
>= nargc
) { /* end of argument vector */
346 if (nonopt_end
!= -1) {
347 /* do permutation, if we have to */
348 permute_args(nonopt_start
, nonopt_end
,
350 optind
-= nonopt_end
- nonopt_start
;
352 else if (nonopt_start
!= -1) {
354 * If we skipped non-options, set optind
355 * to the first of them.
357 optind
= nonopt_start
;
359 nonopt_start
= nonopt_end
= -1;
362 if (*(place
= nargv
[optind
]) != '-' ||
363 (place
[1] == '\0' && strchr(options
, '-') == NULL
)) {
364 place
= EMSG
; /* found non-option */
365 if (flags
& FLAG_ALLARGS
) {
368 * return non-option as argument to option 1
370 optarg
= nargv
[optind
++];
373 if (!(flags
& FLAG_PERMUTE
)) {
375 * If no permutation wanted, stop parsing
376 * at first non-option.
381 if (nonopt_start
== -1)
382 nonopt_start
= optind
;
383 else if (nonopt_end
!= -1) {
384 permute_args(nonopt_start
, nonopt_end
,
386 nonopt_start
= optind
-
387 (nonopt_end
- nonopt_start
);
391 /* process next argument */
394 if (nonopt_start
!= -1 && nonopt_end
== -1)
398 * If we have "-" do nothing, if "--" we are done.
400 if (place
[1] != '\0' && *++place
== '-' && place
[1] == '\0') {
404 * We found an option (--), so if we skipped
405 * non-options, we have to permute.
407 if (nonopt_end
!= -1) {
408 permute_args(nonopt_start
, nonopt_end
,
410 optind
-= nonopt_end
- nonopt_start
;
412 nonopt_start
= nonopt_end
= -1;
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()
423 if (long_options
!= NULL
&& place
!= nargv
[optind
] &&
424 (*place
== '-' || (flags
& FLAG_LONGONLY
))) {
427 place
++; /* --foo long option */
428 else if (*place
!= ':' && strchr(options
, *place
) != NULL
)
429 short_too
= 1; /* could be short option too */
431 optchar
= parse_long_options(nargv
, options
, long_options
,
439 if ((optchar
= (int)*place
++) == (int)':' ||
440 (optchar
== (int)'-' && *place
!= '\0') ||
441 (oli
= strchr(options
, optchar
)) == NULL
) {
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 ':').
447 if (optchar
== (int)'-' && *place
== '\0')
452 warnx(illoptchar
, optchar
);
456 if (long_options
!= NULL
&& optchar
== 'W' && oli
[1] == ';') {
458 if (*place
) /* no space */
460 else if (++optind
>= nargc
) { /* no arg */
463 warnx(recargchar
, optchar
);
467 else /* white space */
468 place
= nargv
[optind
];
469 optchar
= parse_long_options(nargv
, options
, long_options
,
474 if (*++oli
!= ':') { /* doesn't take argument */
478 else { /* takes (optional) argument */
480 if (*place
) /* no white space */
482 else if (oli
[1] != ':') { /* arg not optional */
483 if (++optind
>= nargc
) { /* no arg */
486 warnx(recargchar
, optchar
);
491 optarg
= nargv
[optind
];
496 /* dump back option letter */
501 * getopt_long_only --
502 * Parse argc/argv argument vector.
505 getopt_long_only(int nargc
, char * const *nargv
, const char *options
,
506 const struct option
*long_options
, int *idx
)
509 return (getopt_internal(nargc
, nargv
, options
, long_options
, idx
,
510 FLAG_PERMUTE
| FLAG_LONGONLY
));
512 /////////////////////////////////////////////////////////////////////////////
518 HANDLE hFile
= (HANDLE
)_get_osfhandle(fd
);
519 if (hFile
== INVALID_HANDLE_VALUE
)
522 return !FlushFileBuffers(hFile
);
528 // Just return any nonzero value under Windows to enable isohybrid's usage
529 // as a part of srand initialization.