2 Copied from glibc-2.15 modified to compile on Windows.
5 NOTE: getopt is part of the C library, so if you don't know what
6 "Keep this file name-space clean" means, talk to drepper@gnu.org
8 Copyright (C) 1987-1996,1998-2004,2008,2009,2010,2011
9 Free Software Foundation, Inc.
10 This file is part of the GNU C Library.
12 The GNU C Library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 2.1 of the License, or (at your option) any later version.
17 The GNU C Library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with the GNU C Library; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
28 Ditto for AIX 3.2 and <stdlib.h>. */
37 /* Comment out all this code if we are using the GNU C Library, and are not
38 actually compiling the library itself. This code is part of the GNU C
39 Library, but also included in many other GNU distributions. Compiling
40 and linking in this code is a waste when using the GNU C library
41 (especially if it is a shared library). Rather than having every GNU
42 program understand `configure --with-gnu-libc' and omit the object files,
43 it is simpler to just do this in the source for each such file. */
45 #define GETOPT_INTERFACE_VERSION 2
46 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
47 # include <gnu-versions.h>
48 # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
56 /* This needs to come after some library #include
57 to get __GNU_LIBRARY__ defined. */
58 #ifdef __GNU_LIBRARY__
59 /* Don't include stdlib.h for non-GNU C libraries because some of them
60 contain conflicting prototypes for getopt. */
63 #endif /* GNU C library. */
74 /*# include "gettext.h" */
75 /*# define _(msgid) gettext (msgid) */
76 # define _(msgid) (msgid)
80 # define alloca _alloca
87 #ifndef attribute_hidden
88 # define attribute_hidden
91 /* This version of `getopt' appears to the caller like standard Unix `getopt'
92 but it behaves differently for the user, since it allows the user
93 to intersperse the options with the other arguments.
95 As `getopt' works, it permutes the elements of ARGV so that,
96 when it is done, all the options precede everything else. Thus
97 all application programs are extended to handle flexible argument order.
99 Setting the environment variable POSIXLY_CORRECT disables permutation.
100 Then the behavior is completely standard.
102 GNU application programs can use a third alternative mode in which
103 they can distinguish the relative order of options and other arguments. */
105 #include "wsgetopt.h"
106 #include "wsgetopt_int.h"
108 /* For communication from `getopt' to the caller.
109 When `getopt' finds an option that takes an argument,
110 the argument value is returned here.
111 Also, when `ordering' is RETURN_IN_ORDER,
112 each non-option ARGV-element is returned here. */
116 /* Index in ARGV of the next element to be scanned.
117 This is used for communication to and from the caller
118 and for communication between successive calls to `getopt'.
120 On entry to `getopt', zero means this is the first call; initialize.
122 When `getopt' returns -1, this is the index of the first of the
123 non-option elements that the caller should itself scan.
125 Otherwise, `optind' communicates from one call to the next
126 how much of ARGV has been scanned so far. */
128 /* 1003.2 says this must be 1 before any call. */
131 /* Callers store zero here to inhibit the error message
132 for unrecognized options. */
136 /* Set to an option character which was unrecognized.
137 This must be initialized on some systems to avoid linking in the
138 system's own getopt implementation. */
142 /* Keep a global copy of all internal members of getopt_data. */
144 static struct _getopt_data getopt_data;
146 #ifndef __GNU_LIBRARY__
148 /* Avoid depending on library functions or files
149 whose names are inconsistent. */
152 extern char *getenv ();
155 #endif /* not __GNU_LIBRARY__ */
158 /* Stored original parameters.
159 XXX This is no good solution. We should rather copy the args so
160 that we can compare them later. But we must not use malloc(3). */
161 extern int __libc_argc;
162 extern char **__libc_argv;
164 /* Bash 2.0 gives us an environment variable containing flags
165 indicating ARGV elements that should not be considered arguments. */
167 # ifdef USE_NONOPTION_FLAGS
168 /* Defined in getopt_init.c */
169 extern char *__getopt_nonoption_flags;
172 # ifdef USE_NONOPTION_FLAGS
173 # define SWAP_FLAGS(ch1, ch2) \
174 if (d->__nonoption_flags_len > 0) \
176 char __tmp = __getopt_nonoption_flags[ch1]; \
177 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
178 __getopt_nonoption_flags[ch2] = __tmp; \
181 # define SWAP_FLAGS(ch1, ch2)
184 # define SWAP_FLAGS(ch1, ch2)
187 /* Exchange two adjacent subsequences of ARGV.
188 One subsequence is elements [first_nonopt,last_nonopt)
189 which contains all the non-options that have been skipped so far.
190 The other is elements [last_nonopt,optind), which contains all
191 the options processed since those non-options were skipped.
193 `first_nonopt' and `last_nonopt' are relocated so that they describe
194 the new indices of the non-options in ARGV after they are moved. */
197 exchange (char **argv, struct _getopt_data *d)
199 int bottom = d->__first_nonopt;
200 int middle = d->__last_nonopt;
204 /* Exchange the shorter segment with the far end of the longer segment.
205 That puts the shorter segment into the right place.
206 It leaves the longer segment in the right place overall,
207 but it consists of two parts that need to be swapped next. */
209 #if defined _LIBC && defined USE_NONOPTION_FLAGS
210 /* First make sure the handling of the `__getopt_nonoption_flags'
211 string can work normally. Our top argument must be in the range
213 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
215 /* We must extend the array. The user plays games with us and
216 presents new arguments. */
217 char *new_str = malloc (top + 1);
219 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
222 memset (__mempcpy (new_str, __getopt_nonoption_flags,
223 d->__nonoption_flags_max_len),
224 '\0', top + 1 - d->__nonoption_flags_max_len);
225 d->__nonoption_flags_max_len = top + 1;
226 __getopt_nonoption_flags = new_str;
231 while (top > middle && middle > bottom)
233 if (top - middle > middle - bottom)
235 /* Bottom segment is the short one. */
236 int len = middle - bottom;
239 /* Swap it with the top part of the top segment. */
240 for (i = 0; i < len; i++)
242 tem = argv[bottom + i];
243 argv[bottom + i] = argv[top - (middle - bottom) + i];
244 argv[top - (middle - bottom) + i] = tem;
245 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
247 /* Exclude the moved bottom segment from further swapping. */
252 /* Top segment is the short one. */
253 int len = top - middle;
256 /* Swap it with the bottom part of the bottom segment. */
257 for (i = 0; i < len; i++)
259 tem = argv[bottom + i];
260 argv[bottom + i] = argv[middle + i];
261 argv[middle + i] = tem;
262 SWAP_FLAGS (bottom + i, middle + i);
264 /* Exclude the moved top segment from further swapping. */
269 /* Update records for the slots the non-options now occupy. */
271 d->__first_nonopt += (d->optind - d->__last_nonopt);
272 d->__last_nonopt = d->optind;
275 /* Initialize the internal data when the first call is made. */
278 _getopt_initialize (int argc, char *const *argv, const char *optstring,
279 struct _getopt_data *d, int posixly_correct)
281 /* Start processing options with ARGV-element 1 (since ARGV-element 0
282 is the program name); the sequence of previously skipped
283 non-option ARGV-elements is empty. */
285 d->__first_nonopt = d->__last_nonopt = d->optind;
287 d->__nextchar = NULL;
289 d->__posixly_correct = posixly_correct | !!getenv ("POSIXLY_CORRECT");
291 /* Determine how to handle the ordering of options and nonoptions. */
293 if (optstring[0] == '-')
295 d->__ordering = RETURN_IN_ORDER;
298 else if (optstring[0] == '+')
300 d->__ordering = REQUIRE_ORDER;
303 else if (d->__posixly_correct)
304 d->__ordering = REQUIRE_ORDER;
306 d->__ordering = PERMUTE;
308 #if defined _LIBC && defined USE_NONOPTION_FLAGS
309 if (!d->__posixly_correct
310 && argc == __libc_argc && argv == __libc_argv)
312 if (d->__nonoption_flags_max_len == 0)
314 if (__getopt_nonoption_flags == NULL
315 || __getopt_nonoption_flags[0] == '\0')
316 d->__nonoption_flags_max_len = -1;
319 const char *orig_str = __getopt_nonoption_flags;
320 int len = d->__nonoption_flags_max_len = strlen (orig_str);
321 if (d->__nonoption_flags_max_len < argc)
322 d->__nonoption_flags_max_len = argc;
323 __getopt_nonoption_flags =
324 (char *) malloc (d->__nonoption_flags_max_len);
325 if (__getopt_nonoption_flags == NULL)
326 d->__nonoption_flags_max_len = -1;
328 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
329 '\0', d->__nonoption_flags_max_len - len);
332 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
335 d->__nonoption_flags_len = 0;
341 /* Scan elements of ARGV (whose length is ARGC) for option characters
344 If an element of ARGV starts with '-', and is not exactly "-" or "--",
345 then it is an option element. The characters of this element
346 (aside from the initial '-') are option characters. If `getopt'
347 is called repeatedly, it returns successively each of the option characters
348 from each of the option elements.
350 If `getopt' finds another option character, it returns that character,
351 updating `optind' and `nextchar' so that the next call to `getopt' can
352 resume the scan with the following option character or ARGV-element.
354 If there are no more option characters, `getopt' returns -1.
355 Then `optind' is the index in ARGV of the first ARGV-element
356 that is not an option. (The ARGV-elements have been permuted
357 so that those that are not options now come last.)
359 OPTSTRING is a string containing the legitimate option characters.
360 If an option character is seen that is not listed in OPTSTRING,
361 return '?' after printing an error message. If you set `opterr' to
362 zero, the error message is suppressed but we still return '?'.
364 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
365 so the following text in the same ARGV-element, or the text of the following
366 ARGV-element, is returned in `optarg'. Two colons mean an option that
367 wants an optional arg; if there is text in the current ARGV-element,
368 it is returned in `optarg', otherwise `optarg' is set to zero.
370 If OPTSTRING starts with `-' or `+', it requests different methods of
371 handling the non-option ARGV-elements.
372 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
374 Long-named options begin with `--' instead of `-'.
375 Their names may be abbreviated as long as the abbreviation is unique
376 or is an exact match for some defined option. If they have an
377 argument, it follows the option name in the same ARGV-element, separated
378 from the option name by a `=', or else the in next ARGV-element.
379 When `getopt' finds a long-named option, it returns 0 if that option's
380 `flag' field is nonzero, the value of the option's `val' field
381 if the `flag' field is zero.
383 The elements of ARGV aren't really const, because we permute them.
384 But we pretend they're const in the prototype to be compatible
387 LONGOPTS is a vector of `struct option' terminated by an
388 element containing a name which is zero.
390 LONGIND returns the index in LONGOPT of the long-named option found.
391 It is only valid when a long-named option has been found by the most
394 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
395 long-named options. */
398 _getopt_internal_r (int argc, char *const *argv, const char *optstring,
399 const struct option *longopts, int *longind,
400 int long_only, struct _getopt_data *d, int posixly_correct)
402 int print_errors = d->opterr;
409 if (d->optind == 0 || !d->__initialized)
412 d->optind = 1; /* Don't scan ARGV[0], the program name. */
413 optstring = _getopt_initialize (argc, argv, optstring, d,
415 d->__initialized = 1;
417 else if (optstring[0] == '-' || optstring[0] == '+')
419 if (optstring[0] == ':')
422 /* Test whether ARGV[optind] points to a non-option argument.
423 Either it does not have option syntax, or there is an environment flag
424 from the shell indicating it is not an option. The later information
425 is only used when the used in the GNU libc. */
426 #if defined _LIBC && defined USE_NONOPTION_FLAGS
427 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
428 || (d->optind < d->__nonoption_flags_len \
429 && __getopt_nonoption_flags[d->optind] == '1'))
431 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
434 if (d->__nextchar == NULL || *d->__nextchar == '\0')
436 /* Advance to the next ARGV-element. */
438 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
439 moved back by the user (who may also have changed the arguments). */
440 if (d->__last_nonopt > d->optind)
441 d->__last_nonopt = d->optind;
442 if (d->__first_nonopt > d->optind)
443 d->__first_nonopt = d->optind;
445 if (d->__ordering == PERMUTE)
447 /* If we have just processed some options following some non-options,
448 exchange them so that the options come first. */
450 if (d->__first_nonopt != d->__last_nonopt
451 && d->__last_nonopt != d->optind)
452 exchange ((char **) argv, d);
453 else if (d->__last_nonopt != d->optind)
454 d->__first_nonopt = d->optind;
456 /* Skip any additional non-options
457 and extend the range of non-options previously skipped. */
459 while (d->optind < argc && NONOPTION_P)
461 d->__last_nonopt = d->optind;
464 /* The special ARGV-element `--' means premature end of options.
465 Skip it like a null option,
466 then exchange with previous non-options as if it were an option,
467 then skip everything else like a non-option. */
469 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
473 if (d->__first_nonopt != d->__last_nonopt
474 && d->__last_nonopt != d->optind)
475 exchange ((char **) argv, d);
476 else if (d->__first_nonopt == d->__last_nonopt)
477 d->__first_nonopt = d->optind;
478 d->__last_nonopt = argc;
483 /* If we have done all the ARGV-elements, stop the scan
484 and back over any non-options that we skipped and permuted. */
486 if (d->optind == argc)
488 /* Set the next-arg-index to point at the non-options
489 that we previously skipped, so the caller will digest them. */
490 if (d->__first_nonopt != d->__last_nonopt)
491 d->optind = d->__first_nonopt;
495 /* If we have come to a non-option and did not permute it,
496 either stop the scan or describe it to the caller and pass it by. */
500 if (d->__ordering == REQUIRE_ORDER)
502 d->optarg = argv[d->optind++];
506 /* We have found another option-ARGV-element.
507 Skip the initial punctuation. */
509 d->__nextchar = (argv[d->optind] + 1
510 + (longopts != NULL && argv[d->optind][1] == '-'));
513 /* Decode the current option-ARGV-element. */
515 /* Check whether the ARGV-element is a long option.
517 If long_only and the ARGV-element has the form "-f", where f is
518 a valid short option, don't consider it an abbreviated form of
519 a long option that starts with f. Otherwise there would be no
520 way to give the -f short option.
522 On the other hand, if there's a long option "fubar" and
523 the ARGV-element is "-fu", do consider that an abbreviation of
524 the long option, just like "--fu", and not "-f" with arg "u".
526 This distinction seems to be the most useful approach. */
529 && (argv[d->optind][1] == '-'
530 || (long_only && (argv[d->optind][2]
531 || !strchr (optstring, argv[d->optind][1])))))
535 const struct option *p;
536 const struct option *pfound = NULL;
539 const struct option *p;
540 struct option_list *next;
541 } *ambig_list = NULL;
546 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
548 namelen = nameend - d->__nextchar;
550 /* Test all long options for either exact match
551 or abbreviated matches. */
552 for (p = longopts, option_index = 0; p->name; p++, option_index++)
553 if (!strncmp (p->name, d->__nextchar, namelen))
555 if (namelen == (unsigned int) strlen (p->name))
557 /* Exact match found. */
559 indfound = option_index;
563 else if (pfound == NULL)
565 /* First nonexact match found. */
567 indfound = option_index;
570 || pfound->has_arg != p->has_arg
571 || pfound->flag != p->flag
572 || pfound->val != p->val)
574 /* Second or later nonexact match found. */
575 struct option_list *newp = alloca (sizeof (*newp));
577 newp->next = ambig_list;
582 if (ambig_list != NULL && !exact)
586 struct option_list first;
588 first.next = ambig_list;
595 FILE *fp = open_memstream (&buf, &buflen);
599 _("%s: option '%s' is ambiguous; possibilities:"),
600 argv[0], argv[d->optind]);
604 fprintf (fp, " '--%s'", ambig_list->p->name);
605 ambig_list = ambig_list->next;
607 while (ambig_list != NULL);
609 fputc_unlocked ('\n', fp);
611 if (__builtin_expect (fclose (fp) != EOF, 1))
613 _IO_flockfile (stderr);
615 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
616 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
618 __fxprintf (NULL, "%s", buf);
620 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
621 _IO_funlockfile (stderr);
628 _("%s: option '%s' is ambiguous; possibilities:"),
629 argv[0], argv[d->optind]);
632 fprintf (stderr, " '--%s'", ambig_list->p->name);
633 ambig_list = ambig_list->next;
635 while (ambig_list != NULL);
637 fputc ('\n', stderr);
640 d->__nextchar += strlen (d->__nextchar);
648 option_index = indfound;
652 /* Don't test has_arg with >, because some C compilers don't
653 allow it to be used on enums. */
655 d->optarg = nameend + 1;
665 if (argv[d->optind - 1][1] == '-')
669 n = __asprintf (&buf, _("\
670 %s: option '--%s' doesn't allow an argument\n"),
671 argv[0], pfound->name);
673 fprintf (stderr, _("\
674 %s: option '--%s' doesn't allow an argument\n"),
675 argv[0], pfound->name);
680 /* +option or -option */
682 n = __asprintf (&buf, _("\
683 %s: option '%c%s' doesn't allow an argument\n"),
684 argv[0], argv[d->optind - 1][0],
687 fprintf (stderr, _("\
688 %s: option '%c%s' doesn't allow an argument\n"),
689 argv[0], argv[d->optind - 1][0],
697 _IO_flockfile (stderr);
699 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
700 ((_IO_FILE *) stderr)->_flags2
701 |= _IO_FLAGS2_NOTCANCEL;
703 __fxprintf (NULL, "%s", buf);
705 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
706 _IO_funlockfile (stderr);
713 d->__nextchar += strlen (d->__nextchar);
715 d->optopt = pfound->val;
719 else if (pfound->has_arg == 1)
721 if (d->optind < argc)
722 d->optarg = argv[d->optind++];
730 if (__asprintf (&buf, _("\
731 %s: option '--%s' requires an argument\n"),
732 argv[0], pfound->name) >= 0)
734 _IO_flockfile (stderr);
736 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
737 ((_IO_FILE *) stderr)->_flags2
738 |= _IO_FLAGS2_NOTCANCEL;
740 __fxprintf (NULL, "%s", buf);
742 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
743 _IO_funlockfile (stderr);
749 _("%s: option '--%s' requires an argument\n"),
750 argv[0], pfound->name);
753 d->__nextchar += strlen (d->__nextchar);
754 d->optopt = pfound->val;
755 return optstring[0] == ':' ? ':' : '?';
758 d->__nextchar += strlen (d->__nextchar);
760 *longind = option_index;
763 *(pfound->flag) = pfound->val;
769 /* Can't find it as a long option. If this is not getopt_long_only,
770 or the option starts with '--' or is not a valid short
771 option, then it's an error.
772 Otherwise interpret it as a short option. */
773 if (!long_only || argv[d->optind][1] == '-'
774 || strchr (optstring, *d->__nextchar) == NULL)
783 if (argv[d->optind][1] == '-')
787 n = __asprintf (&buf, _("%s: unrecognized option '--%s'\n"),
788 argv[0], d->__nextchar);
790 fprintf (stderr, _("%s: unrecognized option '--%s'\n"),
791 argv[0], d->__nextchar);
796 /* +option or -option */
798 n = __asprintf (&buf, _("%s: unrecognized option '%c%s'\n"),
799 argv[0], argv[d->optind][0], d->__nextchar);
801 fprintf (stderr, _("%s: unrecognized option '%c%s'\n"),
802 argv[0], argv[d->optind][0], d->__nextchar);
809 _IO_flockfile (stderr);
811 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
812 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
814 __fxprintf (NULL, "%s", buf);
816 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
817 _IO_funlockfile (stderr);
823 d->__nextchar = (char *) "";
830 /* Look at and handle the next short option-character. */
833 char c = *d->__nextchar++;
834 char *temp = strchr (optstring, c);
836 const struct option *p;
837 const struct option *pfound = NULL;
843 /* Increment `optind' when we start to process its last character. */
844 if (*d->__nextchar == '\0')
847 if (temp == NULL || c == ':' || c == ';')
857 n = __asprintf (&buf, _("%s: invalid option -- '%c'\n"),
860 fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
866 _IO_flockfile (stderr);
868 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
869 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
871 __fxprintf (NULL, "%s", buf);
873 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
874 _IO_funlockfile (stderr);
883 /* Convenience. Treat POSIX -W foo same as long option --foo */
884 if (temp[0] == 'W' && temp[1] == ';')
886 if (longopts == NULL)
890 /* This is an option that requires an argument. */
891 if (*d->__nextchar != '\0')
893 d->optarg = d->__nextchar;
894 /* If we end this ARGV-element by taking the rest as an arg,
895 we must advance to the next element now. */
898 else if (d->optind == argc)
905 if (__asprintf (&buf,
906 _("%s: option requires an argument -- '%c'\n"),
909 _IO_flockfile (stderr);
911 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
912 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
914 __fxprintf (NULL, "%s", buf);
916 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
917 _IO_funlockfile (stderr);
923 _("%s: option requires an argument -- '%c'\n"),
928 if (optstring[0] == ':')
935 /* We already incremented `d->optind' once;
936 increment it again when taking next ARGV-elt as argument. */
937 d->optarg = argv[d->optind++];
939 /* optarg is now the argument, see if it's in the
940 table of longopts. */
942 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
946 /* Test all long options for either exact match
947 or abbreviated matches. */
948 for (p = longopts, option_index = 0; p->name; p++, option_index++)
949 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
951 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
953 /* Exact match found. */
955 indfound = option_index;
959 else if (pfound == NULL)
961 /* First nonexact match found. */
963 indfound = option_index;
966 || pfound->has_arg != p->has_arg
967 || pfound->flag != p->flag
968 || pfound->val != p->val)
969 /* Second or later nonexact match found. */
979 if (__asprintf (&buf, _("%s: option '-W %s' is ambiguous\n"),
980 argv[0], d->optarg) >= 0)
982 _IO_flockfile (stderr);
984 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
985 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
987 __fxprintf (NULL, "%s", buf);
989 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
990 _IO_funlockfile (stderr);
995 fprintf (stderr, _("%s: option '-W %s' is ambiguous\n"),
999 d->__nextchar += strlen (d->__nextchar);
1005 option_index = indfound;
1008 /* Don't test has_arg with >, because some C compilers don't
1009 allow it to be used on enums. */
1010 if (pfound->has_arg)
1011 d->optarg = nameend + 1;
1019 if (__asprintf (&buf, _("\
1020 %s: option '-W %s' doesn't allow an argument\n"),
1021 argv[0], pfound->name) >= 0)
1023 _IO_flockfile (stderr);
1025 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1026 ((_IO_FILE *) stderr)->_flags2
1027 |= _IO_FLAGS2_NOTCANCEL;
1029 __fxprintf (NULL, "%s", buf);
1031 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1032 _IO_funlockfile (stderr);
1037 fprintf (stderr, _("\
1038 %s: option '-W %s' doesn't allow an argument\n"),
1039 argv[0], pfound->name);
1043 d->__nextchar += strlen (d->__nextchar);
1047 else if (pfound->has_arg == 1)
1049 if (d->optind < argc)
1050 d->optarg = argv[d->optind++];
1058 if (__asprintf (&buf, _("\
1059 %s: option '-W %s' requires an argument\n"),
1060 argv[0], pfound->name) >= 0)
1062 _IO_flockfile (stderr);
1064 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1065 ((_IO_FILE *) stderr)->_flags2
1066 |= _IO_FLAGS2_NOTCANCEL;
1068 __fxprintf (NULL, "%s", buf);
1070 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1071 _IO_funlockfile (stderr);
1076 fprintf (stderr, _("\
1077 %s: option '-W %s' requires an argument\n"),
1078 argv[0], pfound->name);
1081 d->__nextchar += strlen (d->__nextchar);
1082 return optstring[0] == ':' ? ':' : '?';
1087 d->__nextchar += strlen (d->__nextchar);
1088 if (longind != NULL)
1089 *longind = option_index;
1092 *(pfound->flag) = pfound->val;
1099 d->__nextchar = NULL;
1100 return 'W'; /* Let the application handle it. */
1106 /* This is an option that accepts an argument optionally. */
1107 if (*d->__nextchar != '\0')
1109 d->optarg = d->__nextchar;
1114 d->__nextchar = NULL;
1118 /* This is an option that requires an argument. */
1119 if (*d->__nextchar != '\0')
1121 d->optarg = d->__nextchar;
1122 /* If we end this ARGV-element by taking the rest as an arg,
1123 we must advance to the next element now. */
1126 else if (d->optind == argc)
1133 if (__asprintf (&buf, _("\
1134 %s: option requires an argument -- '%c'\n"),
1137 _IO_flockfile (stderr);
1139 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1140 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1142 __fxprintf (NULL, "%s", buf);
1144 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1145 _IO_funlockfile (stderr);
1151 _("%s: option requires an argument -- '%c'\n"),
1156 if (optstring[0] == ':')
1162 /* We already incremented `optind' once;
1163 increment it again when taking next ARGV-elt as argument. */
1164 d->optarg = argv[d->optind++];
1165 d->__nextchar = NULL;
1173 _getopt_internal (int argc, char *const *argv, const char *optstring,
1174 const struct option *longopts, int *longind, int long_only,
1175 int posixly_correct)
1179 getopt_data.optind = optind;
1180 getopt_data.opterr = opterr;
1182 result = _getopt_internal_r (argc, argv, optstring, longopts,
1183 longind, long_only, &getopt_data,
1186 optind = getopt_data.optind;
1187 optarg = getopt_data.optarg;
1188 optopt = getopt_data.optopt;
1194 getopt (int argc, char *const *argv, const char *optstring)
1196 return _getopt_internal (argc, argv, optstring,
1197 (const struct option *) 0,
1202 /* getopt_long() was copied from posix/getopt1.c
1203 the rest of this file is a nearly identical copy of posix/getopt.c */
1205 getopt_long (int argc, char *const *argv, const char *options,
1206 const struct option *long_options, int *opt_index)
1208 return _getopt_internal (argc, argv, options, long_options,
1215 __posix_getopt (int argc, char *const *argv, const char *optstring)
1217 return _getopt_internal (argc, argv, optstring,
1218 (const struct option *) 0,
1224 #endif /* Not ELIDE_CODE. */
1228 /* Compile with -DTEST to make an executable for use in testing
1229 the above definition of `getopt'. */
1232 main (int argc, char **argv)
1235 int digit_optind = 0;
1239 int this_option_optind = optind ? optind : 1;
1241 c = getopt (argc, argv, "abc:d:0123456789");
1257 if (digit_optind != 0 && digit_optind != this_option_optind)
1258 printf ("digits occur in two different argv-elements.\n");
1259 digit_optind = this_option_optind;
1260 printf ("option %c\n", c);
1264 printf ("option a\n");
1268 printf ("option b\n");
1272 printf ("option c with value '%s'\n", optarg);
1279 printf ("?? getopt returned character code 0%o ??\n", c);
1285 printf ("non-option ARGV-elements: ");
1286 while (optind < argc)
1287 printf ("%s ", argv[optind++]);
1297 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1302 * indent-tabs-mode: nil
1305 * ex: set shiftwidth=2 tabstop=8 expandtab:
1306 * :indentSize=2:tabSize=8:noTabs=true: