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,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004,2006
6 Free Software Foundation, Inc.
7 This file is part of the GNU C Library.
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
41 # define _(msgid) gettext (msgid)
44 #if defined _LIBC && defined USE_IN_LIBIO
48 #ifndef attribute_hidden
49 # define attribute_hidden
52 /* Unlike standard Unix `getopt', functions like `getopt_long'
53 let the user intersperse the options with the other arguments.
55 As `getopt_long' works, it permutes the elements of ARGV so that,
56 when it is done, all the options precede everything else. Thus
57 all application programs are extended to handle flexible argument order.
59 Using `getopt' or setting the environment variable POSIXLY_CORRECT
61 Then the application's behavior is completely standard.
63 GNU application programs can use a third alternative mode in which
64 they can distinguish the relative order of options and other arguments. */
66 #include "getopt_int.h"
68 /* For communication from `getopt' to the caller.
69 When `getopt' finds an option that takes an argument,
70 the argument value is returned here.
71 Also, when `ordering' is RETURN_IN_ORDER,
72 each non-option ARGV-element is returned here. */
76 /* Index in ARGV of the next element to be scanned.
77 This is used for communication to and from the caller
78 and for communication between successive calls to `getopt'.
80 On entry to `getopt', zero means this is the first call; initialize.
82 When `getopt' returns -1, this is the index of the first of the
83 non-option elements that the caller should itself scan.
85 Otherwise, `optind' communicates from one call to the next
86 how much of ARGV has been scanned so far. */
88 /* 1003.2 says this must be 1 before any call. */
91 /* Callers store zero here to inhibit the error message
92 for unrecognized options. */
96 /* Set to an option character which was unrecognized.
97 This must be initialized on some systems to avoid linking in the
98 system's own getopt implementation. */
102 /* Keep a global copy of all internal members of getopt_data. */
104 static struct _getopt_data getopt_data;
107 #if defined HAVE_DECL_GETENV && !HAVE_DECL_GETENV
108 extern char *getenv ();
112 /* Stored original parameters.
113 XXX This is no good solution. We should rather copy the args so
114 that we can compare them later. But we must not use malloc(3). */
115 extern int __libc_argc;
116 extern char **__libc_argv;
118 /* Bash 2.0 gives us an environment variable containing flags
119 indicating ARGV elements that should not be considered arguments. */
121 # ifdef USE_NONOPTION_FLAGS
122 /* Defined in getopt_init.c */
123 extern char *__getopt_nonoption_flags;
126 # ifdef USE_NONOPTION_FLAGS
127 # define SWAP_FLAGS(ch1, ch2) \
128 if (d->__nonoption_flags_len > 0) \
130 char __tmp = __getopt_nonoption_flags[ch1]; \
131 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
132 __getopt_nonoption_flags[ch2] = __tmp; \
135 # define SWAP_FLAGS(ch1, ch2)
138 # define SWAP_FLAGS(ch1, ch2)
141 /* Exchange two adjacent subsequences of ARGV.
142 One subsequence is elements [first_nonopt,last_nonopt)
143 which contains all the non-options that have been skipped so far.
144 The other is elements [last_nonopt,optind), which contains all
145 the options processed since those non-options were skipped.
147 `first_nonopt' and `last_nonopt' are relocated so that they describe
148 the new indices of the non-options in ARGV after they are moved. */
151 exchange (char **argv, struct _getopt_data *d)
153 int bottom = d->__first_nonopt;
154 int middle = d->__last_nonopt;
158 /* Exchange the shorter segment with the far end of the longer segment.
159 That puts the shorter segment into the right place.
160 It leaves the longer segment in the right place overall,
161 but it consists of two parts that need to be swapped next. */
163 #if defined _LIBC && defined USE_NONOPTION_FLAGS
164 /* First make sure the handling of the `__getopt_nonoption_flags'
165 string can work normally. Our top argument must be in the range
167 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
169 /* We must extend the array. The user plays games with us and
170 presents new arguments. */
171 char *new_str = malloc (top + 1);
173 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
176 memset (__mempcpy (new_str, __getopt_nonoption_flags,
177 d->__nonoption_flags_max_len),
178 '\0', top + 1 - d->__nonoption_flags_max_len);
179 d->__nonoption_flags_max_len = top + 1;
180 __getopt_nonoption_flags = new_str;
185 while (top > middle && middle > bottom)
187 if (top - middle > middle - bottom)
189 /* Bottom segment is the short one. */
190 int len = middle - bottom;
193 /* Swap it with the top part of the top segment. */
194 for (i = 0; i < len; i++)
196 tem = argv[bottom + i];
197 argv[bottom + i] = argv[top - (middle - bottom) + i];
198 argv[top - (middle - bottom) + i] = tem;
199 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
201 /* Exclude the moved bottom segment from further swapping. */
206 /* Top segment is the short one. */
207 int len = top - middle;
210 /* Swap it with the bottom part of the bottom segment. */
211 for (i = 0; i < len; i++)
213 tem = argv[bottom + i];
214 argv[bottom + i] = argv[middle + i];
215 argv[middle + i] = tem;
216 SWAP_FLAGS (bottom + i, middle + i);
218 /* Exclude the moved top segment from further swapping. */
223 /* Update records for the slots the non-options now occupy. */
225 d->__first_nonopt += (d->optind - d->__last_nonopt);
226 d->__last_nonopt = d->optind;
229 /* Initialize the internal data when the first call is made. */
232 _getopt_initialize (int argc, char **argv, const char *optstring,
233 int posixly_correct, struct _getopt_data *d)
235 /* Start processing options with ARGV-element 1 (since ARGV-element 0
236 is the program name); the sequence of previously skipped
237 non-option ARGV-elements is empty. */
239 d->__first_nonopt = d->__last_nonopt = d->optind;
241 d->__nextchar = NULL;
243 d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
245 /* Determine how to handle the ordering of options and nonoptions. */
247 if (optstring[0] == '-')
249 d->__ordering = RETURN_IN_ORDER;
252 else if (optstring[0] == '+')
254 d->__ordering = REQUIRE_ORDER;
257 else if (d->__posixly_correct)
258 d->__ordering = REQUIRE_ORDER;
260 d->__ordering = PERMUTE;
262 #if defined _LIBC && defined USE_NONOPTION_FLAGS
263 if (!d->__posixly_correct
264 && argc == __libc_argc && argv == __libc_argv)
266 if (d->__nonoption_flags_max_len == 0)
268 if (__getopt_nonoption_flags == NULL
269 || __getopt_nonoption_flags[0] == '\0')
270 d->__nonoption_flags_max_len = -1;
273 const char *orig_str = __getopt_nonoption_flags;
274 int len = d->__nonoption_flags_max_len = strlen (orig_str);
275 if (d->__nonoption_flags_max_len < argc)
276 d->__nonoption_flags_max_len = argc;
277 __getopt_nonoption_flags =
278 (char *) malloc (d->__nonoption_flags_max_len);
279 if (__getopt_nonoption_flags == NULL)
280 d->__nonoption_flags_max_len = -1;
282 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
283 '\0', d->__nonoption_flags_max_len - len);
286 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
289 d->__nonoption_flags_len = 0;
295 /* Scan elements of ARGV (whose length is ARGC) for option characters
298 If an element of ARGV starts with '-', and is not exactly "-" or "--",
299 then it is an option element. The characters of this element
300 (aside from the initial '-') are option characters. If `getopt'
301 is called repeatedly, it returns successively each of the option characters
302 from each of the option elements.
304 If `getopt' finds another option character, it returns that character,
305 updating `optind' and `nextchar' so that the next call to `getopt' can
306 resume the scan with the following option character or ARGV-element.
308 If there are no more option characters, `getopt' returns -1.
309 Then `optind' is the index in ARGV of the first ARGV-element
310 that is not an option. (The ARGV-elements have been permuted
311 so that those that are not options now come last.)
313 OPTSTRING is a string containing the legitimate option characters.
314 If an option character is seen that is not listed in OPTSTRING,
315 return '?' after printing an error message. If you set `opterr' to
316 zero, the error message is suppressed but we still return '?'.
318 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
319 so the following text in the same ARGV-element, or the text of the following
320 ARGV-element, is returned in `optarg'. Two colons mean an option that
321 wants an optional arg; if there is text in the current ARGV-element,
322 it is returned in `optarg', otherwise `optarg' is set to zero.
324 If OPTSTRING starts with `-' or `+', it requests different methods of
325 handling the non-option ARGV-elements.
326 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
328 Long-named options begin with `--' instead of `-'.
329 Their names may be abbreviated as long as the abbreviation is unique
330 or is an exact match for some defined option. If they have an
331 argument, it follows the option name in the same ARGV-element, separated
332 from the option name by a `=', or else the in next ARGV-element.
333 When `getopt' finds a long-named option, it returns 0 if that option's
334 `flag' field is nonzero, the value of the option's `val' field
335 if the `flag' field is zero.
337 LONGOPTS is a vector of `struct option' terminated by an
338 element containing a name which is zero.
340 LONGIND returns the index in LONGOPT of the long-named option found.
341 It is only valid when a long-named option has been found by the most
344 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
347 If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
348 environment variable were set. */
351 _getopt_internal_r (int argc, char **argv, const char *optstring,
352 const struct option *longopts, int *longind,
353 int long_only, int posixly_correct, struct _getopt_data *d)
355 int print_errors = d->opterr;
356 if (optstring[0] == ':')
364 if (d->optind == 0 || !d->__initialized)
367 d->optind = 1; /* Don't scan ARGV[0], the program name. */
368 optstring = _getopt_initialize (argc, argv, optstring,
370 d->__initialized = 1;
373 /* Test whether ARGV[optind] points to a non-option argument.
374 Either it does not have option syntax, or there is an environment flag
375 from the shell indicating it is not an option. The later information
376 is only used when the used in the GNU libc. */
377 #if defined _LIBC && defined USE_NONOPTION_FLAGS
378 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
379 || (d->optind < d->__nonoption_flags_len \
380 && __getopt_nonoption_flags[d->optind] == '1'))
382 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
385 if (d->__nextchar == NULL || *d->__nextchar == '\0')
387 /* Advance to the next ARGV-element. */
389 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
390 moved back by the user (who may also have changed the arguments). */
391 if (d->__last_nonopt > d->optind)
392 d->__last_nonopt = d->optind;
393 if (d->__first_nonopt > d->optind)
394 d->__first_nonopt = d->optind;
396 if (d->__ordering == PERMUTE)
398 /* If we have just processed some options following some non-options,
399 exchange them so that the options come first. */
401 if (d->__first_nonopt != d->__last_nonopt
402 && d->__last_nonopt != d->optind)
403 exchange ((char **) argv, d);
404 else if (d->__last_nonopt != d->optind)
405 d->__first_nonopt = d->optind;
407 /* Skip any additional non-options
408 and extend the range of non-options previously skipped. */
410 while (d->optind < argc && NONOPTION_P)
412 d->__last_nonopt = d->optind;
415 /* The special ARGV-element `--' means premature end of options.
416 Skip it like a null option,
417 then exchange with previous non-options as if it were an option,
418 then skip everything else like a non-option. */
420 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
424 if (d->__first_nonopt != d->__last_nonopt
425 && d->__last_nonopt != d->optind)
426 exchange ((char **) argv, d);
427 else if (d->__first_nonopt == d->__last_nonopt)
428 d->__first_nonopt = d->optind;
429 d->__last_nonopt = argc;
434 /* If we have done all the ARGV-elements, stop the scan
435 and back over any non-options that we skipped and permuted. */
437 if (d->optind == argc)
439 /* Set the next-arg-index to point at the non-options
440 that we previously skipped, so the caller will digest them. */
441 if (d->__first_nonopt != d->__last_nonopt)
442 d->optind = d->__first_nonopt;
446 /* If we have come to a non-option and did not permute it,
447 either stop the scan or describe it to the caller and pass it by. */
451 if (d->__ordering == REQUIRE_ORDER)
453 d->optarg = argv[d->optind++];
457 /* We have found another option-ARGV-element.
458 Skip the initial punctuation. */
460 d->__nextchar = (argv[d->optind] + 1
461 + (longopts != NULL && argv[d->optind][1] == '-'));
464 /* Decode the current option-ARGV-element. */
466 /* Check whether the ARGV-element is a long option.
468 If long_only and the ARGV-element has the form "-f", where f is
469 a valid short option, don't consider it an abbreviated form of
470 a long option that starts with f. Otherwise there would be no
471 way to give the -f short option.
473 On the other hand, if there's a long option "fubar" and
474 the ARGV-element is "-fu", do consider that an abbreviation of
475 the long option, just like "--fu", and not "-f" with arg "u".
477 This distinction seems to be the most useful approach. */
480 && (argv[d->optind][1] == '-'
481 || (long_only && (argv[d->optind][2]
482 || !strchr (optstring, argv[d->optind][1])))))
485 const struct option *p;
486 const struct option *pfound = NULL;
492 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
495 /* Test all long options for either exact match
496 or abbreviated matches. */
497 for (p = longopts, option_index = 0; p->name; p++, option_index++)
498 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
500 if ((unsigned int) (nameend - d->__nextchar)
501 == (unsigned int) strlen (p->name))
503 /* Exact match found. */
505 indfound = option_index;
509 else if (pfound == NULL)
511 /* First nonexact match found. */
513 indfound = option_index;
516 || pfound->has_arg != p->has_arg
517 || pfound->flag != p->flag
518 || pfound->val != p->val)
519 /* Second or later nonexact match found. */
527 #if defined _LIBC && defined USE_IN_LIBIO
530 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
531 argv[0], argv[d->optind]) >= 0)
533 _IO_flockfile (stderr);
535 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
536 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
538 __fxprintf (NULL, "%s", buf);
540 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
541 _IO_funlockfile (stderr);
546 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
547 argv[0], argv[d->optind]);
550 d->__nextchar += strlen (d->__nextchar);
558 option_index = indfound;
562 /* Don't test has_arg with >, because some C compilers don't
563 allow it to be used on enums. */
565 d->optarg = nameend + 1;
570 #if defined _LIBC && defined USE_IN_LIBIO
575 if (argv[d->optind - 1][1] == '-')
578 #if defined _LIBC && defined USE_IN_LIBIO
579 n = __asprintf (&buf, _("\
580 %s: option `--%s' doesn't allow an argument\n"),
581 argv[0], pfound->name);
583 fprintf (stderr, _("\
584 %s: option `--%s' doesn't allow an argument\n"),
585 argv[0], pfound->name);
590 /* +option or -option */
591 #if defined _LIBC && defined USE_IN_LIBIO
592 n = __asprintf (&buf, _("\
593 %s: option `%c%s' doesn't allow an argument\n"),
594 argv[0], argv[d->optind - 1][0],
597 fprintf (stderr, _("\
598 %s: option `%c%s' doesn't allow an argument\n"),
599 argv[0], argv[d->optind - 1][0],
604 #if defined _LIBC && defined USE_IN_LIBIO
607 _IO_flockfile (stderr);
609 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
610 ((_IO_FILE *) stderr)->_flags2
611 |= _IO_FLAGS2_NOTCANCEL;
613 __fxprintf (NULL, "%s", buf);
615 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
616 _IO_funlockfile (stderr);
623 d->__nextchar += strlen (d->__nextchar);
625 d->optopt = pfound->val;
629 else if (pfound->has_arg == 1)
631 if (d->optind < argc)
632 d->optarg = argv[d->optind++];
637 #if defined _LIBC && defined USE_IN_LIBIO
640 if (__asprintf (&buf, _("\
641 %s: option `%s' requires an argument\n"),
642 argv[0], argv[d->optind - 1]) >= 0)
644 _IO_flockfile (stderr);
646 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
647 ((_IO_FILE *) stderr)->_flags2
648 |= _IO_FLAGS2_NOTCANCEL;
650 __fxprintf (NULL, "%s", buf);
652 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
653 _IO_funlockfile (stderr);
659 _("%s: option `%s' requires an argument\n"),
660 argv[0], argv[d->optind - 1]);
663 d->__nextchar += strlen (d->__nextchar);
664 d->optopt = pfound->val;
665 return optstring[0] == ':' ? ':' : '?';
668 d->__nextchar += strlen (d->__nextchar);
670 *longind = option_index;
673 *(pfound->flag) = pfound->val;
679 /* Can't find it as a long option. If this is not getopt_long_only,
680 or the option starts with '--' or is not a valid short
681 option, then it's an error.
682 Otherwise interpret it as a short option. */
683 if (!long_only || argv[d->optind][1] == '-'
684 || strchr (optstring, *d->__nextchar) == NULL)
688 #if defined _LIBC && defined USE_IN_LIBIO
693 if (argv[d->optind][1] == '-')
696 #if defined _LIBC && defined USE_IN_LIBIO
697 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
698 argv[0], d->__nextchar);
700 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
701 argv[0], d->__nextchar);
706 /* +option or -option */
707 #if defined _LIBC && defined USE_IN_LIBIO
708 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
709 argv[0], argv[d->optind][0], d->__nextchar);
711 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
712 argv[0], argv[d->optind][0], d->__nextchar);
716 #if defined _LIBC && defined USE_IN_LIBIO
719 _IO_flockfile (stderr);
721 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
722 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
724 __fxprintf (NULL, "%s", buf);
726 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
727 _IO_funlockfile (stderr);
733 d->__nextchar = (char *) "";
740 /* Look at and handle the next short option-character. */
743 char c = *d->__nextchar++;
744 char *temp = strchr (optstring, c);
746 /* Increment `optind' when we start to process its last character. */
747 if (*d->__nextchar == '\0')
750 if (temp == NULL || c == ':')
754 #if defined _LIBC && defined USE_IN_LIBIO
759 if (d->__posixly_correct)
761 /* 1003.2 specifies the format of this message. */
762 #if defined _LIBC && defined USE_IN_LIBIO
763 n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
766 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
771 #if defined _LIBC && defined USE_IN_LIBIO
772 n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
775 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
779 #if defined _LIBC && defined USE_IN_LIBIO
782 _IO_flockfile (stderr);
784 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
785 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
787 __fxprintf (NULL, "%s", buf);
789 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
790 _IO_funlockfile (stderr);
799 /* Convenience. Treat POSIX -W foo same as long option --foo */
800 if (temp[0] == 'W' && temp[1] == ';')
803 const struct option *p;
804 const struct option *pfound = NULL;
810 /* This is an option that requires an argument. */
811 if (*d->__nextchar != '\0')
813 d->optarg = d->__nextchar;
814 /* If we end this ARGV-element by taking the rest as an arg,
815 we must advance to the next element now. */
818 else if (d->optind == argc)
822 /* 1003.2 specifies the format of this message. */
823 #if defined _LIBC && defined USE_IN_LIBIO
826 if (__asprintf (&buf,
827 _("%s: option requires an argument -- %c\n"),
830 _IO_flockfile (stderr);
832 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
833 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
835 __fxprintf (NULL, "%s", buf);
837 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
838 _IO_funlockfile (stderr);
843 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
848 if (optstring[0] == ':')
855 /* We already incremented `d->optind' once;
856 increment it again when taking next ARGV-elt as argument. */
857 d->optarg = argv[d->optind++];
859 /* optarg is now the argument, see if it's in the
860 table of longopts. */
862 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
866 /* Test all long options for either exact match
867 or abbreviated matches. */
868 for (p = longopts, option_index = 0; p->name; p++, option_index++)
869 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
871 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
873 /* Exact match found. */
875 indfound = option_index;
879 else if (pfound == NULL)
881 /* First nonexact match found. */
883 indfound = option_index;
886 /* Second or later nonexact match found. */
893 #if defined _LIBC && defined USE_IN_LIBIO
896 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
897 argv[0], argv[d->optind]) >= 0)
899 _IO_flockfile (stderr);
901 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
902 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
904 __fxprintf (NULL, "%s", buf);
906 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
907 _IO_funlockfile (stderr);
912 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
913 argv[0], argv[d->optind]);
916 d->__nextchar += strlen (d->__nextchar);
922 option_index = indfound;
925 /* Don't test has_arg with >, because some C compilers don't
926 allow it to be used on enums. */
928 d->optarg = nameend + 1;
933 #if defined _LIBC && defined USE_IN_LIBIO
936 if (__asprintf (&buf, _("\
937 %s: option `-W %s' doesn't allow an argument\n"),
938 argv[0], pfound->name) >= 0)
940 _IO_flockfile (stderr);
942 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
943 ((_IO_FILE *) stderr)->_flags2
944 |= _IO_FLAGS2_NOTCANCEL;
946 __fxprintf (NULL, "%s", buf);
948 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
949 _IO_funlockfile (stderr);
954 fprintf (stderr, _("\
955 %s: option `-W %s' doesn't allow an argument\n"),
956 argv[0], pfound->name);
960 d->__nextchar += strlen (d->__nextchar);
964 else if (pfound->has_arg == 1)
966 if (d->optind < argc)
967 d->optarg = argv[d->optind++];
972 #if defined _LIBC && defined USE_IN_LIBIO
975 if (__asprintf (&buf, _("\
976 %s: option `%s' requires an argument\n"),
977 argv[0], argv[d->optind - 1]) >= 0)
979 _IO_flockfile (stderr);
981 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
982 ((_IO_FILE *) stderr)->_flags2
983 |= _IO_FLAGS2_NOTCANCEL;
985 __fxprintf (NULL, "%s", buf);
987 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
988 _IO_funlockfile (stderr);
994 _("%s: option `%s' requires an argument\n"),
995 argv[0], argv[d->optind - 1]);
998 d->__nextchar += strlen (d->__nextchar);
999 return optstring[0] == ':' ? ':' : '?';
1002 d->__nextchar += strlen (d->__nextchar);
1003 if (longind != NULL)
1004 *longind = option_index;
1007 *(pfound->flag) = pfound->val;
1012 d->__nextchar = NULL;
1013 return 'W'; /* Let the application handle it. */
1019 /* This is an option that accepts an argument optionally. */
1020 if (*d->__nextchar != '\0')
1022 d->optarg = d->__nextchar;
1027 d->__nextchar = NULL;
1031 /* This is an option that requires an argument. */
1032 if (*d->__nextchar != '\0')
1034 d->optarg = d->__nextchar;
1035 /* If we end this ARGV-element by taking the rest as an arg,
1036 we must advance to the next element now. */
1039 else if (d->optind == argc)
1043 /* 1003.2 specifies the format of this message. */
1044 #if defined _LIBC && defined USE_IN_LIBIO
1047 if (__asprintf (&buf, _("\
1048 %s: option requires an argument -- %c\n"),
1051 _IO_flockfile (stderr);
1053 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1054 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1056 __fxprintf (NULL, "%s", buf);
1058 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1059 _IO_funlockfile (stderr);
1065 _("%s: option requires an argument -- %c\n"),
1070 if (optstring[0] == ':')
1076 /* We already incremented `optind' once;
1077 increment it again when taking next ARGV-elt as argument. */
1078 d->optarg = argv[d->optind++];
1079 d->__nextchar = NULL;
1087 _getopt_internal (int argc, char **argv, const char *optstring,
1088 const struct option *longopts, int *longind,
1089 int long_only, int posixly_correct)
1093 getopt_data.optind = optind;
1094 getopt_data.opterr = opterr;
1096 result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
1097 long_only, posixly_correct, &getopt_data);
1099 optind = getopt_data.optind;
1100 optarg = getopt_data.optarg;
1101 optopt = getopt_data.optopt;
1106 /* glibc gets a LSB-compliant getopt.
1107 Standalone applications get a POSIX-compliant getopt. */
1109 enum { POSIXLY_CORRECT = 0 };
1111 enum { POSIXLY_CORRECT = 1 };
1115 getopt (int argc, char *const *argv, const char *optstring)
1117 return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
1124 /* Compile with -DTEST to make an executable for use in testing
1125 the above definition of `getopt'. */
1128 main (int argc, char **argv)
1131 int digit_optind = 0;
1135 int this_option_optind = optind ? optind : 1;
1137 c = getopt (argc, argv, "abc:d:0123456789");
1153 if (digit_optind != 0 && digit_optind != this_option_optind)
1154 printf ("digits occur in two different argv-elements.\n");
1155 digit_optind = this_option_optind;
1156 printf ("option %c\n", c);
1160 printf ("option a\n");
1164 printf ("option b\n");
1168 printf ("option c with value `%s'\n", optarg);
1175 printf ("?? getopt returned character code 0%o ??\n", c);
1181 printf ("non-option ARGV-elements: ");
1182 while (optind < argc)
1183 printf ("%s ", argv[optind++]);