2 NOTE: getopt is 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-1996,1998-2004,2006,2008-2009 Free Software
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/>. */
37 # define _(msgid) gettext (msgid)
40 #if defined _LIBC && defined USE_IN_LIBIO
44 #ifndef attribute_hidden
45 # define attribute_hidden
48 /* This version of `getopt' appears to the caller like standard Unix `getopt'
49 but it behaves differently for the user, since it allows the user
50 to intersperse the options with the other arguments.
52 As `getopt_long' works, it permutes the elements of ARGV so that,
53 when it is done, all the options precede everything else. Thus
54 all application programs are extended to handle flexible argument order.
56 Using `getopt' or setting the environment variable POSIXLY_CORRECT
58 Then the behavior is completely standard.
60 GNU application programs can use a third alternative mode in which
61 they can distinguish the relative order of options and other arguments. */
63 #include "getopt_int.h"
65 /* For communication from `getopt' to the caller.
66 When `getopt' finds an option that takes an argument,
67 the argument value is returned here.
68 Also, when `ordering' is RETURN_IN_ORDER,
69 each non-option ARGV-element is returned here. */
73 /* Index in ARGV of the next element to be scanned.
74 This is used for communication to and from the caller
75 and for communication between successive calls to `getopt'.
77 On entry to `getopt', zero means this is the first call; initialize.
79 When `getopt' returns -1, this is the index of the first of the
80 non-option elements that the caller should itself scan.
82 Otherwise, `optind' communicates from one call to the next
83 how much of ARGV has been scanned so far. */
85 /* 1003.2 says this must be 1 before any call. */
88 /* Callers store zero here to inhibit the error message
89 for unrecognized options. */
93 /* Set to an option character which was unrecognized.
94 This must be initialized on some systems to avoid linking in the
95 system's own getopt implementation. */
99 /* Keep a global copy of all internal members of getopt_data. */
101 static struct _getopt_data getopt_data;
104 #if defined HAVE_DECL_GETENV && !HAVE_DECL_GETENV
105 extern char *getenv ();
109 /* Stored original parameters.
110 XXX This is no good solution. We should rather copy the args so
111 that we can compare them later. But we must not use malloc(3). */
112 extern int __libc_argc;
113 extern char **__libc_argv;
115 /* Bash 2.0 gives us an environment variable containing flags
116 indicating ARGV elements that should not be considered arguments. */
118 # ifdef USE_NONOPTION_FLAGS
119 /* Defined in getopt_init.c */
120 extern char *__getopt_nonoption_flags;
123 # ifdef USE_NONOPTION_FLAGS
124 # define SWAP_FLAGS(ch1, ch2) \
125 if (d->__nonoption_flags_len > 0) \
127 char __tmp = __getopt_nonoption_flags[ch1]; \
128 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
129 __getopt_nonoption_flags[ch2] = __tmp; \
132 # define SWAP_FLAGS(ch1, ch2)
135 # define SWAP_FLAGS(ch1, ch2)
138 /* Exchange two adjacent subsequences of ARGV.
139 One subsequence is elements [first_nonopt,last_nonopt)
140 which contains all the non-options that have been skipped so far.
141 The other is elements [last_nonopt,optind), which contains all
142 the options processed since those non-options were skipped.
144 `first_nonopt' and `last_nonopt' are relocated so that they describe
145 the new indices of the non-options in ARGV after they are moved. */
148 exchange (char **argv, struct _getopt_data *d)
150 int bottom = d->__first_nonopt;
151 int middle = d->__last_nonopt;
155 /* Exchange the shorter segment with the far end of the longer segment.
156 That puts the shorter segment into the right place.
157 It leaves the longer segment in the right place overall,
158 but it consists of two parts that need to be swapped next. */
160 #if defined _LIBC && defined USE_NONOPTION_FLAGS
161 /* First make sure the handling of the `__getopt_nonoption_flags'
162 string can work normally. Our top argument must be in the range
164 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
166 /* We must extend the array. The user plays games with us and
167 presents new arguments. */
168 char *new_str = malloc (top + 1);
170 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
173 memset (__mempcpy (new_str, __getopt_nonoption_flags,
174 d->__nonoption_flags_max_len),
175 '\0', top + 1 - d->__nonoption_flags_max_len);
176 d->__nonoption_flags_max_len = top + 1;
177 __getopt_nonoption_flags = new_str;
182 while (top > middle && middle > bottom)
184 if (top - middle > middle - bottom)
186 /* Bottom segment is the short one. */
187 int len = middle - bottom;
190 /* Swap it with the top part of the top segment. */
191 for (i = 0; i < len; i++)
193 tem = argv[bottom + i];
194 argv[bottom + i] = argv[top - (middle - bottom) + i];
195 argv[top - (middle - bottom) + i] = tem;
196 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
198 /* Exclude the moved bottom segment from further swapping. */
203 /* Top segment is the short one. */
204 int len = top - middle;
207 /* Swap it with the bottom part of the bottom segment. */
208 for (i = 0; i < len; i++)
210 tem = argv[bottom + i];
211 argv[bottom + i] = argv[middle + i];
212 argv[middle + i] = tem;
213 SWAP_FLAGS (bottom + i, middle + i);
215 /* Exclude the moved top segment from further swapping. */
220 /* Update records for the slots the non-options now occupy. */
222 d->__first_nonopt += (d->optind - d->__last_nonopt);
223 d->__last_nonopt = d->optind;
226 /* Initialize the internal data when the first call is made. */
229 _getopt_initialize (int argc _UNUSED_PARAMETER_,
230 char **argv _UNUSED_PARAMETER_, const char *optstring,
231 struct _getopt_data *d, int posixly_correct)
233 /* Start processing options with ARGV-element 1 (since ARGV-element 0
234 is the program name); the sequence of previously skipped
235 non-option ARGV-elements is empty. */
237 d->__first_nonopt = d->__last_nonopt = d->optind;
239 d->__nextchar = NULL;
241 d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
243 /* Determine how to handle the ordering of options and nonoptions. */
245 if (optstring[0] == '-')
247 d->__ordering = RETURN_IN_ORDER;
250 else if (optstring[0] == '+')
252 d->__ordering = REQUIRE_ORDER;
255 else if (d->__posixly_correct)
256 d->__ordering = REQUIRE_ORDER;
258 d->__ordering = PERMUTE;
260 #if defined _LIBC && defined USE_NONOPTION_FLAGS
261 if (!d->__posixly_correct
262 && argc == __libc_argc && argv == __libc_argv)
264 if (d->__nonoption_flags_max_len == 0)
266 if (__getopt_nonoption_flags == NULL
267 || __getopt_nonoption_flags[0] == '\0')
268 d->__nonoption_flags_max_len = -1;
271 const char *orig_str = __getopt_nonoption_flags;
272 int len = d->__nonoption_flags_max_len = strlen (orig_str);
273 if (d->__nonoption_flags_max_len < argc)
274 d->__nonoption_flags_max_len = argc;
275 __getopt_nonoption_flags =
276 (char *) malloc (d->__nonoption_flags_max_len);
277 if (__getopt_nonoption_flags == NULL)
278 d->__nonoption_flags_max_len = -1;
280 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
281 '\0', d->__nonoption_flags_max_len - len);
284 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
287 d->__nonoption_flags_len = 0;
293 /* Scan elements of ARGV (whose length is ARGC) for option characters
296 If an element of ARGV starts with '-', and is not exactly "-" or "--",
297 then it is an option element. The characters of this element
298 (aside from the initial '-') are option characters. If `getopt'
299 is called repeatedly, it returns successively each of the option characters
300 from each of the option elements.
302 If `getopt' finds another option character, it returns that character,
303 updating `optind' and `nextchar' so that the next call to `getopt' can
304 resume the scan with the following option character or ARGV-element.
306 If there are no more option characters, `getopt' returns -1.
307 Then `optind' is the index in ARGV of the first ARGV-element
308 that is not an option. (The ARGV-elements have been permuted
309 so that those that are not options now come last.)
311 OPTSTRING is a string containing the legitimate option characters.
312 If an option character is seen that is not listed in OPTSTRING,
313 return '?' after printing an error message. If you set `opterr' to
314 zero, the error message is suppressed but we still return '?'.
316 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
317 so the following text in the same ARGV-element, or the text of the following
318 ARGV-element, is returned in `optarg'. Two colons mean an option that
319 wants an optional arg; if there is text in the current ARGV-element,
320 it is returned in `optarg', otherwise `optarg' is set to zero.
322 If OPTSTRING starts with `-' or `+', it requests different methods of
323 handling the non-option ARGV-elements.
324 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
326 Long-named options begin with `--' instead of `-'.
327 Their names may be abbreviated as long as the abbreviation is unique
328 or is an exact match for some defined option. If they have an
329 argument, it follows the option name in the same ARGV-element, separated
330 from the option name by a `=', or else the in next ARGV-element.
331 When `getopt' finds a long-named option, it returns 0 if that option's
332 `flag' field is nonzero, the value of the option's `val' field
333 if the `flag' field is zero.
335 The elements of ARGV aren't really const, because we permute them.
336 But we pretend they're const in the prototype to be compatible
339 LONGOPTS is a vector of `struct option' terminated by an
340 element containing a name which is zero.
342 LONGIND returns the index in LONGOPT of the long-named option found.
343 It is only valid when a long-named option has been found by the most
346 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
347 long-named options. */
350 _getopt_internal_r (int argc, char **argv, const char *optstring,
351 const struct option *longopts, int *longind,
352 int long_only, struct _getopt_data *d, int posixly_correct)
354 int print_errors = d->opterr;
355 if (optstring[0] == ':')
363 if (d->optind == 0 || !d->__initialized)
366 d->optind = 1; /* Don't scan ARGV[0], the program name. */
367 optstring = _getopt_initialize (argc, argv, optstring, d,
369 d->__initialized = 1;
372 /* Test whether ARGV[optind] points to a non-option argument.
373 Either it does not have option syntax, or there is an environment flag
374 from the shell indicating it is not an option. The later information
375 is only used when the used in the GNU libc. */
376 #if defined _LIBC && defined USE_NONOPTION_FLAGS
377 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
378 || (d->optind < d->__nonoption_flags_len \
379 && __getopt_nonoption_flags[d->optind] == '1'))
381 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
384 if (d->__nextchar == NULL || *d->__nextchar == '\0')
386 /* Advance to the next ARGV-element. */
388 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
389 moved back by the user (who may also have changed the arguments). */
390 if (d->__last_nonopt > d->optind)
391 d->__last_nonopt = d->optind;
392 if (d->__first_nonopt > d->optind)
393 d->__first_nonopt = d->optind;
395 if (d->__ordering == PERMUTE)
397 /* If we have just processed some options following some non-options,
398 exchange them so that the options come first. */
400 if (d->__first_nonopt != d->__last_nonopt
401 && d->__last_nonopt != d->optind)
402 exchange ((char **) argv, d);
403 else if (d->__last_nonopt != d->optind)
404 d->__first_nonopt = d->optind;
406 /* Skip any additional non-options
407 and extend the range of non-options previously skipped. */
409 while (d->optind < argc && NONOPTION_P)
411 d->__last_nonopt = d->optind;
414 /* The special ARGV-element `--' means premature end of options.
415 Skip it like a null option,
416 then exchange with previous non-options as if it were an option,
417 then skip everything else like a non-option. */
419 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
423 if (d->__first_nonopt != d->__last_nonopt
424 && d->__last_nonopt != d->optind)
425 exchange ((char **) argv, d);
426 else if (d->__first_nonopt == d->__last_nonopt)
427 d->__first_nonopt = d->optind;
428 d->__last_nonopt = argc;
433 /* If we have done all the ARGV-elements, stop the scan
434 and back over any non-options that we skipped and permuted. */
436 if (d->optind == argc)
438 /* Set the next-arg-index to point at the non-options
439 that we previously skipped, so the caller will digest them. */
440 if (d->__first_nonopt != d->__last_nonopt)
441 d->optind = d->__first_nonopt;
445 /* If we have come to a non-option and did not permute it,
446 either stop the scan or describe it to the caller and pass it by. */
450 if (d->__ordering == REQUIRE_ORDER)
452 d->optarg = argv[d->optind++];
456 /* We have found another option-ARGV-element.
457 Skip the initial punctuation. */
459 d->__nextchar = (argv[d->optind] + 1
460 + (longopts != NULL && argv[d->optind][1] == '-'));
463 /* Decode the current option-ARGV-element. */
465 /* Check whether the ARGV-element is a long option.
467 If long_only and the ARGV-element has the form "-f", where f is
468 a valid short option, don't consider it an abbreviated form of
469 a long option that starts with f. Otherwise there would be no
470 way to give the -f short option.
472 On the other hand, if there's a long option "fubar" and
473 the ARGV-element is "-fu", do consider that an abbreviation of
474 the long option, just like "--fu", and not "-f" with arg "u".
476 This distinction seems to be the most useful approach. */
479 && (argv[d->optind][1] == '-'
480 || (long_only && (argv[d->optind][2]
481 || !strchr (optstring, argv[d->optind][1])))))
484 const struct option *p;
485 const struct option *pfound = NULL;
491 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
494 /* Test all long options for either exact match
495 or abbreviated matches. */
496 for (p = longopts, option_index = 0; p->name; p++, option_index++)
497 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
499 if ((unsigned int) (nameend - d->__nextchar)
500 == (unsigned int) strlen (p->name))
502 /* Exact match found. */
504 indfound = option_index;
508 else if (pfound == NULL)
510 /* First nonexact match found. */
512 indfound = option_index;
515 || pfound->has_arg != p->has_arg
516 || pfound->flag != p->flag
517 || pfound->val != p->val)
518 /* Second or later nonexact match found. */
526 #if defined _LIBC && defined USE_IN_LIBIO
529 if (__asprintf (&buf, _("%s: option '%s' is ambiguous\n"),
530 argv[0], argv[d->optind]) >= 0)
532 _IO_flockfile (stderr);
534 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
535 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
537 __fxprintf (NULL, "%s", buf);
539 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
540 _IO_funlockfile (stderr);
545 fprintf (stderr, _("%s: option '%s' is ambiguous\n"),
546 argv[0], argv[d->optind]);
549 d->__nextchar += strlen (d->__nextchar);
557 option_index = indfound;
561 /* Don't test has_arg with >, because some C compilers don't
562 allow it to be used on enums. */
564 d->optarg = nameend + 1;
569 #if defined _LIBC && defined USE_IN_LIBIO
574 if (argv[d->optind - 1][1] == '-')
577 #if defined _LIBC && defined USE_IN_LIBIO
578 n = __asprintf (&buf, _("\
579 %s: option '--%s' doesn't allow an argument\n"),
580 argv[0], pfound->name);
582 fprintf (stderr, _("\
583 %s: option '--%s' doesn't allow an argument\n"),
584 argv[0], pfound->name);
589 /* +option or -option */
590 #if defined _LIBC && defined USE_IN_LIBIO
591 n = __asprintf (&buf, _("\
592 %s: option '%c%s' doesn't allow an argument\n"),
593 argv[0], argv[d->optind - 1][0],
596 fprintf (stderr, _("\
597 %s: option '%c%s' doesn't allow an argument\n"),
598 argv[0], argv[d->optind - 1][0],
603 #if defined _LIBC && defined USE_IN_LIBIO
606 _IO_flockfile (stderr);
608 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
609 ((_IO_FILE *) stderr)->_flags2
610 |= _IO_FLAGS2_NOTCANCEL;
612 __fxprintf (NULL, "%s", buf);
614 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
615 _IO_funlockfile (stderr);
622 d->__nextchar += strlen (d->__nextchar);
624 d->optopt = pfound->val;
628 else if (pfound->has_arg == 1)
630 if (d->optind < argc)
631 d->optarg = argv[d->optind++];
636 #if defined _LIBC && defined USE_IN_LIBIO
639 if (__asprintf (&buf, _("\
640 %s: option '%s' requires an argument\n"),
641 argv[0], argv[d->optind - 1]) >= 0)
643 _IO_flockfile (stderr);
645 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
646 ((_IO_FILE *) stderr)->_flags2
647 |= _IO_FLAGS2_NOTCANCEL;
649 __fxprintf (NULL, "%s", buf);
651 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
652 _IO_funlockfile (stderr);
658 _("%s: option '%s' requires an argument\n"),
659 argv[0], argv[d->optind - 1]);
662 d->__nextchar += strlen (d->__nextchar);
663 d->optopt = pfound->val;
664 return optstring[0] == ':' ? ':' : '?';
667 d->__nextchar += strlen (d->__nextchar);
669 *longind = option_index;
672 *(pfound->flag) = pfound->val;
678 /* Can't find it as a long option. If this is not getopt_long_only,
679 or the option starts with '--' or is not a valid short
680 option, then it's an error.
681 Otherwise interpret it as a short option. */
682 if (!long_only || argv[d->optind][1] == '-'
683 || strchr (optstring, *d->__nextchar) == NULL)
687 #if defined _LIBC && defined USE_IN_LIBIO
692 if (argv[d->optind][1] == '-')
695 #if defined _LIBC && defined USE_IN_LIBIO
696 n = __asprintf (&buf, _("%s: unrecognized option '--%s'\n"),
697 argv[0], d->__nextchar);
699 fprintf (stderr, _("%s: unrecognized option '--%s'\n"),
700 argv[0], d->__nextchar);
705 /* +option or -option */
706 #if defined _LIBC && defined USE_IN_LIBIO
707 n = __asprintf (&buf, _("%s: unrecognized option '%c%s'\n"),
708 argv[0], argv[d->optind][0], d->__nextchar);
710 fprintf (stderr, _("%s: unrecognized option '%c%s'\n"),
711 argv[0], argv[d->optind][0], d->__nextchar);
715 #if defined _LIBC && defined USE_IN_LIBIO
718 _IO_flockfile (stderr);
720 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
721 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
723 __fxprintf (NULL, "%s", buf);
725 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
726 _IO_funlockfile (stderr);
732 d->__nextchar = (char *) "";
739 /* Look at and handle the next short option-character. */
742 char c = *d->__nextchar++;
743 char *temp = strchr (optstring, c);
745 /* Increment `optind' when we start to process its last character. */
746 if (*d->__nextchar == '\0')
749 if (temp == NULL || c == ':')
753 #if defined _LIBC && defined USE_IN_LIBIO
758 #if defined _LIBC && defined USE_IN_LIBIO
759 n = __asprintf (&buf, _("%s: invalid option -- '%c'\n"),
762 fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
765 #if defined _LIBC && defined USE_IN_LIBIO
768 _IO_flockfile (stderr);
770 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
771 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
773 __fxprintf (NULL, "%s", buf);
775 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
776 _IO_funlockfile (stderr);
785 /* Convenience. Treat POSIX -W foo same as long option --foo */
786 if (temp[0] == 'W' && temp[1] == ';')
789 const struct option *p;
790 const struct option *pfound = NULL;
796 /* This is an option that requires an argument. */
797 if (*d->__nextchar != '\0')
799 d->optarg = d->__nextchar;
800 /* If we end this ARGV-element by taking the rest as an arg,
801 we must advance to the next element now. */
804 else if (d->optind == argc)
808 #if defined _LIBC && defined USE_IN_LIBIO
811 if (__asprintf (&buf,
812 _("%s: option requires an argument -- '%c'\n"),
815 _IO_flockfile (stderr);
817 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
818 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
820 __fxprintf (NULL, "%s", buf);
822 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
823 _IO_funlockfile (stderr);
829 _("%s: option requires an argument -- '%c'\n"),
834 if (optstring[0] == ':')
841 /* We already incremented `d->optind' once;
842 increment it again when taking next ARGV-elt as argument. */
843 d->optarg = argv[d->optind++];
845 /* optarg is now the argument, see if it's in the
846 table of longopts. */
848 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
852 /* Test all long options for either exact match
853 or abbreviated matches. */
854 for (p = longopts, option_index = 0; p->name; p++, option_index++)
855 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
857 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
859 /* Exact match found. */
861 indfound = option_index;
865 else if (pfound == NULL)
867 /* First nonexact match found. */
869 indfound = option_index;
872 /* Second or later nonexact match found. */
879 #if defined _LIBC && defined USE_IN_LIBIO
882 if (__asprintf (&buf, _("%s: option '-W %s' is ambiguous\n"),
883 argv[0], argv[d->optind]) >= 0)
885 _IO_flockfile (stderr);
887 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
888 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
890 __fxprintf (NULL, "%s", buf);
892 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
893 _IO_funlockfile (stderr);
898 fprintf (stderr, _("%s: option '-W %s' is ambiguous\n"),
899 argv[0], argv[d->optind]);
902 d->__nextchar += strlen (d->__nextchar);
908 option_index = indfound;
911 /* Don't test has_arg with >, because some C compilers don't
912 allow it to be used on enums. */
914 d->optarg = nameend + 1;
919 #if defined _LIBC && defined USE_IN_LIBIO
922 if (__asprintf (&buf, _("\
923 %s: option '-W %s' doesn't allow an argument\n"),
924 argv[0], pfound->name) >= 0)
926 _IO_flockfile (stderr);
928 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
929 ((_IO_FILE *) stderr)->_flags2
930 |= _IO_FLAGS2_NOTCANCEL;
932 __fxprintf (NULL, "%s", buf);
934 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
935 _IO_funlockfile (stderr);
940 fprintf (stderr, _("\
941 %s: option '-W %s' doesn't allow an argument\n"),
942 argv[0], pfound->name);
946 d->__nextchar += strlen (d->__nextchar);
950 else if (pfound->has_arg == 1)
952 if (d->optind < argc)
953 d->optarg = argv[d->optind++];
958 #if defined _LIBC && defined USE_IN_LIBIO
961 if (__asprintf (&buf, _("\
962 %s: option '%s' requires an argument\n"),
963 argv[0], argv[d->optind - 1]) >= 0)
965 _IO_flockfile (stderr);
967 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
968 ((_IO_FILE *) stderr)->_flags2
969 |= _IO_FLAGS2_NOTCANCEL;
971 __fxprintf (NULL, "%s", buf);
973 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
974 _IO_funlockfile (stderr);
980 _("%s: option '%s' requires an argument\n"),
981 argv[0], argv[d->optind - 1]);
984 d->__nextchar += strlen (d->__nextchar);
985 return optstring[0] == ':' ? ':' : '?';
988 d->__nextchar += strlen (d->__nextchar);
990 *longind = option_index;
993 *(pfound->flag) = pfound->val;
998 d->__nextchar = NULL;
999 return 'W'; /* Let the application handle it. */
1005 /* This is an option that accepts an argument optionally. */
1006 if (*d->__nextchar != '\0')
1008 d->optarg = d->__nextchar;
1013 d->__nextchar = NULL;
1017 /* This is an option that requires an argument. */
1018 if (*d->__nextchar != '\0')
1020 d->optarg = d->__nextchar;
1021 /* If we end this ARGV-element by taking the rest as an arg,
1022 we must advance to the next element now. */
1025 else if (d->optind == argc)
1029 #if defined _LIBC && defined USE_IN_LIBIO
1032 if (__asprintf (&buf, _("\
1033 %s: option requires an argument -- '%c'\n"),
1036 _IO_flockfile (stderr);
1038 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1039 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1041 __fxprintf (NULL, "%s", buf);
1043 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1044 _IO_funlockfile (stderr);
1050 _("%s: option requires an argument -- '%c'\n"),
1055 if (optstring[0] == ':')
1061 /* We already incremented `optind' once;
1062 increment it again when taking next ARGV-elt as argument. */
1063 d->optarg = argv[d->optind++];
1064 d->__nextchar = NULL;
1072 _getopt_internal (int argc, char **argv, const char *optstring,
1073 const struct option *longopts, int *longind, int long_only,
1074 int posixly_correct)
1078 getopt_data.optind = optind;
1079 getopt_data.opterr = opterr;
1081 result = _getopt_internal_r (argc, argv, optstring, longopts,
1082 longind, long_only, &getopt_data,
1085 optind = getopt_data.optind;
1086 optarg = getopt_data.optarg;
1087 optopt = getopt_data.optopt;
1092 /* glibc gets a LSB-compliant getopt.
1093 Standalone applications get a POSIX-compliant getopt. */
1095 enum { POSIXLY_CORRECT = 0 };
1097 enum { POSIXLY_CORRECT = 1 };
1101 getopt (int argc, char *const *argv, const char *optstring)
1103 return _getopt_internal (argc, (char **) argv, optstring,
1104 (const struct option *) 0,
1106 0, POSIXLY_CORRECT);
1111 __posix_getopt (int argc, char *const *argv, const char *optstring)
1113 return _getopt_internal (argc, argv, optstring,
1114 (const struct option *) 0,
1123 /* Compile with -DTEST to make an executable for use in testing
1124 the above definition of `getopt'. */
1127 main (int argc, char **argv)
1130 int digit_optind = 0;
1134 int this_option_optind = optind ? optind : 1;
1136 c = getopt (argc, argv, "abc:d:0123456789");
1152 if (digit_optind != 0 && digit_optind != this_option_optind)
1153 printf ("digits occur in two different argv-elements.\n");
1154 digit_optind = this_option_optind;
1155 printf ("option %c\n", c);
1159 printf ("option a\n");
1163 printf ("option b\n");
1167 printf ("option c with value '%s'\n", optarg);
1174 printf ("?? getopt returned character code 0%o ??\n", c);
1180 printf ("non-option ARGV-elements: ");
1181 while (optind < argc)
1182 printf ("%s ", argv[optind++]);