2 NOTE: getopt is now part of the C library, so if you don't know what
3 "Keep this file name-space clean" means, talk to drepper@gnu.org
6 Copyright (C) 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
7 1996, 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation,
10 This file is part of the GNU C Library.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2, or (at your option)
17 This program 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
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation,
24 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
26 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
27 Ditto for AIX 3.2 and <stdlib.h>. */
38 /* Comment out all this code if we are using the GNU C Library, and are not
39 actually compiling the library itself. This code is part of the GNU C
40 Library, but also included in many other GNU distributions. Compiling
41 and linking in this code is a waste when using the GNU C library
42 (especially if it is a shared library). Rather than having every GNU
43 program understand `configure --with-gnu-libc' and omit the object files,
44 it is simpler to just do this in the source for each such file. */
46 #define GETOPT_INTERFACE_VERSION 2
47 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
48 # include <gnu-versions.h>
49 # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
57 /* This needs to come after some library #include
58 to get __GNU_LIBRARY__ defined. */
59 #ifdef __GNU_LIBRARY__
60 /* Don't include stdlib.h for non-GNU C libraries because some of them
61 contain conflicting prototypes for getopt. */
64 #endif /* GNU C library. */
76 /* This is for other GNU distributions with internationalized messages. */
79 #define _(msgid) gettext (msgid)
81 #define _(msgid) (msgid)
84 #if defined _LIBC && defined USE_IN_LIBIO
88 #ifndef attribute_hidden
89 # define attribute_hidden
92 /* This version of `getopt' appears to the caller like standard Unix `getopt'
93 but it behaves differently for the user, since it allows the user
94 to intersperse the options with the other arguments.
96 As `getopt' works, it permutes the elements of ARGV so that,
97 when it is done, all the options precede everything else. Thus
98 all application programs are extended to handle flexible argument order.
100 Setting the environment variable POSIXLY_CORRECT disables permutation.
101 Then the behavior is completely standard.
103 GNU application programs can use a third alternative mode in which
104 they can distinguish the relative order of options and other arguments. */
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 /* Formerly, initialization of getopt depended on optind==0, which
132 causes problems with re-calling getopt as programs generally don't
135 int __getopt_initialized attribute_hidden;
137 /* The next char to be scanned in the option-element
138 in which the last option character we returned was found.
139 This allows us to pick up the scan where we left off.
141 If this is zero, or a null string, it means resume the scan
142 by advancing to the next ARGV-element. */
144 static char *nextchar;
146 /* Callers store zero here to inhibit the error message
147 for unrecognized options. */
151 /* Set to an option character which was unrecognized.
152 This must be initialized on some systems to avoid linking in the
153 system's own getopt implementation. */
157 /* Describe how to deal with options that follow non-option ARGV-elements.
159 If the caller did not specify anything,
160 the default is REQUIRE_ORDER if the environment variable
161 POSIXLY_CORRECT is defined, PERMUTE otherwise.
163 REQUIRE_ORDER means don't recognize them as options;
164 stop option processing when the first non-option is seen.
165 This is what Unix does.
166 This mode of operation is selected by either setting the environment
167 variable POSIXLY_CORRECT, or using `+' as the first character
168 of the list of option characters.
170 PERMUTE is the default. We permute the contents of ARGV as we scan,
171 so that eventually all the non-options are at the end. This allows options
172 to be given in any order, even with programs that were not written to
175 RETURN_IN_ORDER is an option available to programs that were written
176 to expect options and other ARGV-elements in any order and that care about
177 the ordering of the two. We describe each non-option ARGV-element
178 as if it were the argument of an option with character code 1.
179 Using `-' as the first character of the list of option characters
180 selects this mode of operation.
182 The special argument `--' forces an end of option-scanning regardless
183 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
184 `--' can cause `getopt' to return -1 with `optind' != ARGC. */
188 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
191 /* Value of POSIXLY_CORRECT environment variable. */
192 static char *posixly_correct;
194 #ifndef __GNU_LIBRARY__
196 /* Avoid depending on library functions or files
197 whose names are inconsistent. */
200 extern char *getenv ();
203 #endif /* not __GNU_LIBRARY__ */
205 /* Handle permutation of arguments. */
207 /* Describe the part of ARGV that contains non-options that have
208 been skipped. `first_nonopt' is the index in ARGV of the first of them;
209 `last_nonopt' is the index after the last of them. */
211 static int first_nonopt;
212 static int last_nonopt;
215 /* Stored original parameters.
216 XXX This is no good solution. We should rather copy the args so
217 that we can compare them later. But we must not use malloc(3). */
218 extern int __libc_argc;
219 extern char **__libc_argv;
221 /* Bash 2.0 gives us an environment variable containing flags
222 indicating ARGV elements that should not be considered arguments. */
224 # ifdef USE_NONOPTION_FLAGS
225 /* Defined in getopt_init.c */
226 extern char *__getopt_nonoption_flags;
228 static int nonoption_flags_max_len;
229 static int nonoption_flags_len;
232 # ifdef USE_NONOPTION_FLAGS
233 # define SWAP_FLAGS(ch1, ch2) \
234 if (nonoption_flags_len > 0) \
236 char __tmp = __getopt_nonoption_flags[ch1]; \
237 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
238 __getopt_nonoption_flags[ch2] = __tmp; \
241 # define SWAP_FLAGS(ch1, ch2)
244 # define SWAP_FLAGS(ch1, ch2)
247 /* Exchange two adjacent subsequences of ARGV.
248 One subsequence is elements [first_nonopt,last_nonopt)
249 which contains all the non-options that have been skipped so far.
250 The other is elements [last_nonopt,optind), which contains all
251 the options processed since those non-options were skipped.
253 `first_nonopt' and `last_nonopt' are relocated so that they describe
254 the new indices of the non-options in ARGV after they are moved. */
257 exchange (char **argv)
259 int bottom = first_nonopt;
260 int middle = last_nonopt;
264 /* Exchange the shorter segment with the far end of the longer segment.
265 That puts the shorter segment into the right place.
266 It leaves the longer segment in the right place overall,
267 but it consists of two parts that need to be swapped next. */
269 #if defined _LIBC && defined USE_NONOPTION_FLAGS
270 /* First make sure the handling of the `__getopt_nonoption_flags'
271 string can work normally. Our top argument must be in the range
273 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
275 /* We must extend the array. The user plays games with us and
276 presents new arguments. */
277 char *new_str = malloc (top + 1);
279 nonoption_flags_len = nonoption_flags_max_len = 0;
282 memset (__mempcpy (new_str, __getopt_nonoption_flags,
283 nonoption_flags_max_len),
284 '\0', top + 1 - nonoption_flags_max_len);
285 nonoption_flags_max_len = top + 1;
286 __getopt_nonoption_flags = new_str;
291 while (top > middle && middle > bottom)
293 if (top - middle > middle - bottom)
295 /* Bottom segment is the short one. */
296 int len = middle - bottom;
299 /* Swap it with the top part of the top segment. */
300 for (i = 0; i < len; i++)
302 tem = argv[bottom + i];
303 argv[bottom + i] = argv[top - (middle - bottom) + i];
304 argv[top - (middle - bottom) + i] = tem;
305 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
307 /* Exclude the moved bottom segment from further swapping. */
312 /* Top segment is the short one. */
313 int len = top - middle;
316 /* Swap it with the bottom part of the bottom segment. */
317 for (i = 0; i < len; i++)
319 tem = argv[bottom + i];
320 argv[bottom + i] = argv[middle + i];
321 argv[middle + i] = tem;
322 SWAP_FLAGS (bottom + i, middle + i);
324 /* Exclude the moved top segment from further swapping. */
329 /* Update records for the slots the non-options now occupy. */
331 first_nonopt += (optind - last_nonopt);
332 last_nonopt = optind;
335 /* Initialize the internal data when the first call is made. */
338 _getopt_initialize (int argc, char *const *argv, const char *optstring)
340 /* Start processing options with ARGV-element 1 (since ARGV-element 0
341 is the program name); the sequence of previously skipped
342 non-option ARGV-elements is empty. */
344 first_nonopt = last_nonopt = optind;
348 posixly_correct = getenv ("POSIXLY_CORRECT");
350 /* Determine how to handle the ordering of options and nonoptions. */
352 if (optstring[0] == '-')
354 ordering = RETURN_IN_ORDER;
357 else if (optstring[0] == '+')
359 ordering = REQUIRE_ORDER;
362 else if (posixly_correct != NULL)
363 ordering = REQUIRE_ORDER;
367 #if defined _LIBC && defined USE_NONOPTION_FLAGS
368 if (posixly_correct == NULL
369 && argc == __libc_argc && argv == __libc_argv)
371 if (nonoption_flags_max_len == 0)
373 if (__getopt_nonoption_flags == NULL
374 || __getopt_nonoption_flags[0] == '\0')
375 nonoption_flags_max_len = -1;
378 const char *orig_str = __getopt_nonoption_flags;
379 int len = nonoption_flags_max_len = strlen (orig_str);
380 if (nonoption_flags_max_len < argc)
381 nonoption_flags_max_len = argc;
382 __getopt_nonoption_flags =
383 (char *) malloc (nonoption_flags_max_len);
384 if (__getopt_nonoption_flags == NULL)
385 nonoption_flags_max_len = -1;
387 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
388 '\0', nonoption_flags_max_len - len);
391 nonoption_flags_len = nonoption_flags_max_len;
394 nonoption_flags_len = 0;
400 /* Scan elements of ARGV (whose length is ARGC) for option characters
403 If an element of ARGV starts with '-', and is not exactly "-" or "--",
404 then it is an option element. The characters of this element
405 (aside from the initial '-') are option characters. If `getopt'
406 is called repeatedly, it returns successively each of the option characters
407 from each of the option elements.
409 If `getopt' finds another option character, it returns that character,
410 updating `optind' and `nextchar' so that the next call to `getopt' can
411 resume the scan with the following option character or ARGV-element.
413 If there are no more option characters, `getopt' returns -1.
414 Then `optind' is the index in ARGV of the first ARGV-element
415 that is not an option. (The ARGV-elements have been permuted
416 so that those that are not options now come last.)
418 OPTSTRING is a string containing the legitimate option characters.
419 If an option character is seen that is not listed in OPTSTRING,
420 return '?' after printing an error message. If you set `opterr' to
421 zero, the error message is suppressed but we still return '?'.
423 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
424 so the following text in the same ARGV-element, or the text of the following
425 ARGV-element, is returned in `optarg'. Two colons mean an option that
426 wants an optional arg; if there is text in the current ARGV-element,
427 it is returned in `optarg', otherwise `optarg' is set to zero.
429 If OPTSTRING starts with `-' or `+', it requests different methods of
430 handling the non-option ARGV-elements.
431 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
433 Long-named options begin with `--' instead of `-'.
434 Their names may be abbreviated as long as the abbreviation is unique
435 or is an exact match for some defined option. If they have an
436 argument, it follows the option name in the same ARGV-element, separated
437 from the option name by a `=', or else the in next ARGV-element.
438 When `getopt' finds a long-named option, it returns 0 if that option's
439 `flag' field is nonzero, the value of the option's `val' field
440 if the `flag' field is zero.
442 The elements of ARGV aren't really const, because we permute them.
443 But we pretend they're const in the prototype to be compatible
446 LONGOPTS is a vector of `struct option' terminated by an
447 element containing a name which is zero.
449 LONGIND returns the index in LONGOPT of the long-named option found.
450 It is only valid when a long-named option has been found by the most
453 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
454 long-named options. */
457 _getopt_internal (int argc, char *const *argv,
458 const char *optstring, const struct option *longopts,
459 int *longind, int long_only)
461 int print_errors = opterr;
462 if (optstring[0] == ':')
470 if (optind == 0 || !__getopt_initialized)
473 optind = 1; /* Don't scan ARGV[0], the program name. */
474 optstring = _getopt_initialize (argc, argv, optstring);
475 __getopt_initialized = 1;
478 /* Test whether ARGV[optind] points to a non-option argument.
479 Either it does not have option syntax, or there is an environment flag
480 from the shell indicating it is not an option. The later information
481 is only used when the used in the GNU libc. */
482 #if defined _LIBC && defined USE_NONOPTION_FLAGS
483 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
484 || (optind < nonoption_flags_len \
485 && __getopt_nonoption_flags[optind] == '1'))
487 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
490 if (nextchar == NULL || *nextchar == '\0')
492 /* Advance to the next ARGV-element. */
494 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
495 moved back by the user (who may also have changed the arguments). */
496 if (last_nonopt > optind)
497 last_nonopt = optind;
498 if (first_nonopt > optind)
499 first_nonopt = optind;
501 if (ordering == PERMUTE)
503 /* If we have just processed some options following some non-options,
504 exchange them so that the options come first. */
506 if (first_nonopt != last_nonopt && last_nonopt != optind)
507 exchange ((char **) argv);
508 else if (last_nonopt != optind)
509 first_nonopt = optind;
511 /* Skip any additional non-options
512 and extend the range of non-options previously skipped. */
514 while (optind < argc && NONOPTION_P)
516 last_nonopt = optind;
519 /* The special ARGV-element `--' means premature end of options.
520 Skip it like a null option,
521 then exchange with previous non-options as if it were an option,
522 then skip everything else like a non-option. */
524 if (optind != argc && !strcmp (argv[optind], "--"))
528 if (first_nonopt != last_nonopt && last_nonopt != optind)
529 exchange ((char **) argv);
530 else if (first_nonopt == last_nonopt)
531 first_nonopt = optind;
537 /* If we have done all the ARGV-elements, stop the scan
538 and back over any non-options that we skipped and permuted. */
542 /* Set the next-arg-index to point at the non-options
543 that we previously skipped, so the caller will digest them. */
544 if (first_nonopt != last_nonopt)
545 optind = first_nonopt;
549 /* If we have come to a non-option and did not permute it,
550 either stop the scan or describe it to the caller and pass it by. */
554 if (ordering == REQUIRE_ORDER)
556 optarg = argv[optind++];
560 /* We have found another option-ARGV-element.
561 Skip the initial punctuation. */
563 nextchar = (argv[optind] + 1
564 + (longopts != NULL && argv[optind][1] == '-'));
567 /* Decode the current option-ARGV-element. */
569 /* Check whether the ARGV-element is a long option.
571 If long_only and the ARGV-element has the form "-f", where f is
572 a valid short option, don't consider it an abbreviated form of
573 a long option that starts with f. Otherwise there would be no
574 way to give the -f short option.
576 On the other hand, if there's a long option "fubar" and
577 the ARGV-element is "-fu", do consider that an abbreviation of
578 the long option, just like "--fu", and not "-f" with arg "u".
580 This distinction seems to be the most useful approach. */
583 && (argv[optind][1] == '-'
585 && (argv[optind][2] || !strchr (optstring, argv[optind][1])))))
588 const struct option *p;
589 const struct option *pfound = NULL;
595 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
598 /* Test all long options for either exact match
599 or abbreviated matches. */
600 for (p = longopts, option_index = 0; p->name; p++, option_index++)
601 if (!strncmp (p->name, nextchar, nameend - nextchar))
603 if ((unsigned int) (nameend - nextchar)
604 == (unsigned int) strlen (p->name))
606 /* Exact match found. */
608 indfound = option_index;
612 else if (pfound == NULL)
614 /* First nonexact match found. */
616 indfound = option_index;
619 || pfound->has_arg != p->has_arg
620 || pfound->flag != p->flag
621 || pfound->val != p->val)
622 /* Second or later nonexact match found. */
630 #if defined _LIBC && defined USE_IN_LIBIO
633 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
634 argv[0], argv[optind]) >= 0)
637 if (_IO_fwide (stderr, 0) > 0)
638 __fwprintf (stderr, L"%s", buf);
645 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
646 argv[0], argv[optind]);
649 nextchar += strlen (nextchar);
657 option_index = indfound;
661 /* Don't test has_arg with >, because some C compilers don't
662 allow it to be used on enums. */
664 optarg = nameend + 1;
669 #if defined _LIBC && defined USE_IN_LIBIO
674 if (argv[optind - 1][1] == '-')
677 #if defined _LIBC && defined USE_IN_LIBIO
678 n = __asprintf (&buf, _("\
679 %s: option `--%s' doesn't allow an argument\n"),
680 argv[0], pfound->name);
682 fprintf (stderr, _("\
683 %s: option `--%s' doesn't allow an argument\n"),
684 argv[0], pfound->name);
689 /* +option or -option */
690 #if defined _LIBC && defined USE_IN_LIBIO
691 n = __asprintf (&buf, _("\
692 %s: option `%c%s' doesn't allow an argument\n"),
693 argv[0], argv[optind - 1][0],
696 fprintf (stderr, _("\
697 %s: option `%c%s' doesn't allow an argument\n"),
698 argv[0], argv[optind - 1][0], pfound->name);
702 #if defined _LIBC && defined USE_IN_LIBIO
705 if (_IO_fwide (stderr, 0) > 0)
706 __fwprintf (stderr, L"%s", buf);
715 nextchar += strlen (nextchar);
717 optopt = pfound->val;
721 else if (pfound->has_arg == 1)
724 optarg = argv[optind++];
729 #if defined _LIBC && defined USE_IN_LIBIO
732 if (__asprintf (&buf, _("\
733 %s: option `%s' requires an argument\n"),
734 argv[0], argv[optind - 1]) >= 0)
736 if (_IO_fwide (stderr, 0) > 0)
737 __fwprintf (stderr, L"%s", buf);
745 _("%s: option `%s' requires an argument\n"),
746 argv[0], argv[optind - 1]);
749 nextchar += strlen (nextchar);
750 optopt = pfound->val;
751 return optstring[0] == ':' ? ':' : '?';
754 nextchar += strlen (nextchar);
756 *longind = option_index;
759 *(pfound->flag) = pfound->val;
765 /* Can't find it as a long option. If this is not getopt_long_only,
766 or the option starts with '--' or is not a valid short
767 option, then it's an error.
768 Otherwise interpret it as a short option. */
769 if (!long_only || argv[optind][1] == '-'
770 || strchr (optstring, *nextchar) == NULL)
774 #if defined _LIBC && defined USE_IN_LIBIO
779 if (argv[optind][1] == '-')
782 #if defined _LIBC && defined USE_IN_LIBIO
783 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
786 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
792 /* +option or -option */
793 #if defined _LIBC && defined USE_IN_LIBIO
794 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
795 argv[0], argv[optind][0], nextchar);
797 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
798 argv[0], argv[optind][0], nextchar);
802 #if defined _LIBC && defined USE_IN_LIBIO
805 if (_IO_fwide (stderr, 0) > 0)
806 __fwprintf (stderr, L"%s", buf);
814 nextchar = (char *) "";
821 /* Look at and handle the next short option-character. */
824 char c = *nextchar++;
825 char *temp = strchr (optstring, c);
827 /* Increment `optind' when we start to process its last character. */
828 if (*nextchar == '\0')
831 if (temp == NULL || c == ':')
835 #if defined _LIBC && defined USE_IN_LIBIO
842 /* 1003.2 specifies the format of this message. */
843 #if defined _LIBC && defined USE_IN_LIBIO
844 n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
847 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
852 #if defined _LIBC && defined USE_IN_LIBIO
853 n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
856 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
860 #if defined _LIBC && defined USE_IN_LIBIO
863 if (_IO_fwide (stderr, 0) > 0)
864 __fwprintf (stderr, L"%s", buf);
875 /* Convenience. Treat POSIX -W foo same as long option --foo */
876 if (temp[0] == 'W' && temp[1] == ';')
879 const struct option *p;
880 const struct option *pfound = NULL;
886 /* This is an option that requires an argument. */
887 if (*nextchar != '\0')
890 /* If we end this ARGV-element by taking the rest as an arg,
891 we must advance to the next element now. */
894 else if (optind == argc)
898 /* 1003.2 specifies the format of this message. */
899 #if defined _LIBC && defined USE_IN_LIBIO
902 if (__asprintf (&buf,
903 _("%s: option requires an argument -- %c\n"),
906 if (_IO_fwide (stderr, 0) > 0)
907 __fwprintf (stderr, L"%s", buf);
914 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
919 if (optstring[0] == ':')
926 /* We already incremented `optind' once;
927 increment it again when taking next ARGV-elt as argument. */
928 optarg = argv[optind++];
930 /* optarg is now the argument, see if it's in the
931 table of longopts. */
933 for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
936 /* Test all long options for either exact match
937 or abbreviated matches. */
938 for (p = longopts, option_index = 0; p->name; p++, option_index++)
939 if (!strncmp (p->name, nextchar, nameend - nextchar))
941 if ((unsigned int) (nameend - nextchar) == strlen (p->name))
943 /* Exact match found. */
945 indfound = option_index;
949 else if (pfound == NULL)
951 /* First nonexact match found. */
953 indfound = option_index;
956 /* Second or later nonexact match found. */
963 #if defined _LIBC && defined USE_IN_LIBIO
966 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
967 argv[0], argv[optind]) >= 0)
969 if (_IO_fwide (stderr, 0) > 0)
970 __fwprintf (stderr, L"%s", buf);
977 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
978 argv[0], argv[optind]);
981 nextchar += strlen (nextchar);
987 option_index = indfound;
990 /* Don't test has_arg with >, because some C compilers don't
991 allow it to be used on enums. */
993 optarg = nameend + 1;
998 #if defined _LIBC && defined USE_IN_LIBIO
1001 if (__asprintf (&buf, _("\
1002 %s: option `-W %s' doesn't allow an argument\n"),
1003 argv[0], pfound->name) >= 0)
1005 if (_IO_fwide (stderr, 0) > 0)
1006 __fwprintf (stderr, L"%s", buf);
1008 fputs (buf, stderr);
1013 fprintf (stderr, _("\
1014 %s: option `-W %s' doesn't allow an argument\n"),
1015 argv[0], pfound->name);
1019 nextchar += strlen (nextchar);
1023 else if (pfound->has_arg == 1)
1026 optarg = argv[optind++];
1031 #if defined _LIBC && defined USE_IN_LIBIO
1034 if (__asprintf (&buf, _("\
1035 %s: option `%s' requires an argument\n"),
1036 argv[0], argv[optind - 1]) >= 0)
1038 if (_IO_fwide (stderr, 0) > 0)
1039 __fwprintf (stderr, L"%s", buf);
1041 fputs (buf, stderr);
1047 _("%s: option `%s' requires an argument\n"),
1048 argv[0], argv[optind - 1]);
1051 nextchar += strlen (nextchar);
1052 return optstring[0] == ':' ? ':' : '?';
1055 nextchar += strlen (nextchar);
1056 if (longind != NULL)
1057 *longind = option_index;
1060 *(pfound->flag) = pfound->val;
1066 return 'W'; /* Let the application handle it. */
1072 /* This is an option that accepts an argument optionally. */
1073 if (*nextchar != '\0')
1084 /* This is an option that requires an argument. */
1085 if (*nextchar != '\0')
1088 /* If we end this ARGV-element by taking the rest as an arg,
1089 we must advance to the next element now. */
1092 else if (optind == argc)
1096 /* 1003.2 specifies the format of this message. */
1097 #if defined _LIBC && defined USE_IN_LIBIO
1100 if (__asprintf (&buf, _("\
1101 %s: option requires an argument -- %c\n"),
1104 if (_IO_fwide (stderr, 0) > 0)
1105 __fwprintf (stderr, L"%s", buf);
1107 fputs (buf, stderr);
1113 _("%s: option requires an argument -- %c\n"),
1118 if (optstring[0] == ':')
1124 /* We already incremented `optind' once;
1125 increment it again when taking next ARGV-elt as argument. */
1126 optarg = argv[optind++];
1135 getopt (int argc, char *const *argv, const char *optstring)
1137 return _getopt_internal (argc, argv, optstring,
1138 (const struct option *) 0,
1143 #endif /* Not ELIDE_CODE. */
1147 /* Compile with -DTEST to make an executable for use in testing
1148 the above definition of `getopt'. */
1151 main (int argc, char **argv)
1154 int digit_optind = 0;
1158 int this_option_optind = optind ? optind : 1;
1160 c = getopt (argc, argv, "abc:d:0123456789");
1176 if (digit_optind != 0 && digit_optind != this_option_optind)
1177 printf ("digits occur in two different argv-elements.\n");
1178 digit_optind = this_option_optind;
1179 printf ("option %c\n", c);
1183 printf ("option a\n");
1187 printf ("option b\n");
1191 printf ("option c with value `%s'\n", optarg);
1198 printf ("?? getopt returned character code 0%o ??\n", c);
1204 printf ("non-option ARGV-elements: ");
1205 while (optind < argc)
1206 printf ("%s ", argv[optind++]);