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
6 Free Software Foundation, Inc.
7 This file is part of the GNU C Library.
9 The GNU C Library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
14 The GNU C Library 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 GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with the GNU C Library; if not, see
21 <http://www.gnu.org/licenses/>. */
24 * Modified for uClibc by Manuel Novoa III on 1/5/01.
25 * Modified once again for uClibc by Erik Andersen 8/7/02
28 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
29 Ditto for AIX 3.2 and <stdlib.h>. */
40 /* Comment out all this code if we are using the GNU C Library, and are not
41 actually compiling the library itself. This code is part of the GNU C
42 Library, but also included in many other GNU distributions. Compiling
43 and linking in this code is a waste when using the GNU C library
44 (especially if it is a shared library). Rather than having every GNU
45 program understand `configure --with-gnu-libc' and omit the object files,
46 it is simpler to just do this in the source for each such file. */
48 #define GETOPT_INTERFACE_VERSION 2
49 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
50 # include <gnu-versions.h>
51 # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
59 /* This needs to come after some library #include
60 to get __GNU_LIBRARY__ defined. */
61 #if defined __GNU_LIBRARY__ || defined __UCLIBC__
62 /* Don't include stdlib.h for non-GNU C libraries because some of them
63 contain conflicting prototypes for getopt. */
66 #endif /* GNU C library. */
78 # define _(msgid) gettext (msgid)
81 /* Treat '-W foo' the same as the long option '--foo',
82 * disabled for the moment since it costs about 2k... */
83 #undef SPECIAL_TREATMENT_FOR_W
85 #if defined _LIBC && defined USE_IN_LIBIO
89 #ifndef attribute_hidden
90 # define attribute_hidden
93 /* This version of `getopt' appears to the caller like standard Unix `getopt'
94 but it behaves differently for the user, since it allows the user
95 to intersperse the options with the other arguments.
97 As `getopt' works, it permutes the elements of ARGV so that,
98 when it is done, all the options precede everything else. Thus
99 all application programs are extended to handle flexible argument order.
101 Setting the environment variable POSIXLY_CORRECT disables permutation.
102 Then the behavior is completely standard.
104 GNU application programs can use a third alternative mode in which
105 they can distinguish the relative order of options and other arguments. */
108 #include "getopt_int.h"
111 /* For communication from `getopt' to the caller.
112 When `getopt' finds an option that takes an argument,
113 the argument value is returned here.
114 Also, when `ordering' is RETURN_IN_ORDER,
115 each non-option ARGV-element is returned here. */
119 /* Index in ARGV of the next element to be scanned.
120 This is used for communication to and from the caller
121 and for communication between successive calls to `getopt'.
123 On entry to `getopt', zero means this is the first call; initialize.
125 When `getopt' returns -1, this is the index of the first of the
126 non-option elements that the caller should itself scan.
128 Otherwise, `optind' communicates from one call to the next
129 how much of ARGV has been scanned so far. */
131 /* 1003.2 says this must be 1 before any call. */
134 /* Callers store zero here to inhibit the error message
135 for unrecognized options. */
139 /* Set to an option character which was unrecognized.
140 This must be initialized on some systems to avoid linking in the
141 system's own getopt implementation. */
145 /* Keep a global copy of all internal members of getopt_data. */
147 static struct _getopt_data getopt_data;
150 #if !defined __GNU_LIBRARY__ && !defined __UCLIBC__
152 /* Avoid depending on library functions or files
153 whose names are inconsistent. */
156 extern char *getenv ();
159 #endif /* not __GNU_LIBRARY__ */
162 /* Stored original parameters.
163 XXX This is no good solution. We should rather copy the args so
164 that we can compare them later. But we must not use malloc(3). */
165 # ifdef USE_NONOPTION_FLAGS
166 extern int __libc_argc;
167 extern char **__libc_argv;
169 /* Bash 2.0 gives us an environment variable containing flags
170 indicating ARGV elements that should not be considered arguments. */
172 /* Defined in getopt_init.c */
173 extern char *__getopt_nonoption_flags;
175 # define SWAP_FLAGS(ch1, ch2) \
176 if (d->__nonoption_flags_len > 0) \
178 char __tmp = __getopt_nonoption_flags[ch1]; \
179 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
180 __getopt_nonoption_flags[ch2] = __tmp; \
183 # define SWAP_FLAGS(ch1, ch2)
186 # define SWAP_FLAGS(ch1, ch2)
189 /* Exchange two adjacent subsequences of ARGV.
190 One subsequence is elements [first_nonopt,last_nonopt)
191 which contains all the non-options that have been skipped so far.
192 The other is elements [last_nonopt,optind), which contains all
193 the options processed since those non-options were skipped.
195 `first_nonopt' and `last_nonopt' are relocated so that they describe
196 the new indices of the non-options in ARGV after they are moved. */
199 exchange (char **argv, struct _getopt_data *d)
201 int bottom = d->__first_nonopt;
202 int middle = d->__last_nonopt;
206 /* Exchange the shorter segment with the far end of the longer segment.
207 That puts the shorter segment into the right place.
208 It leaves the longer segment in the right place overall,
209 but it consists of two parts that need to be swapped next. */
211 #if defined _LIBC && defined USE_NONOPTION_FLAGS
212 /* First make sure the handling of the `__getopt_nonoption_flags'
213 string can work normally. Our top argument must be in the range
215 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
217 /* We must extend the array. The user plays games with us and
218 presents new arguments. */
219 char *new_str = malloc (top + 1);
221 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
224 memset (mempcpy (new_str, __getopt_nonoption_flags,
225 d->__nonoption_flags_max_len),
226 '\0', top + 1 - d->__nonoption_flags_max_len);
227 d->__nonoption_flags_max_len = top + 1;
228 __getopt_nonoption_flags = new_str;
233 while (top > middle && middle > bottom)
235 if (top - middle > middle - bottom)
237 /* Bottom segment is the short one. */
238 int len = middle - bottom;
241 /* Swap it with the top part of the top segment. */
242 for (i = 0; i < len; i++)
244 tem = argv[bottom + i];
245 argv[bottom + i] = argv[top - (middle - bottom) + i];
246 argv[top - (middle - bottom) + i] = tem;
247 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
249 /* Exclude the moved bottom segment from further swapping. */
254 /* Top segment is the short one. */
255 int len = top - middle;
258 /* Swap it with the bottom part of the bottom segment. */
259 for (i = 0; i < len; i++)
261 tem = argv[bottom + i];
262 argv[bottom + i] = argv[middle + i];
263 argv[middle + i] = tem;
264 SWAP_FLAGS (bottom + i, middle + i);
266 /* Exclude the moved top segment from further swapping. */
271 /* Update records for the slots the non-options now occupy. */
273 d->__first_nonopt += (d->optind - d->__last_nonopt);
274 d->__last_nonopt = d->optind;
277 /* Initialize the internal data when the first call is made. */
280 _getopt_initialize (attribute_unused int argc, attribute_unused char *const *argv, const char *optstring,
281 struct _getopt_data *d)
283 /* Start processing options with ARGV-element 1 (since ARGV-element 0
284 is the program name); the sequence of previously skipped
285 non-option ARGV-elements is empty. */
287 d->__first_nonopt = d->__last_nonopt = d->optind;
289 d->__nextchar = NULL;
291 d->__posixly_correct = !!getenv ("POSIXLY_CORRECT");
293 /* Determine how to handle the ordering of options and nonoptions. */
295 if (optstring[0] == '-')
297 d->__ordering = RETURN_IN_ORDER;
300 else if (optstring[0] == '+')
302 d->__ordering = REQUIRE_ORDER;
305 else if (d->__posixly_correct)
306 d->__ordering = REQUIRE_ORDER;
308 d->__ordering = PERMUTE;
310 #if defined _LIBC && defined USE_NONOPTION_FLAGS
311 if (!d->__posixly_correct
312 && argc == __libc_argc && argv == __libc_argv)
314 if (d->__nonoption_flags_max_len == 0)
316 if (__getopt_nonoption_flags == NULL
317 || __getopt_nonoption_flags[0] == '\0')
318 d->__nonoption_flags_max_len = -1;
321 const char *orig_str = __getopt_nonoption_flags;
322 int len = d->__nonoption_flags_max_len = strlen (orig_str);
323 if (d->__nonoption_flags_max_len < argc)
324 d->__nonoption_flags_max_len = argc;
325 __getopt_nonoption_flags =
326 (char *) malloc (d->__nonoption_flags_max_len);
327 if (__getopt_nonoption_flags == NULL)
328 d->__nonoption_flags_max_len = -1;
330 memset (mempcpy (__getopt_nonoption_flags, orig_str, len),
331 '\0', d->__nonoption_flags_max_len - len);
334 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
337 d->__nonoption_flags_len = 0;
343 /* Scan elements of ARGV (whose length is ARGC) for option characters
346 If an element of ARGV starts with '-', and is not exactly "-" or "--",
347 then it is an option element. The characters of this element
348 (aside from the initial '-') are option characters. If `getopt'
349 is called repeatedly, it returns successively each of the option characters
350 from each of the option elements.
352 If `getopt' finds another option character, it returns that character,
353 updating `optind' and `nextchar' so that the next call to `getopt' can
354 resume the scan with the following option character or ARGV-element.
356 If there are no more option characters, `getopt' returns -1.
357 Then `optind' is the index in ARGV of the first ARGV-element
358 that is not an option. (The ARGV-elements have been permuted
359 so that those that are not options now come last.)
361 OPTSTRING is a string containing the legitimate option characters.
362 If an option character is seen that is not listed in OPTSTRING,
363 return '?' after printing an error message. If you set `opterr' to
364 zero, the error message is suppressed but we still return '?'.
366 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
367 so the following text in the same ARGV-element, or the text of the following
368 ARGV-element, is returned in `optarg'. Two colons mean an option that
369 wants an optional arg; if there is text in the current ARGV-element,
370 it is returned in `optarg', otherwise `optarg' is set to zero.
372 If OPTSTRING starts with `-' or `+', it requests different methods of
373 handling the non-option ARGV-elements.
374 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
376 Long-named options begin with `--' instead of `-'.
377 Their names may be abbreviated as long as the abbreviation is unique
378 or is an exact match for some defined option. If they have an
379 argument, it follows the option name in the same ARGV-element, separated
380 from the option name by a `=', or else the in next ARGV-element.
381 When `getopt' finds a long-named option, it returns 0 if that option's
382 `flag' field is nonzero, the value of the option's `val' field
383 if the `flag' field is zero.
385 The elements of ARGV aren't really const, because we permute them.
386 But we pretend they're const in the prototype to be compatible
389 LONGOPTS is a vector of `struct option' terminated by an
390 element containing a name which is zero.
392 LONGIND returns the index in LONGOPT of the long-named option found.
393 It is only valid when a long-named option has been found by the most
396 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
397 long-named options. */
400 _getopt_internal_r (int argc, char *const *argv, const char *optstring,
401 const struct option *longopts, int *longind,
402 int long_only, struct _getopt_data *d)
404 int print_errors = d->opterr;
405 if (optstring[0] == ':')
413 if (d->optind == 0 || !d->__initialized)
416 d->optind = 1; /* Don't scan ARGV[0], the program name. */
417 optstring = _getopt_initialize (argc, argv, optstring, d);
418 d->__initialized = 1;
421 /* Test whether ARGV[optind] points to a non-option argument.
422 Either it does not have option syntax, or there is an environment flag
423 from the shell indicating it is not an option. The later information
424 is only used when the used in the GNU libc. */
425 #if defined _LIBC && defined USE_NONOPTION_FLAGS
426 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
427 || (d->optind < d->__nonoption_flags_len \
428 && __getopt_nonoption_flags[d->optind] == '1'))
430 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
433 if (d->__nextchar == NULL || *d->__nextchar == '\0')
435 /* Advance to the next ARGV-element. */
437 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
438 moved back by the user (who may also have changed the arguments). */
439 if (d->__last_nonopt > d->optind)
440 d->__last_nonopt = d->optind;
441 if (d->__first_nonopt > d->optind)
442 d->__first_nonopt = d->optind;
444 if (d->__ordering == PERMUTE)
446 /* If we have just processed some options following some non-options,
447 exchange them so that the options come first. */
449 if (d->__first_nonopt != d->__last_nonopt
450 && d->__last_nonopt != d->optind)
451 exchange ((char **) argv, d);
452 else if (d->__last_nonopt != d->optind)
453 d->__first_nonopt = d->optind;
455 /* Skip any additional non-options
456 and extend the range of non-options previously skipped. */
458 while (d->optind < argc && NONOPTION_P)
460 d->__last_nonopt = d->optind;
463 /* The special ARGV-element `--' means premature end of options.
464 Skip it like a null option,
465 then exchange with previous non-options as if it were an option,
466 then skip everything else like a non-option. */
468 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
472 if (d->__first_nonopt != d->__last_nonopt
473 && d->__last_nonopt != d->optind)
474 exchange ((char **) argv, d);
475 else if (d->__first_nonopt == d->__last_nonopt)
476 d->__first_nonopt = d->optind;
477 d->__last_nonopt = argc;
482 /* If we have done all the ARGV-elements, stop the scan
483 and back over any non-options that we skipped and permuted. */
485 if (d->optind == argc)
487 /* Set the next-arg-index to point at the non-options
488 that we previously skipped, so the caller will digest them. */
489 if (d->__first_nonopt != d->__last_nonopt)
490 d->optind = d->__first_nonopt;
494 /* If we have come to a non-option and did not permute it,
495 either stop the scan or describe it to the caller and pass it by. */
499 if (d->__ordering == REQUIRE_ORDER)
501 d->optarg = argv[d->optind++];
505 /* We have found another option-ARGV-element.
506 Skip the initial punctuation. */
508 d->__nextchar = (argv[d->optind] + 1
509 + (longopts != NULL && argv[d->optind][1] == '-'));
512 /* Decode the current option-ARGV-element. */
514 /* Check whether the ARGV-element is a long option.
516 If long_only and the ARGV-element has the form "-f", where f is
517 a valid short option, don't consider it an abbreviated form of
518 a long option that starts with f. Otherwise there would be no
519 way to give the -f short option.
521 On the other hand, if there's a long option "fubar" and
522 the ARGV-element is "-fu", do consider that an abbreviation of
523 the long option, just like "--fu", and not "-f" with arg "u".
525 This distinction seems to be the most useful approach. */
528 && (argv[d->optind][1] == '-'
529 || (long_only && (argv[d->optind][2]
530 || !strchr (optstring, argv[d->optind][1])))))
533 const struct option *p;
534 const struct option *pfound = NULL;
540 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
543 /* Test all long options for either exact match
544 or abbreviated matches. */
545 for (p = longopts, option_index = 0; p->name; p++, option_index++)
546 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
548 if ((unsigned int) (nameend - d->__nextchar)
549 == (unsigned int) strlen (p->name))
551 /* Exact match found. */
553 indfound = option_index;
557 else if (pfound == NULL)
559 /* First nonexact match found. */
561 indfound = option_index;
564 || pfound->has_arg != p->has_arg
565 || pfound->flag != p->flag
566 || pfound->val != p->val)
567 /* Second or later nonexact match found. */
575 #if defined _LIBC && defined USE_IN_LIBIO
578 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
579 argv[0], argv[d->optind]) >= 0)
581 _IO_flockfile (stderr);
583 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
584 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
586 __fxprintf (NULL, "%s", buf);
588 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
589 _IO_funlockfile (stderr);
594 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
595 argv[0], argv[d->optind]);
598 d->__nextchar += strlen (d->__nextchar);
606 option_index = indfound;
610 /* Don't test has_arg with >, because some C compilers don't
611 allow it to be used on enums. */
613 d->optarg = nameend + 1;
618 #if defined _LIBC && defined USE_IN_LIBIO
623 if (argv[d->optind - 1][1] == '-')
626 #if defined _LIBC && defined USE_IN_LIBIO
627 n = __asprintf (&buf, _("\
628 %s: option `--%s' doesn't allow an argument\n"),
629 argv[0], pfound->name);
631 fprintf (stderr, _("\
632 %s: option `--%s' doesn't allow an argument\n"),
633 argv[0], pfound->name);
638 /* +option or -option */
639 #if defined _LIBC && defined USE_IN_LIBIO
640 n = __asprintf (&buf, _("\
641 %s: option `%c%s' doesn't allow an argument\n"),
642 argv[0], argv[d->optind - 1][0],
645 fprintf (stderr, _("\
646 %s: option `%c%s' doesn't allow an argument\n"),
647 argv[0], argv[d->optind - 1][0],
652 #if defined _LIBC && defined USE_IN_LIBIO
655 _IO_flockfile (stderr);
657 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
658 ((_IO_FILE *) stderr)->_flags2
659 |= _IO_FLAGS2_NOTCANCEL;
661 __fxprintf (NULL, "%s", buf);
663 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
664 _IO_funlockfile (stderr);
671 d->__nextchar += strlen (d->__nextchar);
673 d->optopt = pfound->val;
677 else if (pfound->has_arg == 1)
679 if (d->optind < argc)
680 d->optarg = argv[d->optind++];
685 #if defined _LIBC && defined USE_IN_LIBIO
688 if (__asprintf (&buf, _("\
689 %s: option `%s' requires an argument\n"),
690 argv[0], argv[d->optind - 1]) >= 0)
692 _IO_flockfile (stderr);
694 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
695 ((_IO_FILE *) stderr)->_flags2
696 |= _IO_FLAGS2_NOTCANCEL;
698 __fxprintf (NULL, "%s", buf);
700 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
701 _IO_funlockfile (stderr);
707 _("%s: option `%s' requires an argument\n"),
708 argv[0], argv[d->optind - 1]);
711 d->__nextchar += strlen (d->__nextchar);
712 d->optopt = pfound->val;
713 return optstring[0] == ':' ? ':' : '?';
716 d->__nextchar += strlen (d->__nextchar);
718 *longind = option_index;
721 *(pfound->flag) = pfound->val;
727 /* Can't find it as a long option. If this is not getopt_long_only,
728 or the option starts with '--' or is not a valid short
729 option, then it's an error.
730 Otherwise interpret it as a short option. */
731 if (!long_only || argv[d->optind][1] == '-'
732 || strchr (optstring, *d->__nextchar) == NULL)
736 #if defined _LIBC && defined USE_IN_LIBIO
741 if (argv[d->optind][1] == '-')
744 #if defined _LIBC && defined USE_IN_LIBIO
745 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
746 argv[0], d->__nextchar);
748 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
749 argv[0], d->__nextchar);
754 /* +option or -option */
755 #if defined _LIBC && defined USE_IN_LIBIO
756 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
757 argv[0], argv[d->optind][0], d->__nextchar);
759 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
760 argv[0], argv[d->optind][0], d->__nextchar);
764 #if defined _LIBC && defined USE_IN_LIBIO
767 _IO_flockfile (stderr);
769 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
770 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
772 __fxprintf (NULL, "%s", buf);
774 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
775 _IO_funlockfile (stderr);
781 d->__nextchar = (char *) "";
788 /* Look at and handle the next short option-character. */
791 char c = *d->__nextchar++;
792 char *temp = strchr (optstring, c);
794 /* Increment `optind' when we start to process its last character. */
795 if (*d->__nextchar == '\0')
798 if (temp == NULL || c == ':')
802 #if defined _LIBC && defined USE_IN_LIBIO
807 if (d->__posixly_correct)
809 /* 1003.2 specifies the format of this message. */
810 #if defined _LIBC && defined USE_IN_LIBIO
811 n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
814 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
819 #if defined _LIBC && defined USE_IN_LIBIO
820 n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
823 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
827 #if defined _LIBC && defined USE_IN_LIBIO
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);
847 #ifdef SPECIAL_TREATMENT_FOR_W
848 /* Convenience. Treat POSIX -W foo same as long option --foo */
849 if (temp[0] == 'W' && temp[1] == ';')
852 const struct option *p;
853 const struct option *pfound = NULL;
859 /* This is an option that requires an argument. */
860 if (*d->__nextchar != '\0')
862 d->optarg = d->__nextchar;
863 /* If we end this ARGV-element by taking the rest as an arg,
864 we must advance to the next element now. */
867 else if (d->optind == argc)
871 /* 1003.2 specifies the format of this message. */
872 #if defined _LIBC && defined USE_IN_LIBIO
875 if (__asprintf (&buf,
876 _("%s: option requires an argument -- %c\n"),
879 _IO_flockfile (stderr);
881 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
882 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
884 __fxprintf (NULL, "%s", buf);
886 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
887 _IO_funlockfile (stderr);
892 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
897 if (optstring[0] == ':')
904 /* We already incremented `d->optind' once;
905 increment it again when taking next ARGV-elt as argument. */
906 d->optarg = argv[d->optind++];
908 /* optarg is now the argument, see if it's in the
909 table of longopts. */
911 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
915 /* Test all long options for either exact match
916 or abbreviated matches. */
917 for (p = longopts, option_index = 0; p->name; p++, option_index++)
918 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
920 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
922 /* Exact match found. */
924 indfound = option_index;
928 else if (pfound == NULL)
930 /* First nonexact match found. */
932 indfound = option_index;
935 /* Second or later nonexact match found. */
942 #if defined _LIBC && defined USE_IN_LIBIO
945 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
946 argv[0], argv[d->optind]) >= 0)
948 _IO_flockfile (stderr);
950 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
951 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
953 __fxprintf (NULL, "%s", buf);
955 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
956 _IO_funlockfile (stderr);
961 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
962 argv[0], argv[d->optind]);
965 d->__nextchar += strlen (d->__nextchar);
971 option_index = indfound;
974 /* Don't test has_arg with >, because some C compilers don't
975 allow it to be used on enums. */
977 d->optarg = nameend + 1;
982 #if defined _LIBC && defined USE_IN_LIBIO
985 if (__asprintf (&buf, _("\
986 %s: option `-W %s' doesn't allow an argument\n"),
987 argv[0], pfound->name) >= 0)
989 _IO_flockfile (stderr);
991 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
992 ((_IO_FILE *) stderr)->_flags2
993 |= _IO_FLAGS2_NOTCANCEL;
995 __fxprintf (NULL, "%s", buf);
997 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
998 _IO_funlockfile (stderr);
1003 fprintf (stderr, _("\
1004 %s: option `-W %s' doesn't allow an argument\n"),
1005 argv[0], pfound->name);
1009 d->__nextchar += strlen (d->__nextchar);
1013 else if (pfound->has_arg == 1)
1015 if (d->optind < argc)
1016 d->optarg = argv[d->optind++];
1021 #if defined _LIBC && defined USE_IN_LIBIO
1024 if (__asprintf (&buf, _("\
1025 %s: option `%s' requires an argument\n"),
1026 argv[0], argv[d->optind - 1]) >= 0)
1028 _IO_flockfile (stderr);
1030 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1031 ((_IO_FILE *) stderr)->_flags2
1032 |= _IO_FLAGS2_NOTCANCEL;
1034 __fxprintf (NULL, "%s", buf);
1036 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1037 _IO_funlockfile (stderr);
1043 _("%s: option `%s' requires an argument\n"),
1044 argv[0], argv[d->optind - 1]);
1047 d->__nextchar += strlen (d->__nextchar);
1048 return optstring[0] == ':' ? ':' : '?';
1051 d->__nextchar += strlen (d->__nextchar);
1052 if (longind != NULL)
1053 *longind = option_index;
1056 *(pfound->flag) = pfound->val;
1061 d->__nextchar = NULL;
1062 return 'W'; /* Let the application handle it. */
1069 /* This is an option that accepts an argument optionally. */
1070 if (*d->__nextchar != '\0')
1072 d->optarg = d->__nextchar;
1077 d->__nextchar = NULL;
1081 /* This is an option that requires an argument. */
1082 if (*d->__nextchar != '\0')
1084 d->optarg = d->__nextchar;
1085 /* If we end this ARGV-element by taking the rest as an arg,
1086 we must advance to the next element now. */
1089 else if (d->optind == argc)
1093 /* 1003.2 specifies the format of this message. */
1094 #if defined _LIBC && defined USE_IN_LIBIO
1097 if (__asprintf (&buf, _("\
1098 %s: option requires an argument -- %c\n"),
1101 _IO_flockfile (stderr);
1103 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1104 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1106 __fxprintf (NULL, "%s", buf);
1108 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1109 _IO_funlockfile (stderr);
1115 _("%s: option requires an argument -- %c\n"),
1120 if (optstring[0] == ':')
1126 /* We already incremented `optind' once;
1127 increment it again when taking next ARGV-elt as argument. */
1128 d->optarg = argv[d->optind++];
1129 d->__nextchar = NULL;
1137 _getopt_internal (int argc, char *const *argv, const char *optstring,
1138 const struct option *longopts, int *longind, int long_only)
1142 getopt_data.optind = optind;
1143 getopt_data.opterr = opterr;
1145 result = _getopt_internal_r (argc, argv, optstring, longopts,
1146 longind, long_only, &getopt_data);
1148 optind = getopt_data.optind;
1149 optarg = getopt_data.optarg;
1150 optopt = getopt_data.optopt;
1156 getopt (int argc, char *const *argv, const char *optstring)
1158 return _getopt_internal (argc, argv, optstring,
1159 (const struct option *) 0,
1163 libc_hidden_def(getopt)
1166 getopt_long (int argc, char *const *argv, const char *options,
1167 const struct option *long_options, int *opt_index)
1169 return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
1172 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
1173 If an option that starts with '-' (not '--') doesn't match a long option,
1174 but does match a short option, it is parsed as a short option
1178 getopt_long_only (int argc, char *const *argv, const char *options,
1179 const struct option *long_options, int *opt_index)
1181 return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
1184 #endif /* Not ELIDE_CODE. */