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 2, or (at your option)
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 along
20 with this program; if not, write to the Free Software Foundation,
21 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
43 # define _(msgid) gettext (msgid)
45 # define _(msgid) (msgid)
49 #if defined _LIBC && defined USE_IN_LIBIO
53 #ifndef attribute_hidden
54 # define attribute_hidden
57 /* Unlike standard Unix `getopt', functions like `getopt_long'
58 let the user intersperse the options with the other arguments.
60 As `getopt_long' works, it permutes the elements of ARGV so that,
61 when it is done, all the options precede everything else. Thus
62 all application programs are extended to handle flexible argument order.
64 Using `getopt' or setting the environment variable POSIXLY_CORRECT
66 Then the application's behavior is completely standard.
68 GNU application programs can use a third alternative mode in which
69 they can distinguish the relative order of options and other arguments. */
71 #include "getopt_int.h"
73 /* For communication from `getopt' to the caller.
74 When `getopt' finds an option that takes an argument,
75 the argument value is returned here.
76 Also, when `ordering' is RETURN_IN_ORDER,
77 each non-option ARGV-element is returned here. */
81 /* Index in ARGV of the next element to be scanned.
82 This is used for communication to and from the caller
83 and for communication between successive calls to `getopt'.
85 On entry to `getopt', zero means this is the first call; initialize.
87 When `getopt' returns -1, this is the index of the first of the
88 non-option elements that the caller should itself scan.
90 Otherwise, `optind' communicates from one call to the next
91 how much of ARGV has been scanned so far. */
93 /* 1003.2 says this must be 1 before any call. */
96 /* Callers store zero here to inhibit the error message
97 for unrecognized options. */
101 /* Set to an option character which was unrecognized.
102 This must be initialized on some systems to avoid linking in the
103 system's own getopt implementation. */
107 /* Keep a global copy of all internal members of getopt_data. */
109 static struct _getopt_data getopt_data;
112 #if defined HAVE_DECL_GETENV && !HAVE_DECL_GETENV
113 extern char *getenv ();
117 /* Stored original parameters.
118 XXX This is no good solution. We should rather copy the args so
119 that we can compare them later. But we must not use malloc(3). */
120 extern int __libc_argc;
121 extern char **__libc_argv;
123 /* Bash 2.0 gives us an environment variable containing flags
124 indicating ARGV elements that should not be considered arguments. */
126 # ifdef USE_NONOPTION_FLAGS
127 /* Defined in getopt_init.c */
128 extern char *__getopt_nonoption_flags;
131 # ifdef USE_NONOPTION_FLAGS
132 # define SWAP_FLAGS(ch1, ch2) \
133 if (d->__nonoption_flags_len > 0) \
135 char __tmp = __getopt_nonoption_flags[ch1]; \
136 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
137 __getopt_nonoption_flags[ch2] = __tmp; \
140 # define SWAP_FLAGS(ch1, ch2)
143 # define SWAP_FLAGS(ch1, ch2)
146 /* Exchange two adjacent subsequences of ARGV.
147 One subsequence is elements [first_nonopt,last_nonopt)
148 which contains all the non-options that have been skipped so far.
149 The other is elements [last_nonopt,optind), which contains all
150 the options processed since those non-options were skipped.
152 `first_nonopt' and `last_nonopt' are relocated so that they describe
153 the new indices of the non-options in ARGV after they are moved. */
156 exchange (char **argv, struct _getopt_data *d)
158 int bottom = d->__first_nonopt;
159 int middle = d->__last_nonopt;
163 /* Exchange the shorter segment with the far end of the longer segment.
164 That puts the shorter segment into the right place.
165 It leaves the longer segment in the right place overall,
166 but it consists of two parts that need to be swapped next. */
168 #if defined _LIBC && defined USE_NONOPTION_FLAGS
169 /* First make sure the handling of the `__getopt_nonoption_flags'
170 string can work normally. Our top argument must be in the range
172 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
174 /* We must extend the array. The user plays games with us and
175 presents new arguments. */
176 char *new_str = malloc (top + 1);
178 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
181 memset (__mempcpy (new_str, __getopt_nonoption_flags,
182 d->__nonoption_flags_max_len),
183 '\0', top + 1 - d->__nonoption_flags_max_len);
184 d->__nonoption_flags_max_len = top + 1;
185 __getopt_nonoption_flags = new_str;
190 while (top > middle && middle > bottom)
192 if (top - middle > middle - bottom)
194 /* Bottom segment is the short one. */
195 int len = middle - bottom;
198 /* Swap it with the top part of the top segment. */
199 for (i = 0; i < len; i++)
201 tem = argv[bottom + i];
202 argv[bottom + i] = argv[top - (middle - bottom) + i];
203 argv[top - (middle - bottom) + i] = tem;
204 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
206 /* Exclude the moved bottom segment from further swapping. */
211 /* Top segment is the short one. */
212 int len = top - middle;
215 /* Swap it with the bottom part of the bottom segment. */
216 for (i = 0; i < len; i++)
218 tem = argv[bottom + i];
219 argv[bottom + i] = argv[middle + i];
220 argv[middle + i] = tem;
221 SWAP_FLAGS (bottom + i, middle + i);
223 /* Exclude the moved top segment from further swapping. */
228 /* Update records for the slots the non-options now occupy. */
230 d->__first_nonopt += (d->optind - d->__last_nonopt);
231 d->__last_nonopt = d->optind;
234 /* Initialize the internal data when the first call is made. */
237 _getopt_initialize (int argc, char **argv, const char *optstring,
238 int posixly_correct, struct _getopt_data *d)
240 /* Start processing options with ARGV-element 1 (since ARGV-element 0
241 is the program name); the sequence of previously skipped
242 non-option ARGV-elements is empty. */
244 d->__first_nonopt = d->__last_nonopt = d->optind;
246 d->__nextchar = NULL;
248 d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
250 /* Determine how to handle the ordering of options and nonoptions. */
252 if (optstring[0] == '-')
254 d->__ordering = RETURN_IN_ORDER;
257 else if (optstring[0] == '+')
259 d->__ordering = REQUIRE_ORDER;
262 else if (d->__posixly_correct)
263 d->__ordering = REQUIRE_ORDER;
265 d->__ordering = PERMUTE;
267 #if defined _LIBC && defined USE_NONOPTION_FLAGS
268 if (!d->__posixly_correct
269 && argc == __libc_argc && argv == __libc_argv)
271 if (d->__nonoption_flags_max_len == 0)
273 if (__getopt_nonoption_flags == NULL
274 || __getopt_nonoption_flags[0] == '\0')
275 d->__nonoption_flags_max_len = -1;
278 const char *orig_str = __getopt_nonoption_flags;
279 int len = d->__nonoption_flags_max_len = strlen (orig_str);
280 if (d->__nonoption_flags_max_len < argc)
281 d->__nonoption_flags_max_len = argc;
282 __getopt_nonoption_flags =
283 (char *) malloc (d->__nonoption_flags_max_len);
284 if (__getopt_nonoption_flags == NULL)
285 d->__nonoption_flags_max_len = -1;
287 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
288 '\0', d->__nonoption_flags_max_len - len);
291 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
294 d->__nonoption_flags_len = 0;
300 /* Scan elements of ARGV (whose length is ARGC) for option characters
303 If an element of ARGV starts with '-', and is not exactly "-" or "--",
304 then it is an option element. The characters of this element
305 (aside from the initial '-') are option characters. If `getopt'
306 is called repeatedly, it returns successively each of the option characters
307 from each of the option elements.
309 If `getopt' finds another option character, it returns that character,
310 updating `optind' and `nextchar' so that the next call to `getopt' can
311 resume the scan with the following option character or ARGV-element.
313 If there are no more option characters, `getopt' returns -1.
314 Then `optind' is the index in ARGV of the first ARGV-element
315 that is not an option. (The ARGV-elements have been permuted
316 so that those that are not options now come last.)
318 OPTSTRING is a string containing the legitimate option characters.
319 If an option character is seen that is not listed in OPTSTRING,
320 return '?' after printing an error message. If you set `opterr' to
321 zero, the error message is suppressed but we still return '?'.
323 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
324 so the following text in the same ARGV-element, or the text of the following
325 ARGV-element, is returned in `optarg'. Two colons mean an option that
326 wants an optional arg; if there is text in the current ARGV-element,
327 it is returned in `optarg', otherwise `optarg' is set to zero.
329 If OPTSTRING starts with `-' or `+', it requests different methods of
330 handling the non-option ARGV-elements.
331 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
333 Long-named options begin with `--' instead of `-'.
334 Their names may be abbreviated as long as the abbreviation is unique
335 or is an exact match for some defined option. If they have an
336 argument, it follows the option name in the same ARGV-element, separated
337 from the option name by a `=', or else the in next ARGV-element.
338 When `getopt' finds a long-named option, it returns 0 if that option's
339 `flag' field is nonzero, the value of the option's `val' field
340 if the `flag' field is zero.
342 LONGOPTS is a vector of `struct option' terminated by an
343 element containing a name which is zero.
345 LONGIND returns the index in LONGOPT of the long-named option found.
346 It is only valid when a long-named option has been found by the most
349 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
352 If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
353 environment variable were set. */
356 _getopt_internal_r (int argc, char **argv, const char *optstring,
357 const struct option *longopts, int *longind,
358 int long_only, int posixly_correct, struct _getopt_data *d)
360 int print_errors = d->opterr;
361 if (optstring[0] == ':')
369 if (d->optind == 0 || !d->__initialized)
372 d->optind = 1; /* Don't scan ARGV[0], the program name. */
373 optstring = _getopt_initialize (argc, argv, optstring,
375 d->__initialized = 1;
378 /* Test whether ARGV[optind] points to a non-option argument.
379 Either it does not have option syntax, or there is an environment flag
380 from the shell indicating it is not an option. The later information
381 is only used when the used in the GNU libc. */
382 #if defined _LIBC && defined USE_NONOPTION_FLAGS
383 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
384 || (d->optind < d->__nonoption_flags_len \
385 && __getopt_nonoption_flags[d->optind] == '1'))
387 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
390 if (d->__nextchar == NULL || *d->__nextchar == '\0')
392 /* Advance to the next ARGV-element. */
394 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
395 moved back by the user (who may also have changed the arguments). */
396 if (d->__last_nonopt > d->optind)
397 d->__last_nonopt = d->optind;
398 if (d->__first_nonopt > d->optind)
399 d->__first_nonopt = d->optind;
401 if (d->__ordering == PERMUTE)
403 /* If we have just processed some options following some non-options,
404 exchange them so that the options come first. */
406 if (d->__first_nonopt != d->__last_nonopt
407 && d->__last_nonopt != d->optind)
408 exchange ((char **) argv, d);
409 else if (d->__last_nonopt != d->optind)
410 d->__first_nonopt = d->optind;
412 /* Skip any additional non-options
413 and extend the range of non-options previously skipped. */
415 while (d->optind < argc && NONOPTION_P)
417 d->__last_nonopt = d->optind;
420 /* The special ARGV-element `--' means premature end of options.
421 Skip it like a null option,
422 then exchange with previous non-options as if it were an option,
423 then skip everything else like a non-option. */
425 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
429 if (d->__first_nonopt != d->__last_nonopt
430 && d->__last_nonopt != d->optind)
431 exchange ((char **) argv, d);
432 else if (d->__first_nonopt == d->__last_nonopt)
433 d->__first_nonopt = d->optind;
434 d->__last_nonopt = argc;
439 /* If we have done all the ARGV-elements, stop the scan
440 and back over any non-options that we skipped and permuted. */
442 if (d->optind == argc)
444 /* Set the next-arg-index to point at the non-options
445 that we previously skipped, so the caller will digest them. */
446 if (d->__first_nonopt != d->__last_nonopt)
447 d->optind = d->__first_nonopt;
451 /* If we have come to a non-option and did not permute it,
452 either stop the scan or describe it to the caller and pass it by. */
456 if (d->__ordering == REQUIRE_ORDER)
458 d->optarg = argv[d->optind++];
462 /* We have found another option-ARGV-element.
463 Skip the initial punctuation. */
465 d->__nextchar = (argv[d->optind] + 1
466 + (longopts != NULL && argv[d->optind][1] == '-'));
469 /* Decode the current option-ARGV-element. */
471 /* Check whether the ARGV-element is a long option.
473 If long_only and the ARGV-element has the form "-f", where f is
474 a valid short option, don't consider it an abbreviated form of
475 a long option that starts with f. Otherwise there would be no
476 way to give the -f short option.
478 On the other hand, if there's a long option "fubar" and
479 the ARGV-element is "-fu", do consider that an abbreviation of
480 the long option, just like "--fu", and not "-f" with arg "u".
482 This distinction seems to be the most useful approach. */
485 && (argv[d->optind][1] == '-'
486 || (long_only && (argv[d->optind][2]
487 || !strchr (optstring, argv[d->optind][1])))))
490 const struct option *p;
491 const struct option *pfound = NULL;
497 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
500 /* Test all long options for either exact match
501 or abbreviated matches. */
502 for (p = longopts, option_index = 0; p->name; p++, option_index++)
503 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
505 if ((unsigned int) (nameend - d->__nextchar)
506 == (unsigned int) strlen (p->name))
508 /* Exact match found. */
510 indfound = option_index;
514 else if (pfound == NULL)
516 /* First nonexact match found. */
518 indfound = option_index;
521 || pfound->has_arg != p->has_arg
522 || pfound->flag != p->flag
523 || pfound->val != p->val)
524 /* Second or later nonexact match found. */
532 #if defined _LIBC && defined USE_IN_LIBIO
535 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
536 argv[0], argv[d->optind]) >= 0)
538 _IO_flockfile (stderr);
540 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
541 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
543 __fxprintf (NULL, "%s", buf);
545 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
546 _IO_funlockfile (stderr);
551 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
552 argv[0], argv[d->optind]);
555 d->__nextchar += strlen (d->__nextchar);
563 option_index = indfound;
567 /* Don't test has_arg with >, because some C compilers don't
568 allow it to be used on enums. */
570 d->optarg = nameend + 1;
575 #if defined _LIBC && defined USE_IN_LIBIO
580 if (argv[d->optind - 1][1] == '-')
583 #if defined _LIBC && defined USE_IN_LIBIO
584 n = __asprintf (&buf, _("\
585 %s: option `--%s' doesn't allow an argument\n"),
586 argv[0], pfound->name);
588 fprintf (stderr, _("\
589 %s: option `--%s' doesn't allow an argument\n"),
590 argv[0], pfound->name);
595 /* +option or -option */
596 #if defined _LIBC && defined USE_IN_LIBIO
597 n = __asprintf (&buf, _("\
598 %s: option `%c%s' doesn't allow an argument\n"),
599 argv[0], argv[d->optind - 1][0],
602 fprintf (stderr, _("\
603 %s: option `%c%s' doesn't allow an argument\n"),
604 argv[0], argv[d->optind - 1][0],
609 #if defined _LIBC && defined USE_IN_LIBIO
612 _IO_flockfile (stderr);
614 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
615 ((_IO_FILE *) stderr)->_flags2
616 |= _IO_FLAGS2_NOTCANCEL;
618 __fxprintf (NULL, "%s", buf);
620 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
621 _IO_funlockfile (stderr);
628 d->__nextchar += strlen (d->__nextchar);
630 d->optopt = pfound->val;
634 else if (pfound->has_arg == 1)
636 if (d->optind < argc)
637 d->optarg = argv[d->optind++];
642 #if defined _LIBC && defined USE_IN_LIBIO
645 if (__asprintf (&buf, _("\
646 %s: option `%s' requires an argument\n"),
647 argv[0], argv[d->optind - 1]) >= 0)
649 _IO_flockfile (stderr);
651 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
652 ((_IO_FILE *) stderr)->_flags2
653 |= _IO_FLAGS2_NOTCANCEL;
655 __fxprintf (NULL, "%s", buf);
657 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
658 _IO_funlockfile (stderr);
664 _("%s: option `%s' requires an argument\n"),
665 argv[0], argv[d->optind - 1]);
668 d->__nextchar += strlen (d->__nextchar);
669 d->optopt = pfound->val;
670 return optstring[0] == ':' ? ':' : '?';
673 d->__nextchar += strlen (d->__nextchar);
675 *longind = option_index;
678 *(pfound->flag) = pfound->val;
684 /* Can't find it as a long option. If this is not getopt_long_only,
685 or the option starts with '--' or is not a valid short
686 option, then it's an error.
687 Otherwise interpret it as a short option. */
688 if (!long_only || argv[d->optind][1] == '-'
689 || strchr (optstring, *d->__nextchar) == NULL)
693 #if defined _LIBC && defined USE_IN_LIBIO
698 if (argv[d->optind][1] == '-')
701 #if defined _LIBC && defined USE_IN_LIBIO
702 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
703 argv[0], d->__nextchar);
705 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
706 argv[0], d->__nextchar);
711 /* +option or -option */
712 #if defined _LIBC && defined USE_IN_LIBIO
713 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
714 argv[0], argv[d->optind][0], d->__nextchar);
716 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
717 argv[0], argv[d->optind][0], d->__nextchar);
721 #if defined _LIBC && defined USE_IN_LIBIO
724 _IO_flockfile (stderr);
726 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
727 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
729 __fxprintf (NULL, "%s", buf);
731 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
732 _IO_funlockfile (stderr);
738 d->__nextchar = (char *) "";
745 /* Look at and handle the next short option-character. */
748 char c = *d->__nextchar++;
749 char *temp = strchr (optstring, c);
751 /* Increment `optind' when we start to process its last character. */
752 if (*d->__nextchar == '\0')
755 if (temp == NULL || c == ':')
759 #if defined _LIBC && defined USE_IN_LIBIO
764 if (d->__posixly_correct)
766 /* 1003.2 specifies the format of this message. */
767 #if defined _LIBC && defined USE_IN_LIBIO
768 n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
771 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
776 #if defined _LIBC && defined USE_IN_LIBIO
777 n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
780 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
784 #if defined _LIBC && defined USE_IN_LIBIO
787 _IO_flockfile (stderr);
789 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
790 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
792 __fxprintf (NULL, "%s", buf);
794 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
795 _IO_funlockfile (stderr);
804 /* Convenience. Treat POSIX -W foo same as long option --foo */
805 if (temp[0] == 'W' && temp[1] == ';')
808 const struct option *p;
809 const struct option *pfound = NULL;
815 /* This is an option that requires an argument. */
816 if (*d->__nextchar != '\0')
818 d->optarg = d->__nextchar;
819 /* If we end this ARGV-element by taking the rest as an arg,
820 we must advance to the next element now. */
823 else if (d->optind == argc)
827 /* 1003.2 specifies the format of this message. */
828 #if defined _LIBC && defined USE_IN_LIBIO
831 if (__asprintf (&buf,
832 _("%s: option requires an argument -- %c\n"),
835 _IO_flockfile (stderr);
837 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
838 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
840 __fxprintf (NULL, "%s", buf);
842 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
843 _IO_funlockfile (stderr);
848 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
853 if (optstring[0] == ':')
860 /* We already incremented `d->optind' once;
861 increment it again when taking next ARGV-elt as argument. */
862 d->optarg = argv[d->optind++];
864 /* optarg is now the argument, see if it's in the
865 table of longopts. */
867 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
871 /* Test all long options for either exact match
872 or abbreviated matches. */
873 for (p = longopts, option_index = 0; p->name; p++, option_index++)
874 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
876 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
878 /* Exact match found. */
880 indfound = option_index;
884 else if (pfound == NULL)
886 /* First nonexact match found. */
888 indfound = option_index;
891 /* Second or later nonexact match found. */
898 #if defined _LIBC && defined USE_IN_LIBIO
901 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
902 argv[0], argv[d->optind]) >= 0)
904 _IO_flockfile (stderr);
906 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
907 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
909 __fxprintf (NULL, "%s", buf);
911 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
912 _IO_funlockfile (stderr);
917 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
918 argv[0], argv[d->optind]);
921 d->__nextchar += strlen (d->__nextchar);
927 option_index = indfound;
930 /* Don't test has_arg with >, because some C compilers don't
931 allow it to be used on enums. */
933 d->optarg = nameend + 1;
938 #if defined _LIBC && defined USE_IN_LIBIO
941 if (__asprintf (&buf, _("\
942 %s: option `-W %s' doesn't allow an argument\n"),
943 argv[0], pfound->name) >= 0)
945 _IO_flockfile (stderr);
947 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
948 ((_IO_FILE *) stderr)->_flags2
949 |= _IO_FLAGS2_NOTCANCEL;
951 __fxprintf (NULL, "%s", buf);
953 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
954 _IO_funlockfile (stderr);
959 fprintf (stderr, _("\
960 %s: option `-W %s' doesn't allow an argument\n"),
961 argv[0], pfound->name);
965 d->__nextchar += strlen (d->__nextchar);
969 else if (pfound->has_arg == 1)
971 if (d->optind < argc)
972 d->optarg = argv[d->optind++];
977 #if defined _LIBC && defined USE_IN_LIBIO
980 if (__asprintf (&buf, _("\
981 %s: option `%s' requires an argument\n"),
982 argv[0], argv[d->optind - 1]) >= 0)
984 _IO_flockfile (stderr);
986 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
987 ((_IO_FILE *) stderr)->_flags2
988 |= _IO_FLAGS2_NOTCANCEL;
990 __fxprintf (NULL, "%s", buf);
992 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
993 _IO_funlockfile (stderr);
999 _("%s: option `%s' requires an argument\n"),
1000 argv[0], argv[d->optind - 1]);
1003 d->__nextchar += strlen (d->__nextchar);
1004 return optstring[0] == ':' ? ':' : '?';
1007 d->__nextchar += strlen (d->__nextchar);
1008 if (longind != NULL)
1009 *longind = option_index;
1012 *(pfound->flag) = pfound->val;
1017 d->__nextchar = NULL;
1018 return 'W'; /* Let the application handle it. */
1024 /* This is an option that accepts an argument optionally. */
1025 if (*d->__nextchar != '\0')
1027 d->optarg = d->__nextchar;
1032 d->__nextchar = NULL;
1036 /* This is an option that requires an argument. */
1037 if (*d->__nextchar != '\0')
1039 d->optarg = d->__nextchar;
1040 /* If we end this ARGV-element by taking the rest as an arg,
1041 we must advance to the next element now. */
1044 else if (d->optind == argc)
1048 /* 1003.2 specifies the format of this message. */
1049 #if defined _LIBC && defined USE_IN_LIBIO
1052 if (__asprintf (&buf, _("\
1053 %s: option requires an argument -- %c\n"),
1056 _IO_flockfile (stderr);
1058 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1059 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1061 __fxprintf (NULL, "%s", buf);
1063 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1064 _IO_funlockfile (stderr);
1070 _("%s: option requires an argument -- %c\n"),
1075 if (optstring[0] == ':')
1081 /* We already incremented `optind' once;
1082 increment it again when taking next ARGV-elt as argument. */
1083 d->optarg = argv[d->optind++];
1084 d->__nextchar = NULL;
1092 _getopt_internal (int argc, char **argv, const char *optstring,
1093 const struct option *longopts, int *longind,
1094 int long_only, int posixly_correct)
1098 getopt_data.optind = optind;
1099 getopt_data.opterr = opterr;
1101 result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
1102 long_only, posixly_correct, &getopt_data);
1104 optind = getopt_data.optind;
1105 optarg = getopt_data.optarg;
1106 optopt = getopt_data.optopt;
1111 /* glibc gets a LSB-compliant getopt.
1112 Standalone applications get a POSIX-compliant getopt. */
1114 enum { POSIXLY_CORRECT = 0 };
1116 enum { POSIXLY_CORRECT = 1 };
1120 getopt (int argc, char *const *argv, const char *optstring)
1122 return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
1129 /* Compile with -DTEST to make an executable for use in testing
1130 the above definition of `getopt'. */
1133 main (int argc, char **argv)
1136 int digit_optind = 0;
1140 int this_option_optind = optind ? optind : 1;
1142 c = getopt (argc, argv, "abc:d:0123456789");
1158 if (digit_optind != 0 && digit_optind != this_option_optind)
1159 printf ("digits occur in two different argv-elements.\n");
1160 digit_optind = this_option_optind;
1161 printf ("option %c\n", c);
1165 printf ("option a\n");
1169 printf ("option b\n");
1173 printf ("option c with value `%s'\n", optarg);
1180 printf ("?? getopt returned character code 0%o ??\n", c);
1186 printf ("non-option ARGV-elements: ");
1187 while (optind < argc)
1188 printf ("%s ", argv[optind++]);