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
5 Copyright (C) 1987-2014 Free Software Foundation, Inc.
6 This file is part of the GNU C Library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
22 Ditto for AIX 3.2 and <stdlib.h>. */
33 /* This needs to come after some library #include
34 to get __GNU_LIBRARY__ defined. */
35 #ifdef __GNU_LIBRARY__
36 /* Don't include stdlib.h for non-GNU C libraries because some of them
37 contain conflicting prototypes for getopt. */
40 #endif /* GNU C library. */
52 # define _(msgid) gettext (msgid)
55 #if defined _LIBC && defined USE_IN_LIBIO
59 #ifndef attribute_hidden
60 # define attribute_hidden
63 /* Unlike standard Unix `getopt', functions like `getopt_long'
64 let the user intersperse the options with the other arguments.
66 As `getopt_long' works, it permutes the elements of ARGV so that,
67 when it is done, all the options precede everything else. Thus
68 all application programs are extended to handle flexible argument order.
70 Using `getopt' or setting the environment variable POSIXLY_CORRECT
72 Then the application's behavior is completely standard.
74 GNU application programs can use a third alternative mode in which
75 they can distinguish the relative order of options and other arguments. */
78 #include "getopt_int.h"
80 /* For communication from `getopt' to the caller.
81 When `getopt' finds an option that takes an argument,
82 the argument value is returned here.
83 Also, when `ordering' is RETURN_IN_ORDER,
84 each non-option ARGV-element is returned here. */
88 /* Index in ARGV of the next element to be scanned.
89 This is used for communication to and from the caller
90 and for communication between successive calls to `getopt'.
92 On entry to `getopt', zero means this is the first call; initialize.
94 When `getopt' returns -1, this is the index of the first of the
95 non-option elements that the caller should itself scan.
97 Otherwise, `optind' communicates from one call to the next
98 how much of ARGV has been scanned so far. */
100 /* 1003.2 says this must be 1 before any call. */
103 /* Callers store zero here to inhibit the error message
104 for unrecognized options. */
108 /* Set to an option character which was unrecognized.
109 This must be initialized on some systems to avoid linking in the
110 system's own getopt implementation. */
114 /* Keep a global copy of all internal members of getopt_data. */
116 static struct _getopt_data getopt_data;
119 #ifndef __GNU_LIBRARY__
121 /* Avoid depending on library functions or files
122 whose names are inconsistent. */
125 extern char *getenv ();
128 #endif /* not __GNU_LIBRARY__ */
131 /* Stored original parameters.
132 XXX This is no good solution. We should rather copy the args so
133 that we can compare them later. But we must not use malloc(3). */
134 extern int __libc_argc;
135 extern char **__libc_argv;
137 /* Bash 2.0 gives us an environment variable containing flags
138 indicating ARGV elements that should not be considered arguments. */
140 # ifdef USE_NONOPTION_FLAGS
141 /* Defined in getopt_init.c */
142 extern char *__getopt_nonoption_flags;
145 # ifdef USE_NONOPTION_FLAGS
146 # define SWAP_FLAGS(ch1, ch2) \
147 if (d->__nonoption_flags_len > 0) \
149 char __tmp = __getopt_nonoption_flags[ch1]; \
150 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
151 __getopt_nonoption_flags[ch2] = __tmp; \
154 # define SWAP_FLAGS(ch1, ch2)
157 # define SWAP_FLAGS(ch1, ch2)
160 /* Exchange two adjacent subsequences of ARGV.
161 One subsequence is elements [first_nonopt,last_nonopt)
162 which contains all the non-options that have been skipped so far.
163 The other is elements [last_nonopt,optind), which contains all
164 the options processed since those non-options were skipped.
166 `first_nonopt' and `last_nonopt' are relocated so that they describe
167 the new indices of the non-options in ARGV after they are moved. */
170 exchange (char **argv, struct _getopt_data *d)
172 int bottom = d->__first_nonopt;
173 int middle = d->__last_nonopt;
177 /* Exchange the shorter segment with the far end of the longer segment.
178 That puts the shorter segment into the right place.
179 It leaves the longer segment in the right place overall,
180 but it consists of two parts that need to be swapped next. */
182 #if defined _LIBC && defined USE_NONOPTION_FLAGS
183 /* First make sure the handling of the `__getopt_nonoption_flags'
184 string can work normally. Our top argument must be in the range
186 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
188 /* We must extend the array. The user plays games with us and
189 presents new arguments. */
190 char *new_str = malloc (top + 1);
192 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
195 memset (__mempcpy (new_str, __getopt_nonoption_flags,
196 d->__nonoption_flags_max_len),
197 '\0', top + 1 - d->__nonoption_flags_max_len);
198 d->__nonoption_flags_max_len = top + 1;
199 __getopt_nonoption_flags = new_str;
204 while (top > middle && middle > bottom)
206 if (top - middle > middle - bottom)
208 /* Bottom segment is the short one. */
209 int len = middle - bottom;
212 /* Swap it with the top part of the top segment. */
213 for (i = 0; i < len; i++)
215 tem = argv[bottom + i];
216 argv[bottom + i] = argv[top - (middle - bottom) + i];
217 argv[top - (middle - bottom) + i] = tem;
218 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
220 /* Exclude the moved bottom segment from further swapping. */
225 /* Top segment is the short one. */
226 int len = top - middle;
229 /* Swap it with the bottom part of the bottom segment. */
230 for (i = 0; i < len; i++)
232 tem = argv[bottom + i];
233 argv[bottom + i] = argv[middle + i];
234 argv[middle + i] = tem;
235 SWAP_FLAGS (bottom + i, middle + i);
237 /* Exclude the moved top segment from further swapping. */
242 /* Update records for the slots the non-options now occupy. */
244 d->__first_nonopt += (d->optind - d->__last_nonopt);
245 d->__last_nonopt = d->optind;
248 /* Initialize the internal data when the first call is made. */
251 _getopt_initialize (int argc, char **argv, const char *optstring,
252 int posixly_correct, struct _getopt_data *d)
254 /* Start processing options with ARGV-element 1 (since ARGV-element 0
255 is the program name); the sequence of previously skipped
256 non-option ARGV-elements is empty. */
258 d->__first_nonopt = d->__last_nonopt = d->optind;
260 d->__nextchar = NULL;
262 d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
264 /* Determine how to handle the ordering of options and nonoptions. */
266 if (optstring[0] == '-')
268 d->__ordering = RETURN_IN_ORDER;
271 else if (optstring[0] == '+')
273 d->__ordering = REQUIRE_ORDER;
276 else if (d->__posixly_correct)
277 d->__ordering = REQUIRE_ORDER;
279 d->__ordering = PERMUTE;
281 #if defined _LIBC && defined USE_NONOPTION_FLAGS
282 if (!d->__posixly_correct
283 && argc == __libc_argc && argv == __libc_argv)
285 if (d->__nonoption_flags_max_len == 0)
287 if (__getopt_nonoption_flags == NULL
288 || __getopt_nonoption_flags[0] == '\0')
289 d->__nonoption_flags_max_len = -1;
292 const char *orig_str = __getopt_nonoption_flags;
293 int len = d->__nonoption_flags_max_len = strlen (orig_str);
294 if (d->__nonoption_flags_max_len < argc)
295 d->__nonoption_flags_max_len = argc;
296 __getopt_nonoption_flags =
297 (char *) malloc (d->__nonoption_flags_max_len);
298 if (__getopt_nonoption_flags == NULL)
299 d->__nonoption_flags_max_len = -1;
301 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
302 '\0', d->__nonoption_flags_max_len - len);
305 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
308 d->__nonoption_flags_len = 0;
314 /* Scan elements of ARGV (whose length is ARGC) for option characters
317 If an element of ARGV starts with '-', and is not exactly "-" or "--",
318 then it is an option element. The characters of this element
319 (aside from the initial '-') are option characters. If `getopt'
320 is called repeatedly, it returns successively each of the option characters
321 from each of the option elements.
323 If `getopt' finds another option character, it returns that character,
324 updating `optind' and `nextchar' so that the next call to `getopt' can
325 resume the scan with the following option character or ARGV-element.
327 If there are no more option characters, `getopt' returns -1.
328 Then `optind' is the index in ARGV of the first ARGV-element
329 that is not an option. (The ARGV-elements have been permuted
330 so that those that are not options now come last.)
332 OPTSTRING is a string containing the legitimate option characters.
333 If an option character is seen that is not listed in OPTSTRING,
334 return '?' after printing an error message. If you set `opterr' to
335 zero, the error message is suppressed but we still return '?'.
337 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
338 so the following text in the same ARGV-element, or the text of the following
339 ARGV-element, is returned in `optarg'. Two colons mean an option that
340 wants an optional arg; if there is text in the current ARGV-element,
341 it is returned in `optarg', otherwise `optarg' is set to zero.
343 If OPTSTRING starts with `-' or `+', it requests different methods of
344 handling the non-option ARGV-elements.
345 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
347 Long-named options begin with `--' instead of `-'.
348 Their names may be abbreviated as long as the abbreviation is unique
349 or is an exact match for some defined option. If they have an
350 argument, it follows the option name in the same ARGV-element, separated
351 from the option name by a `=', or else the in next ARGV-element.
352 When `getopt' finds a long-named option, it returns 0 if that option's
353 `flag' field is nonzero, the value of the option's `val' field
354 if the `flag' field is zero.
356 LONGOPTS is a vector of `struct option' terminated by an
357 element containing a name which is zero.
359 LONGIND returns the index in LONGOPT of the long-named option found.
360 It is only valid when a long-named option has been found by the most
363 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
366 If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
367 environment variable were set. */
370 _getopt_internal_r (int argc, char **argv, const char *optstring,
371 const struct option *longopts, int *longind,
372 int long_only, int posixly_correct, struct _getopt_data *d)
374 int print_errors = d->opterr;
375 if (optstring[0] == ':')
383 if (d->optind == 0 || !d->__initialized)
386 d->optind = 1; /* Don't scan ARGV[0], the program name. */
387 optstring = _getopt_initialize (argc, argv, optstring,
389 d->__initialized = 1;
392 /* Test whether ARGV[optind] points to a non-option argument.
393 Either it does not have option syntax, or there is an environment flag
394 from the shell indicating it is not an option. The later information
395 is only used when the used in the GNU libc. */
396 #if defined _LIBC && defined USE_NONOPTION_FLAGS
397 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
398 || (d->optind < d->__nonoption_flags_len \
399 && __getopt_nonoption_flags[d->optind] == '1'))
401 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
404 if (d->__nextchar == NULL || *d->__nextchar == '\0')
406 /* Advance to the next ARGV-element. */
408 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
409 moved back by the user (who may also have changed the arguments). */
410 if (d->__last_nonopt > d->optind)
411 d->__last_nonopt = d->optind;
412 if (d->__first_nonopt > d->optind)
413 d->__first_nonopt = d->optind;
415 if (d->__ordering == PERMUTE)
417 /* If we have just processed some options following some non-options,
418 exchange them so that the options come first. */
420 if (d->__first_nonopt != d->__last_nonopt
421 && d->__last_nonopt != d->optind)
422 exchange ((char **) argv, d);
423 else if (d->__last_nonopt != d->optind)
424 d->__first_nonopt = d->optind;
426 /* Skip any additional non-options
427 and extend the range of non-options previously skipped. */
429 while (d->optind < argc && NONOPTION_P)
431 d->__last_nonopt = d->optind;
434 /* The special ARGV-element `--' means premature end of options.
435 Skip it like a null option,
436 then exchange with previous non-options as if it were an option,
437 then skip everything else like a non-option. */
439 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
443 if (d->__first_nonopt != d->__last_nonopt
444 && d->__last_nonopt != d->optind)
445 exchange ((char **) argv, d);
446 else if (d->__first_nonopt == d->__last_nonopt)
447 d->__first_nonopt = d->optind;
448 d->__last_nonopt = argc;
453 /* If we have done all the ARGV-elements, stop the scan
454 and back over any non-options that we skipped and permuted. */
456 if (d->optind == argc)
458 /* Set the next-arg-index to point at the non-options
459 that we previously skipped, so the caller will digest them. */
460 if (d->__first_nonopt != d->__last_nonopt)
461 d->optind = d->__first_nonopt;
465 /* If we have come to a non-option and did not permute it,
466 either stop the scan or describe it to the caller and pass it by. */
470 if (d->__ordering == REQUIRE_ORDER)
472 d->optarg = argv[d->optind++];
476 /* We have found another option-ARGV-element.
477 Skip the initial punctuation. */
479 d->__nextchar = (argv[d->optind] + 1
480 + (longopts != NULL && argv[d->optind][1] == '-'));
483 /* Decode the current option-ARGV-element. */
485 /* Check whether the ARGV-element is a long option.
487 If long_only and the ARGV-element has the form "-f", where f is
488 a valid short option, don't consider it an abbreviated form of
489 a long option that starts with f. Otherwise there would be no
490 way to give the -f short option.
492 On the other hand, if there's a long option "fubar" and
493 the ARGV-element is "-fu", do consider that an abbreviation of
494 the long option, just like "--fu", and not "-f" with arg "u".
496 This distinction seems to be the most useful approach. */
499 && (argv[d->optind][1] == '-'
500 || (long_only && (argv[d->optind][2]
501 || !strchr (optstring, argv[d->optind][1])))))
504 const struct option *p;
505 const struct option *pfound = NULL;
511 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
514 /* Test all long options for either exact match
515 or abbreviated matches. */
516 for (p = longopts, option_index = 0; p->name; p++, option_index++)
517 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
519 if ((unsigned int) (nameend - d->__nextchar)
520 == (unsigned int) strlen (p->name))
522 /* Exact match found. */
524 indfound = option_index;
528 else if (pfound == NULL)
530 /* First nonexact match found. */
532 indfound = option_index;
535 || pfound->has_arg != p->has_arg
536 || pfound->flag != p->flag
537 || pfound->val != p->val)
538 /* Second or later nonexact match found. */
546 #if defined _LIBC && defined USE_IN_LIBIO
549 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
550 argv[0], argv[d->optind]) >= 0)
552 _IO_flockfile (stderr);
554 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
555 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
557 if (_IO_fwide (stderr, 0) > 0)
558 __fwprintf (stderr, L"%s", buf);
562 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
563 _IO_funlockfile (stderr);
568 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
569 argv[0], argv[d->optind]);
572 d->__nextchar += strlen (d->__nextchar);
580 option_index = indfound;
584 /* Don't test has_arg with >, because some C compilers don't
585 allow it to be used on enums. */
587 d->optarg = nameend + 1;
592 #if defined _LIBC && defined USE_IN_LIBIO
597 if (argv[d->optind - 1][1] == '-')
600 #if defined _LIBC && defined USE_IN_LIBIO
601 n = __asprintf (&buf, _("\
602 %s: option `--%s' doesn't allow an argument\n"),
603 argv[0], pfound->name);
605 fprintf (stderr, _("\
606 %s: option `--%s' doesn't allow an argument\n"),
607 argv[0], pfound->name);
612 /* +option or -option */
613 #if defined _LIBC && defined USE_IN_LIBIO
614 n = __asprintf (&buf, _("\
615 %s: option `%c%s' doesn't allow an argument\n"),
616 argv[0], argv[d->optind - 1][0],
619 fprintf (stderr, _("\
620 %s: option `%c%s' doesn't allow an argument\n"),
621 argv[0], argv[d->optind - 1][0],
626 #if defined _LIBC && defined USE_IN_LIBIO
629 _IO_flockfile (stderr);
631 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
632 ((_IO_FILE *) stderr)->_flags2
633 |= _IO_FLAGS2_NOTCANCEL;
635 if (_IO_fwide (stderr, 0) > 0)
636 __fwprintf (stderr, L"%s", buf);
640 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
641 _IO_funlockfile (stderr);
648 d->__nextchar += strlen (d->__nextchar);
650 d->optopt = pfound->val;
654 else if (pfound->has_arg == 1)
656 if (d->optind < argc)
657 d->optarg = argv[d->optind++];
662 #if defined _LIBC && defined USE_IN_LIBIO
665 if (__asprintf (&buf, _("\
666 %s: option `%s' requires an argument\n"),
667 argv[0], argv[d->optind - 1]) >= 0)
669 _IO_flockfile (stderr);
671 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
672 ((_IO_FILE *) stderr)->_flags2
673 |= _IO_FLAGS2_NOTCANCEL;
675 if (_IO_fwide (stderr, 0) > 0)
676 __fwprintf (stderr, L"%s", buf);
680 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
681 _IO_funlockfile (stderr);
687 _("%s: option `%s' requires an argument\n"),
688 argv[0], argv[d->optind - 1]);
691 d->__nextchar += strlen (d->__nextchar);
692 d->optopt = pfound->val;
693 return optstring[0] == ':' ? ':' : '?';
696 d->__nextchar += strlen (d->__nextchar);
698 *longind = option_index;
701 *(pfound->flag) = pfound->val;
707 /* Can't find it as a long option. If this is not getopt_long_only,
708 or the option starts with '--' or is not a valid short
709 option, then it's an error.
710 Otherwise interpret it as a short option. */
711 if (!long_only || argv[d->optind][1] == '-'
712 || strchr (optstring, *d->__nextchar) == NULL)
716 #if defined _LIBC && defined USE_IN_LIBIO
721 if (argv[d->optind][1] == '-')
724 #if defined _LIBC && defined USE_IN_LIBIO
725 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
726 argv[0], d->__nextchar);
728 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
729 argv[0], d->__nextchar);
734 /* +option or -option */
735 #if defined _LIBC && defined USE_IN_LIBIO
736 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
737 argv[0], argv[d->optind][0], d->__nextchar);
739 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
740 argv[0], argv[d->optind][0], d->__nextchar);
744 #if defined _LIBC && defined USE_IN_LIBIO
747 _IO_flockfile (stderr);
749 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
750 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
752 if (_IO_fwide (stderr, 0) > 0)
753 __fwprintf (stderr, L"%s", buf);
757 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
758 _IO_funlockfile (stderr);
764 d->__nextchar = (char *) "";
771 /* Look at and handle the next short option-character. */
774 char c = *d->__nextchar++;
775 char *temp = strchr (optstring, c);
777 /* Increment `optind' when we start to process its last character. */
778 if (*d->__nextchar == '\0')
781 if (temp == NULL || c == ':')
785 #if defined _LIBC && defined USE_IN_LIBIO
790 if (d->__posixly_correct)
792 /* 1003.2 specifies the format of this message. */
793 #if defined _LIBC && defined USE_IN_LIBIO
794 n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
797 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
802 #if defined _LIBC && defined USE_IN_LIBIO
803 n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
806 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
810 #if defined _LIBC && defined USE_IN_LIBIO
813 _IO_flockfile (stderr);
815 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
816 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
818 if (_IO_fwide (stderr, 0) > 0)
819 __fwprintf (stderr, L"%s", buf);
823 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
824 _IO_funlockfile (stderr);
833 /* Convenience. Treat POSIX -W foo same as long option --foo */
834 if (temp[0] == 'W' && temp[1] == ';')
837 const struct option *p;
838 const struct option *pfound = NULL;
844 /* This is an option that requires an argument. */
845 if (*d->__nextchar != '\0')
847 d->optarg = d->__nextchar;
848 /* If we end this ARGV-element by taking the rest as an arg,
849 we must advance to the next element now. */
852 else if (d->optind == argc)
856 /* 1003.2 specifies the format of this message. */
857 #if defined _LIBC && defined USE_IN_LIBIO
860 if (__asprintf (&buf,
861 _("%s: option requires an argument -- %c\n"),
864 _IO_flockfile (stderr);
866 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
867 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
869 if (_IO_fwide (stderr, 0) > 0)
870 __fwprintf (stderr, L"%s", buf);
874 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
875 _IO_funlockfile (stderr);
880 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
885 if (optstring[0] == ':')
892 /* We already incremented `d->optind' once;
893 increment it again when taking next ARGV-elt as argument. */
894 d->optarg = argv[d->optind++];
896 /* optarg is now the argument, see if it's in the
897 table of longopts. */
899 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
903 /* Test all long options for either exact match
904 or abbreviated matches. */
905 for (p = longopts, option_index = 0; p->name; p++, option_index++)
906 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
908 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
910 /* Exact match found. */
912 indfound = option_index;
916 else if (pfound == NULL)
918 /* First nonexact match found. */
920 indfound = option_index;
923 /* Second or later nonexact match found. */
930 #if defined _LIBC && defined USE_IN_LIBIO
933 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
934 argv[0], argv[d->optind]) >= 0)
936 _IO_flockfile (stderr);
938 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
939 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
941 if (_IO_fwide (stderr, 0) > 0)
942 __fwprintf (stderr, L"%s", buf);
946 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
947 _IO_funlockfile (stderr);
952 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
953 argv[0], argv[d->optind]);
956 d->__nextchar += strlen (d->__nextchar);
962 option_index = indfound;
965 /* Don't test has_arg with >, because some C compilers don't
966 allow it to be used on enums. */
968 d->optarg = nameend + 1;
973 #if defined _LIBC && defined USE_IN_LIBIO
976 if (__asprintf (&buf, _("\
977 %s: option `-W %s' doesn't allow an argument\n"),
978 argv[0], pfound->name) >= 0)
980 _IO_flockfile (stderr);
982 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
983 ((_IO_FILE *) stderr)->_flags2
984 |= _IO_FLAGS2_NOTCANCEL;
986 if (_IO_fwide (stderr, 0) > 0)
987 __fwprintf (stderr, L"%s", buf);
991 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
992 _IO_funlockfile (stderr);
997 fprintf (stderr, _("\
998 %s: option `-W %s' doesn't allow an argument\n"),
999 argv[0], pfound->name);
1003 d->__nextchar += strlen (d->__nextchar);
1007 else if (pfound->has_arg == 1)
1009 if (d->optind < argc)
1010 d->optarg = argv[d->optind++];
1015 #if defined _LIBC && defined USE_IN_LIBIO
1018 if (__asprintf (&buf, _("\
1019 %s: option `%s' requires an argument\n"),
1020 argv[0], argv[d->optind - 1]) >= 0)
1022 _IO_flockfile (stderr);
1024 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1025 ((_IO_FILE *) stderr)->_flags2
1026 |= _IO_FLAGS2_NOTCANCEL;
1028 if (_IO_fwide (stderr, 0) > 0)
1029 __fwprintf (stderr, L"%s", buf);
1031 fputs (buf, stderr);
1033 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1034 _IO_funlockfile (stderr);
1040 _("%s: option `%s' requires an argument\n"),
1041 argv[0], argv[d->optind - 1]);
1044 d->__nextchar += strlen (d->__nextchar);
1045 return optstring[0] == ':' ? ':' : '?';
1048 d->__nextchar += strlen (d->__nextchar);
1049 if (longind != NULL)
1050 *longind = option_index;
1053 *(pfound->flag) = pfound->val;
1058 d->__nextchar = NULL;
1059 return 'W'; /* Let the application handle it. */
1065 /* This is an option that accepts an argument optionally. */
1066 if (*d->__nextchar != '\0')
1068 d->optarg = d->__nextchar;
1073 d->__nextchar = NULL;
1077 /* This is an option that requires an argument. */
1078 if (*d->__nextchar != '\0')
1080 d->optarg = d->__nextchar;
1081 /* If we end this ARGV-element by taking the rest as an arg,
1082 we must advance to the next element now. */
1085 else if (d->optind == argc)
1089 /* 1003.2 specifies the format of this message. */
1090 #if defined _LIBC && defined USE_IN_LIBIO
1093 if (__asprintf (&buf, _("\
1094 %s: option requires an argument -- %c\n"),
1097 _IO_flockfile (stderr);
1099 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1100 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1102 if (_IO_fwide (stderr, 0) > 0)
1103 __fwprintf (stderr, L"%s", buf);
1105 fputs (buf, stderr);
1107 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1108 _IO_funlockfile (stderr);
1114 _("%s: option requires an argument -- %c\n"),
1119 if (optstring[0] == ':')
1125 /* We already incremented `optind' once;
1126 increment it again when taking next ARGV-elt as argument. */
1127 d->optarg = argv[d->optind++];
1128 d->__nextchar = NULL;
1136 _getopt_internal (int argc, char **argv, const char *optstring,
1137 const struct option *longopts, int *longind,
1138 int long_only, int posixly_correct)
1142 getopt_data.optind = optind;
1143 getopt_data.opterr = opterr;
1145 result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
1146 long_only, posixly_correct, &getopt_data);
1148 optind = getopt_data.optind;
1149 optarg = getopt_data.optarg;
1150 optopt = getopt_data.optopt;
1155 /* glibc gets a LSB-compliant getopt.
1156 Standalone applications get a POSIX-compliant getopt. */
1158 enum { POSIXLY_CORRECT = 0 };
1160 enum { POSIXLY_CORRECT = 1 };
1164 getopt (int argc, char *const *argv, const char *optstring)
1166 return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
1173 /* Compile with -DTEST to make an executable for use in testing
1174 the above definition of `getopt'. */
1177 main (int argc, char **argv)
1180 int digit_optind = 0;
1184 int this_option_optind = optind ? optind : 1;
1186 c = getopt (argc, argv, "abc:d:0123456789");
1202 if (digit_optind != 0 && digit_optind != this_option_optind)
1203 printf ("digits occur in two different argv-elements.\n");
1204 digit_optind = this_option_optind;
1205 printf ("option %c\n", c);
1209 printf ("option a\n");
1213 printf ("option b\n");
1217 printf ("option c with value `%s'\n", optarg);
1224 printf ("?? getopt returned character code 0%o ??\n", c);
1230 printf ("non-option ARGV-elements: ");
1231 while (optind < argc)
1232 printf ("%s ", argv[optind++]);