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
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, write to the Free
21 Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
24 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
25 Ditto for AIX 3.2 and <stdlib.h>. */
31 #include "wine/port.h"
33 #ifndef HAVE_GETOPT_LONG_ONLY
45 /* This is for other GNU distributions with internationalized messages. */
46 # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
49 # define _(msgid) gettext (msgid)
52 # define _(msgid) (msgid)
54 # if defined _LIBC && defined USE_IN_LIBIO
59 #ifndef attribute_hidden
60 # define attribute_hidden
63 /* This version of `getopt' appears to the caller like standard Unix `getopt'
64 but it behaves differently for the user, since it allows the user
65 to intersperse the options with the other arguments.
67 As `getopt' works, it permutes the elements of ARGV so that,
68 when it is done, all the options precede everything else. Thus
69 all application programs are extended to handle flexible argument order.
71 Setting the environment variable POSIXLY_CORRECT disables permutation.
72 Then the behavior is completely standard.
74 GNU application programs can use a third alternative mode in which
75 they can distinguish the relative order of options and other arguments. */
77 /* For communication from `getopt' to the caller.
78 When `getopt' finds an option that takes an argument,
79 the argument value is returned here.
80 Also, when `ordering' is RETURN_IN_ORDER,
81 each non-option ARGV-element is returned here. */
85 /* Index in ARGV of the next element to be scanned.
86 This is used for communication to and from the caller
87 and for communication between successive calls to `getopt'.
89 On entry to `getopt', zero means this is the first call; initialize.
91 When `getopt' returns -1, this is the index of the first of the
92 non-option elements that the caller should itself scan.
94 Otherwise, `optind' communicates from one call to the next
95 how much of ARGV has been scanned so far. */
97 /* 1003.2 says this must be 1 before any call. */
100 /* Formerly, initialization of getopt depended on optind==0, which
101 causes problems with re-calling getopt as programs generally don't
104 int __getopt_initialized attribute_hidden
;
106 /* The next char to be scanned in the option-element
107 in which the last option character we returned was found.
108 This allows us to pick up the scan where we left off.
110 If this is zero, or a null string, it means resume the scan
111 by advancing to the next ARGV-element. */
113 static char *nextchar
;
115 /* Callers store zero here to inhibit the error message
116 for unrecognized options. */
120 /* Set to an option character which was unrecognized.
121 This must be initialized on some systems to avoid linking in the
122 system's own getopt implementation. */
126 /* Describe how to deal with options that follow non-option ARGV-elements.
128 If the caller did not specify anything,
129 the default is REQUIRE_ORDER if the environment variable
130 POSIXLY_CORRECT is defined, PERMUTE otherwise.
132 REQUIRE_ORDER means don't recognize them as options;
133 stop option processing when the first non-option is seen.
134 This is what Unix does.
135 This mode of operation is selected by either setting the environment
136 variable POSIXLY_CORRECT, or using `+' as the first character
137 of the list of option characters.
139 PERMUTE is the default. We permute the contents of ARGV as we scan,
140 so that eventually all the non-options are at the end. This allows options
141 to be given in any order, even with programs that were not written to
144 RETURN_IN_ORDER is an option available to programs that were written
145 to expect options and other ARGV-elements in any order and that care about
146 the ordering of the two. We describe each non-option ARGV-element
147 as if it were the argument of an option with character code 1.
148 Using `-' as the first character of the list of option characters
149 selects this mode of operation.
151 The special argument `--' forces an end of option-scanning regardless
152 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
153 `--' can cause `getopt' to return -1 with `optind' != ARGC. */
157 REQUIRE_ORDER
, PERMUTE
, RETURN_IN_ORDER
160 /* Value of POSIXLY_CORRECT environment variable. */
161 static char *posixly_correct
;
164 #define my_index strchr
166 /* If using GCC, we can safely declare strlen this way.
167 If not using GCC, it is ok not to declare it. */
169 /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
170 That was relevant to code that was here before. */
171 # if (!defined __STDC__ || !__STDC__) && !defined strlen
172 /* gcc with -traditional declares the built-in strlen to return int,
173 and has done so at least since version 2.4.5. -- rms. */
174 extern int strlen (const char *);
175 # endif /* not __STDC__ */
176 #endif /* __GNUC__ */
178 /* Handle permutation of arguments. */
180 /* Describe the part of ARGV that contains non-options that have
181 been skipped. `first_nonopt' is the index in ARGV of the first of them;
182 `last_nonopt' is the index after the last of them. */
184 static int first_nonopt
;
185 static int last_nonopt
;
188 /* Stored original parameters.
189 XXX This is no good solution. We should rather copy the args so
190 that we can compare them later. But we must not use malloc(3). */
191 extern int __libc_argc
;
192 extern char **__libc_argv
;
194 /* Bash 2.0 gives us an environment variable containing flags
195 indicating ARGV elements that should not be considered arguments. */
197 # ifdef USE_NONOPTION_FLAGS
198 /* Defined in getopt_init.c */
199 extern char *__getopt_nonoption_flags
;
201 static int nonoption_flags_max_len
;
202 static int nonoption_flags_len
;
205 # ifdef USE_NONOPTION_FLAGS
206 # define SWAP_FLAGS(ch1, ch2) \
207 if (nonoption_flags_len > 0) \
209 char __tmp = __getopt_nonoption_flags[ch1]; \
210 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
211 __getopt_nonoption_flags[ch2] = __tmp; \
214 # define SWAP_FLAGS(ch1, ch2)
217 # define SWAP_FLAGS(ch1, ch2)
220 /* Exchange two adjacent subsequences of ARGV.
221 One subsequence is elements [first_nonopt,last_nonopt)
222 which contains all the non-options that have been skipped so far.
223 The other is elements [last_nonopt,optind), which contains all
224 the options processed since those non-options were skipped.
226 `first_nonopt' and `last_nonopt' are relocated so that they describe
227 the new indices of the non-options in ARGV after they are moved. */
229 #if defined __STDC__ && __STDC__
230 static void exchange (char **);
237 int bottom
= first_nonopt
;
238 int middle
= last_nonopt
;
242 /* Exchange the shorter segment with the far end of the longer segment.
243 That puts the shorter segment into the right place.
244 It leaves the longer segment in the right place overall,
245 but it consists of two parts that need to be swapped next. */
247 #if defined _LIBC && defined USE_NONOPTION_FLAGS
248 /* First make sure the handling of the `__getopt_nonoption_flags'
249 string can work normally. Our top argument must be in the range
251 if (nonoption_flags_len
> 0 && top
>= nonoption_flags_max_len
)
253 /* We must extend the array. The user plays games with us and
254 presents new arguments. */
255 char *new_str
= malloc (top
+ 1);
257 nonoption_flags_len
= nonoption_flags_max_len
= 0;
260 memset (__mempcpy (new_str
, __getopt_nonoption_flags
,
261 nonoption_flags_max_len
),
262 '\0', top
+ 1 - nonoption_flags_max_len
);
263 nonoption_flags_max_len
= top
+ 1;
264 __getopt_nonoption_flags
= new_str
;
269 while (top
> middle
&& middle
> bottom
)
271 if (top
- middle
> middle
- bottom
)
273 /* Bottom segment is the short one. */
274 int len
= middle
- bottom
;
277 /* Swap it with the top part of the top segment. */
278 for (i
= 0; i
< len
; i
++)
280 tem
= argv
[bottom
+ i
];
281 argv
[bottom
+ i
] = argv
[top
- (middle
- bottom
) + i
];
282 argv
[top
- (middle
- bottom
) + i
] = tem
;
283 SWAP_FLAGS (bottom
+ i
, top
- (middle
- bottom
) + i
);
285 /* Exclude the moved bottom segment from further swapping. */
290 /* Top segment is the short one. */
291 int len
= top
- middle
;
294 /* Swap it with the bottom part of the bottom segment. */
295 for (i
= 0; i
< len
; i
++)
297 tem
= argv
[bottom
+ i
];
298 argv
[bottom
+ i
] = argv
[middle
+ i
];
299 argv
[middle
+ i
] = tem
;
300 SWAP_FLAGS (bottom
+ i
, middle
+ i
);
302 /* Exclude the moved top segment from further swapping. */
307 /* Update records for the slots the non-options now occupy. */
309 first_nonopt
+= (optind
- last_nonopt
);
310 last_nonopt
= optind
;
313 /* Initialize the internal data when the first call is made. */
315 #if defined __STDC__ && __STDC__
316 static const char *_getopt_initialize (int, char *const *, const char *);
319 _getopt_initialize (argc
, argv
, optstring
)
322 const char *optstring
;
324 /* Start processing options with ARGV-element 1 (since ARGV-element 0
325 is the program name); the sequence of previously skipped
326 non-option ARGV-elements is empty. */
328 first_nonopt
= last_nonopt
= optind
;
332 posixly_correct
= getenv ("POSIXLY_CORRECT");
334 /* Determine how to handle the ordering of options and nonoptions. */
336 if (optstring
[0] == '-')
338 ordering
= RETURN_IN_ORDER
;
341 else if (optstring
[0] == '+')
343 ordering
= REQUIRE_ORDER
;
346 else if (posixly_correct
!= NULL
)
347 ordering
= REQUIRE_ORDER
;
351 #if defined _LIBC && defined USE_NONOPTION_FLAGS
352 if (posixly_correct
== NULL
353 && argc
== __libc_argc
&& argv
== __libc_argv
)
355 if (nonoption_flags_max_len
== 0)
357 if (__getopt_nonoption_flags
== NULL
358 || __getopt_nonoption_flags
[0] == '\0')
359 nonoption_flags_max_len
= -1;
362 const char *orig_str
= __getopt_nonoption_flags
;
363 int len
= nonoption_flags_max_len
= strlen (orig_str
);
364 if (nonoption_flags_max_len
< argc
)
365 nonoption_flags_max_len
= argc
;
366 __getopt_nonoption_flags
= malloc (nonoption_flags_max_len
);
367 if (__getopt_nonoption_flags
== NULL
)
368 nonoption_flags_max_len
= -1;
370 memset (__mempcpy (__getopt_nonoption_flags
, orig_str
, len
),
371 '\0', nonoption_flags_max_len
- len
);
374 nonoption_flags_len
= nonoption_flags_max_len
;
377 nonoption_flags_len
= 0;
383 /* Scan elements of ARGV (whose length is ARGC) for option characters
386 If an element of ARGV starts with '-', and is not exactly "-" or "--",
387 then it is an option element. The characters of this element
388 (aside from the initial '-') are option characters. If `getopt'
389 is called repeatedly, it returns successively each of the option characters
390 from each of the option elements.
392 If `getopt' finds another option character, it returns that character,
393 updating `optind' and `nextchar' so that the next call to `getopt' can
394 resume the scan with the following option character or ARGV-element.
396 If there are no more option characters, `getopt' returns -1.
397 Then `optind' is the index in ARGV of the first ARGV-element
398 that is not an option. (The ARGV-elements have been permuted
399 so that those that are not options now come last.)
401 OPTSTRING is a string containing the legitimate option characters.
402 If an option character is seen that is not listed in OPTSTRING,
403 return '?' after printing an error message. If you set `opterr' to
404 zero, the error message is suppressed but we still return '?'.
406 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
407 so the following text in the same ARGV-element, or the text of the following
408 ARGV-element, is returned in `optarg'. Two colons mean an option that
409 wants an optional arg; if there is text in the current ARGV-element,
410 it is returned in `optarg', otherwise `optarg' is set to zero.
412 If OPTSTRING starts with `-' or `+', it requests different methods of
413 handling the non-option ARGV-elements.
414 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
416 Long-named options begin with `--' instead of `-'.
417 Their names may be abbreviated as long as the abbreviation is unique
418 or is an exact match for some defined option. If they have an
419 argument, it follows the option name in the same ARGV-element, separated
420 from the option name by a `=', or else the in next ARGV-element.
421 When `getopt' finds a long-named option, it returns 0 if that option's
422 `flag' field is nonzero, the value of the option's `val' field
423 if the `flag' field is zero.
425 The elements of ARGV aren't really const, because we permute them.
426 But we pretend they're const in the prototype to be compatible
429 LONGOPTS is a vector of `struct option' terminated by an
430 element containing a name which is zero.
432 LONGIND returns the index in LONGOPT of the long-named option found.
433 It is only valid when a long-named option has been found by the most
436 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
437 long-named options. */
440 _getopt_internal( int argc
, char * const *argv
, const char *optstring
, const struct option
*longopts
, int *longind
, int long_only
)
442 int print_errors
= opterr
;
443 if (optstring
[0] == ':')
451 if (optind
== 0 || !__getopt_initialized
)
454 optind
= 1; /* Don't scan ARGV[0], the program name. */
455 optstring
= _getopt_initialize (argc
, argv
, optstring
);
456 __getopt_initialized
= 1;
459 /* Test whether ARGV[optind] points to a non-option argument.
460 Either it does not have option syntax, or there is an environment flag
461 from the shell indicating it is not an option. The later information
462 is only used when the used in the GNU libc. */
463 #if defined _LIBC && defined USE_NONOPTION_FLAGS
464 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
465 || (optind < nonoption_flags_len \
466 && __getopt_nonoption_flags[optind] == '1'))
468 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
471 if (nextchar
== NULL
|| *nextchar
== '\0')
473 /* Advance to the next ARGV-element. */
475 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
476 moved back by the user (who may also have changed the arguments). */
477 if (last_nonopt
> optind
)
478 last_nonopt
= optind
;
479 if (first_nonopt
> optind
)
480 first_nonopt
= optind
;
482 if (ordering
== PERMUTE
)
484 /* If we have just processed some options following some non-options,
485 exchange them so that the options come first. */
487 if (first_nonopt
!= last_nonopt
&& last_nonopt
!= optind
)
488 exchange ((char **) argv
);
489 else if (last_nonopt
!= optind
)
490 first_nonopt
= optind
;
492 /* Skip any additional non-options
493 and extend the range of non-options previously skipped. */
495 while (optind
< argc
&& NONOPTION_P
)
497 last_nonopt
= optind
;
500 /* The special ARGV-element `--' means premature end of options.
501 Skip it like a null option,
502 then exchange with previous non-options as if it were an option,
503 then skip everything else like a non-option. */
505 if (optind
!= argc
&& !strcmp (argv
[optind
], "--"))
509 if (first_nonopt
!= last_nonopt
&& last_nonopt
!= optind
)
510 exchange ((char **) argv
);
511 else if (first_nonopt
== last_nonopt
)
512 first_nonopt
= optind
;
518 /* If we have done all the ARGV-elements, stop the scan
519 and back over any non-options that we skipped and permuted. */
523 /* Set the next-arg-index to point at the non-options
524 that we previously skipped, so the caller will digest them. */
525 if (first_nonopt
!= last_nonopt
)
526 optind
= first_nonopt
;
530 /* If we have come to a non-option and did not permute it,
531 either stop the scan or describe it to the caller and pass it by. */
535 if (ordering
== REQUIRE_ORDER
)
537 optarg
= argv
[optind
++];
541 /* We have found another option-ARGV-element.
542 Skip the initial punctuation. */
544 nextchar
= (argv
[optind
] + 1
545 + (longopts
!= NULL
&& argv
[optind
][1] == '-'));
548 /* Decode the current option-ARGV-element. */
550 /* Check whether the ARGV-element is a long option.
552 If long_only and the ARGV-element has the form "-f", where f is
553 a valid short option, don't consider it an abbreviated form of
554 a long option that starts with f. Otherwise there would be no
555 way to give the -f short option.
557 On the other hand, if there's a long option "fubar" and
558 the ARGV-element is "-fu", do consider that an abbreviation of
559 the long option, just like "--fu", and not "-f" with arg "u".
561 This distinction seems to be the most useful approach. */
564 && (argv
[optind
][1] == '-'
565 || (long_only
&& (argv
[optind
][2] || !my_index (optstring
, argv
[optind
][1])))))
568 const struct option
*p
;
569 const struct option
*pfound
= NULL
;
575 for (nameend
= nextchar
; *nameend
&& *nameend
!= '='; nameend
++)
578 /* Test all long options for either exact match
579 or abbreviated matches. */
580 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
581 if (!strncmp (p
->name
, nextchar
, nameend
- nextchar
))
583 if ((unsigned int) (nameend
- nextchar
)
584 == (unsigned int) strlen (p
->name
))
586 /* Exact match found. */
588 indfound
= option_index
;
592 else if (pfound
== NULL
)
594 /* First nonexact match found. */
596 indfound
= option_index
;
599 || pfound
->has_arg
!= p
->has_arg
600 || pfound
->flag
!= p
->flag
601 || pfound
->val
!= p
->val
)
602 /* Second or later nonexact match found. */
610 #if defined _LIBC && defined USE_IN_LIBIO
613 if (__asprintf (&buf
, _("%s: option `%s' is ambiguous\n"),
614 argv
[0], argv
[optind
]) >= 0)
617 if (_IO_fwide (stderr
, 0) > 0)
618 __fwprintf (stderr
, L
"%s", buf
);
625 fprintf (stderr
, _("%s: option `%s' is ambiguous\n"),
626 argv
[0], argv
[optind
]);
629 nextchar
+= strlen (nextchar
);
637 option_index
= indfound
;
641 /* Don't test has_arg with >, because some C compilers don't
642 allow it to be used on enums. */
644 optarg
= nameend
+ 1;
649 #if defined _LIBC && defined USE_IN_LIBIO
654 if (argv
[optind
- 1][1] == '-')
657 #if defined _LIBC && defined USE_IN_LIBIO
658 n
= __asprintf (&buf
, _("\
659 %s: option `--%s' doesn't allow an argument\n"),
660 argv
[0], pfound
->name
);
662 fprintf (stderr
, _("\
663 %s: option `--%s' doesn't allow an argument\n"),
664 argv
[0], pfound
->name
);
669 /* +option or -option */
670 #if defined _LIBC && defined USE_IN_LIBIO
671 n
= __asprintf (&buf
, _("\
672 %s: option `%c%s' doesn't allow an argument\n"),
673 argv
[0], argv
[optind
- 1][0],
676 fprintf (stderr
, _("\
677 %s: option `%c%s' doesn't allow an argument\n"),
678 argv
[0], argv
[optind
- 1][0], pfound
->name
);
682 #if defined _LIBC && defined USE_IN_LIBIO
685 if (_IO_fwide (stderr
, 0) > 0)
686 __fwprintf (stderr
, L
"%s", buf
);
695 nextchar
+= strlen (nextchar
);
697 optopt
= pfound
->val
;
701 else if (pfound
->has_arg
== 1)
704 optarg
= argv
[optind
++];
709 #if defined _LIBC && defined USE_IN_LIBIO
712 if (__asprintf (&buf
, _("\
713 %s: option `%s' requires an argument\n"),
714 argv
[0], argv
[optind
- 1]) >= 0)
716 if (_IO_fwide (stderr
, 0) > 0)
717 __fwprintf (stderr
, L
"%s", buf
);
725 _("%s: option `%s' requires an argument\n"),
726 argv
[0], argv
[optind
- 1]);
729 nextchar
+= strlen (nextchar
);
730 optopt
= pfound
->val
;
731 return optstring
[0] == ':' ? ':' : '?';
734 nextchar
+= strlen (nextchar
);
736 *longind
= option_index
;
739 *(pfound
->flag
) = pfound
->val
;
745 /* Can't find it as a long option. If this is not getopt_long_only,
746 or the option starts with '--' or is not a valid short
747 option, then it's an error.
748 Otherwise interpret it as a short option. */
749 if (!long_only
|| argv
[optind
][1] == '-'
750 || my_index (optstring
, *nextchar
) == NULL
)
754 #if defined _LIBC && defined USE_IN_LIBIO
759 if (argv
[optind
][1] == '-')
762 #if defined _LIBC && defined USE_IN_LIBIO
763 n
= __asprintf (&buf
, _("%s: unrecognized option `--%s'\n"),
766 fprintf (stderr
, _("%s: unrecognized option `--%s'\n"),
772 /* +option or -option */
773 #if defined _LIBC && defined USE_IN_LIBIO
774 n
= __asprintf (&buf
, _("%s: unrecognized option `%c%s'\n"),
775 argv
[0], argv
[optind
][0], nextchar
);
777 fprintf (stderr
, _("%s: unrecognized option `%c%s'\n"),
778 argv
[0], argv
[optind
][0], nextchar
);
782 #if defined _LIBC && defined USE_IN_LIBIO
785 if (_IO_fwide (stderr
, 0) > 0)
786 __fwprintf (stderr
, L
"%s", buf
);
794 nextchar
= (char *) "";
801 /* Look at and handle the next short option-character. */
804 char c
= *nextchar
++;
805 char *temp
= my_index (optstring
, c
);
807 /* Increment `optind' when we start to process its last character. */
808 if (*nextchar
== '\0')
811 if (temp
== NULL
|| c
== ':')
815 #if defined _LIBC && defined USE_IN_LIBIO
822 /* 1003.2 specifies the format of this message. */
823 #if defined _LIBC && defined USE_IN_LIBIO
824 n
= __asprintf (&buf
, _("%s: illegal option -- %c\n"),
827 fprintf (stderr
, _("%s: illegal option -- %c\n"), argv
[0], c
);
832 #if defined _LIBC && defined USE_IN_LIBIO
833 n
= __asprintf (&buf
, _("%s: invalid option -- %c\n"),
836 fprintf (stderr
, _("%s: invalid option -- %c\n"), argv
[0], c
);
840 #if defined _LIBC && defined USE_IN_LIBIO
843 if (_IO_fwide (stderr
, 0) > 0)
844 __fwprintf (stderr
, L
"%s", buf
);
855 /* Convenience. Treat POSIX -W foo same as long option --foo */
856 if (temp
[0] == 'W' && temp
[1] == ';')
859 const struct option
*p
;
860 const struct option
*pfound
= NULL
;
866 /* This is an option that requires an argument. */
867 if (*nextchar
!= '\0')
870 /* If we end this ARGV-element by taking the rest as an arg,
871 we must advance to the next element now. */
874 else if (optind
== argc
)
878 /* 1003.2 specifies the format of this message. */
879 #if defined _LIBC && defined USE_IN_LIBIO
882 if (__asprintf (&buf
,
883 _("%s: option requires an argument -- %c\n"),
886 if (_IO_fwide (stderr
, 0) > 0)
887 __fwprintf (stderr
, L
"%s", buf
);
894 fprintf (stderr
, _("%s: option requires an argument -- %c\n"),
899 if (optstring
[0] == ':')
906 /* We already incremented `optind' once;
907 increment it again when taking next ARGV-elt as argument. */
908 optarg
= argv
[optind
++];
910 /* optarg is now the argument, see if it's in the
911 table of longopts. */
913 for (nextchar
= nameend
= optarg
; *nameend
&& *nameend
!= '='; nameend
++)
916 /* Test all long options for either exact match
917 or abbreviated matches. */
918 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
919 if (!strncmp (p
->name
, nextchar
, nameend
- nextchar
))
921 if ((unsigned int) (nameend
- nextchar
) == strlen (p
->name
))
923 /* Exact match found. */
925 indfound
= option_index
;
929 else if (pfound
== NULL
)
931 /* First nonexact match found. */
933 indfound
= option_index
;
936 /* Second or later nonexact match found. */
943 #if defined _LIBC && defined USE_IN_LIBIO
946 if (__asprintf (&buf
, _("%s: option `-W %s' is ambiguous\n"),
947 argv
[0], argv
[optind
]) >= 0)
949 if (_IO_fwide (stderr
, 0) > 0)
950 __fwprintf (stderr
, L
"%s", buf
);
957 fprintf (stderr
, _("%s: option `-W %s' is ambiguous\n"),
958 argv
[0], argv
[optind
]);
961 nextchar
+= strlen (nextchar
);
967 option_index
= indfound
;
970 /* Don't test has_arg with >, because some C compilers don't
971 allow it to be used on enums. */
973 optarg
= nameend
+ 1;
978 #if defined _LIBC && defined USE_IN_LIBIO
981 if (__asprintf (&buf
, _("\
982 %s: option `-W %s' doesn't allow an argument\n"),
983 argv
[0], pfound
->name
) >= 0)
985 if (_IO_fwide (stderr
, 0) > 0)
986 __fwprintf (stderr
, L
"%s", buf
);
993 fprintf (stderr
, _("\
994 %s: option `-W %s' doesn't allow an argument\n"),
995 argv
[0], pfound
->name
);
999 nextchar
+= strlen (nextchar
);
1003 else if (pfound
->has_arg
== 1)
1006 optarg
= argv
[optind
++];
1011 #if defined _LIBC && defined USE_IN_LIBIO
1014 if (__asprintf (&buf
, _("\
1015 %s: option `%s' requires an argument\n"),
1016 argv
[0], argv
[optind
- 1]) >= 0)
1018 if (_IO_fwide (stderr
, 0) > 0)
1019 __fwprintf (stderr
, L
"%s", buf
);
1021 fputs (buf
, stderr
);
1027 _("%s: option `%s' requires an argument\n"),
1028 argv
[0], argv
[optind
- 1]);
1031 nextchar
+= strlen (nextchar
);
1032 return optstring
[0] == ':' ? ':' : '?';
1035 nextchar
+= strlen (nextchar
);
1036 if (longind
!= NULL
)
1037 *longind
= option_index
;
1040 *(pfound
->flag
) = pfound
->val
;
1046 return 'W'; /* Let the application handle it. */
1052 /* This is an option that accepts an argument optionally. */
1053 if (*nextchar
!= '\0')
1064 /* This is an option that requires an argument. */
1065 if (*nextchar
!= '\0')
1068 /* If we end this ARGV-element by taking the rest as an arg,
1069 we must advance to the next element now. */
1072 else if (optind
== argc
)
1076 /* 1003.2 specifies the format of this message. */
1077 #if defined _LIBC && defined USE_IN_LIBIO
1080 if (__asprintf (&buf
, _("\
1081 %s: option requires an argument -- %c\n"),
1084 if (_IO_fwide (stderr
, 0) > 0)
1085 __fwprintf (stderr
, L
"%s", buf
);
1087 fputs (buf
, stderr
);
1093 _("%s: option requires an argument -- %c\n"),
1098 if (optstring
[0] == ':')
1104 /* We already incremented `optind' once;
1105 increment it again when taking next ARGV-elt as argument. */
1106 optarg
= argv
[optind
++];
1115 getopt (int argc
, char * const *argv
, const char *optstring
)
1117 return _getopt_internal (argc
, argv
, optstring
,
1124 getopt_long (int argc
, char * const *argv
, const char *options
, const struct option
*long_options
, int *opt_index
)
1126 return _getopt_internal (argc
, argv
, options
, long_options
, opt_index
, 0);
1129 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
1130 If an option that starts with '-' (not '--') doesn't match a long option,
1131 but does match a short option, it is parsed as a short option
1135 getopt_long_only (int argc
, char * const *argv
, const char *options
, const struct option
*long_options
, int *opt_index
)
1137 return _getopt_internal (argc
, argv
, options
, long_options
, opt_index
, 1);
1140 #endif /* HAVE_GETOPT_LONG_ONLY */