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,2008,2009,2010
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., 59 Temple Place, Suite 330, Boston, MA
24 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
25 Ditto for AIX 3.2 and <stdlib.h>. */
36 /* Comment out all this code if we are using the GNU C Library, and are not
37 actually compiling the library itself. This code is part of the GNU C
38 Library, but also included in many other GNU distributions. Compiling
39 and linking in this code is a waste when using the GNU C library
40 (especially if it is a shared library). Rather than having every GNU
41 program understand `configure --with-gnu-libc' and omit the object files,
42 it is simpler to just do this in the source for each such file. */
44 #define GETOPT_INTERFACE_VERSION 2
45 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
46 # include <gnu-versions.h>
47 # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
55 /* This needs to come after some library #include
56 to get __GNU_LIBRARY__ defined. */
57 #ifdef __GNU_LIBRARY__
58 /* Don't include stdlib.h for non-GNU C libraries because some of them
59 contain conflicting prototypes for getopt. */
62 #endif /* GNU C library. */
74 # define _(msgid) gettext (msgid)
77 #if defined _LIBC && defined USE_IN_LIBIO
81 #ifndef attribute_hidden
82 # define attribute_hidden
85 /* This version of `getopt' appears to the caller like standard Unix `getopt'
86 but it behaves differently for the user, since it allows the user
87 to intersperse the options with the other arguments.
89 As `getopt' works, it permutes the elements of ARGV so that,
90 when it is done, all the options precede everything else. Thus
91 all application programs are extended to handle flexible argument order.
93 Setting the environment variable POSIXLY_CORRECT disables permutation.
94 Then the behavior is completely standard.
96 GNU application programs can use a third alternative mode in which
97 they can distinguish the relative order of options and other arguments. */
100 #include "getopt_int.h"
102 /* For communication from `getopt' to the caller.
103 When `getopt' finds an option that takes an argument,
104 the argument value is returned here.
105 Also, when `ordering' is RETURN_IN_ORDER,
106 each non-option ARGV-element is returned here. */
110 /* Index in ARGV of the next element to be scanned.
111 This is used for communication to and from the caller
112 and for communication between successive calls to `getopt'.
114 On entry to `getopt', zero means this is the first call; initialize.
116 When `getopt' returns -1, this is the index of the first of the
117 non-option elements that the caller should itself scan.
119 Otherwise, `optind' communicates from one call to the next
120 how much of ARGV has been scanned so far. */
122 /* 1003.2 says this must be 1 before any call. */
125 /* Callers store zero here to inhibit the error message
126 for unrecognized options. */
130 /* Set to an option character which was unrecognized.
131 This must be initialized on some systems to avoid linking in the
132 system's own getopt implementation. */
136 /* Keep a global copy of all internal members of getopt_data. */
138 static struct _getopt_data getopt_data
;
141 #ifndef __GNU_LIBRARY__
143 /* Avoid depending on library functions or files
144 whose names are inconsistent. */
147 extern char *getenv ();
150 #endif /* not __GNU_LIBRARY__ */
153 /* Stored original parameters.
154 XXX This is no good solution. We should rather copy the args so
155 that we can compare them later. But we must not use malloc(3). */
156 extern int __libc_argc
;
157 extern char **__libc_argv
;
159 /* Bash 2.0 gives us an environment variable containing flags
160 indicating ARGV elements that should not be considered arguments. */
162 # ifdef USE_NONOPTION_FLAGS
163 /* Defined in getopt_init.c */
164 extern char *__getopt_nonoption_flags
;
167 # ifdef USE_NONOPTION_FLAGS
168 # define SWAP_FLAGS(ch1, ch2) \
169 if (d->__nonoption_flags_len > 0) \
171 char __tmp = __getopt_nonoption_flags[ch1]; \
172 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
173 __getopt_nonoption_flags[ch2] = __tmp; \
176 # define SWAP_FLAGS(ch1, ch2)
179 # define SWAP_FLAGS(ch1, ch2)
182 /* Exchange two adjacent subsequences of ARGV.
183 One subsequence is elements [first_nonopt,last_nonopt)
184 which contains all the non-options that have been skipped so far.
185 The other is elements [last_nonopt,optind), which contains all
186 the options processed since those non-options were skipped.
188 `first_nonopt' and `last_nonopt' are relocated so that they describe
189 the new indices of the non-options in ARGV after they are moved. */
192 exchange (char **argv
, struct _getopt_data
*d
)
194 int bottom
= d
->__first_nonopt
;
195 int middle
= d
->__last_nonopt
;
199 /* Exchange the shorter segment with the far end of the longer segment.
200 That puts the shorter segment into the right place.
201 It leaves the longer segment in the right place overall,
202 but it consists of two parts that need to be swapped next. */
204 #if defined _LIBC && defined USE_NONOPTION_FLAGS
205 /* First make sure the handling of the `__getopt_nonoption_flags'
206 string can work normally. Our top argument must be in the range
208 if (d
->__nonoption_flags_len
> 0 && top
>= d
->__nonoption_flags_max_len
)
210 /* We must extend the array. The user plays games with us and
211 presents new arguments. */
212 char *new_str
= malloc (top
+ 1);
214 d
->__nonoption_flags_len
= d
->__nonoption_flags_max_len
= 0;
217 memset (__mempcpy (new_str
, __getopt_nonoption_flags
,
218 d
->__nonoption_flags_max_len
),
219 '\0', top
+ 1 - d
->__nonoption_flags_max_len
);
220 d
->__nonoption_flags_max_len
= top
+ 1;
221 __getopt_nonoption_flags
= new_str
;
226 while (top
> middle
&& middle
> bottom
)
228 if (top
- middle
> middle
- bottom
)
230 /* Bottom segment is the short one. */
231 int len
= middle
- bottom
;
234 /* Swap it with the top part of the top segment. */
235 for (i
= 0; i
< len
; i
++)
237 tem
= argv
[bottom
+ i
];
238 argv
[bottom
+ i
] = argv
[top
- (middle
- bottom
) + i
];
239 argv
[top
- (middle
- bottom
) + i
] = tem
;
240 SWAP_FLAGS (bottom
+ i
, top
- (middle
- bottom
) + i
);
242 /* Exclude the moved bottom segment from further swapping. */
247 /* Top segment is the short one. */
248 int len
= top
- middle
;
251 /* Swap it with the bottom part of the bottom segment. */
252 for (i
= 0; i
< len
; i
++)
254 tem
= argv
[bottom
+ i
];
255 argv
[bottom
+ i
] = argv
[middle
+ i
];
256 argv
[middle
+ i
] = tem
;
257 SWAP_FLAGS (bottom
+ i
, middle
+ i
);
259 /* Exclude the moved top segment from further swapping. */
264 /* Update records for the slots the non-options now occupy. */
266 d
->__first_nonopt
+= (d
->optind
- d
->__last_nonopt
);
267 d
->__last_nonopt
= d
->optind
;
270 /* Initialize the internal data when the first call is made. */
273 _getopt_initialize (int argc
, char *const *argv
, const char *optstring
,
274 struct _getopt_data
*d
, int posixly_correct
)
276 /* Start processing options with ARGV-element 1 (since ARGV-element 0
277 is the program name); the sequence of previously skipped
278 non-option ARGV-elements is empty. */
280 d
->__first_nonopt
= d
->__last_nonopt
= d
->optind
;
282 d
->__nextchar
= NULL
;
284 d
->__posixly_correct
= posixly_correct
| !!getenv ("POSIXLY_CORRECT");
286 /* Determine how to handle the ordering of options and nonoptions. */
288 if (optstring
[0] == '-')
290 d
->__ordering
= RETURN_IN_ORDER
;
293 else if (optstring
[0] == '+')
295 d
->__ordering
= REQUIRE_ORDER
;
298 else if (d
->__posixly_correct
)
299 d
->__ordering
= REQUIRE_ORDER
;
301 d
->__ordering
= PERMUTE
;
303 #if defined _LIBC && defined USE_NONOPTION_FLAGS
304 if (!d
->__posixly_correct
305 && argc
== __libc_argc
&& argv
== __libc_argv
)
307 if (d
->__nonoption_flags_max_len
== 0)
309 if (__getopt_nonoption_flags
== NULL
310 || __getopt_nonoption_flags
[0] == '\0')
311 d
->__nonoption_flags_max_len
= -1;
314 const char *orig_str
= __getopt_nonoption_flags
;
315 int len
= d
->__nonoption_flags_max_len
= strlen (orig_str
);
316 if (d
->__nonoption_flags_max_len
< argc
)
317 d
->__nonoption_flags_max_len
= argc
;
318 __getopt_nonoption_flags
=
319 (char *) malloc (d
->__nonoption_flags_max_len
);
320 if (__getopt_nonoption_flags
== NULL
)
321 d
->__nonoption_flags_max_len
= -1;
323 memset (__mempcpy (__getopt_nonoption_flags
, orig_str
, len
),
324 '\0', d
->__nonoption_flags_max_len
- len
);
327 d
->__nonoption_flags_len
= d
->__nonoption_flags_max_len
;
330 d
->__nonoption_flags_len
= 0;
336 /* Scan elements of ARGV (whose length is ARGC) for option characters
339 If an element of ARGV starts with '-', and is not exactly "-" or "--",
340 then it is an option element. The characters of this element
341 (aside from the initial '-') are option characters. If `getopt'
342 is called repeatedly, it returns successively each of the option characters
343 from each of the option elements.
345 If `getopt' finds another option character, it returns that character,
346 updating `optind' and `nextchar' so that the next call to `getopt' can
347 resume the scan with the following option character or ARGV-element.
349 If there are no more option characters, `getopt' returns -1.
350 Then `optind' is the index in ARGV of the first ARGV-element
351 that is not an option. (The ARGV-elements have been permuted
352 so that those that are not options now come last.)
354 OPTSTRING is a string containing the legitimate option characters.
355 If an option character is seen that is not listed in OPTSTRING,
356 return '?' after printing an error message. If you set `opterr' to
357 zero, the error message is suppressed but we still return '?'.
359 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
360 so the following text in the same ARGV-element, or the text of the following
361 ARGV-element, is returned in `optarg'. Two colons mean an option that
362 wants an optional arg; if there is text in the current ARGV-element,
363 it is returned in `optarg', otherwise `optarg' is set to zero.
365 If OPTSTRING starts with `-' or `+', it requests different methods of
366 handling the non-option ARGV-elements.
367 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
369 Long-named options begin with `--' instead of `-'.
370 Their names may be abbreviated as long as the abbreviation is unique
371 or is an exact match for some defined option. If they have an
372 argument, it follows the option name in the same ARGV-element, separated
373 from the option name by a `=', or else the in next ARGV-element.
374 When `getopt' finds a long-named option, it returns 0 if that option's
375 `flag' field is nonzero, the value of the option's `val' field
376 if the `flag' field is zero.
378 The elements of ARGV aren't really const, because we permute them.
379 But we pretend they're const in the prototype to be compatible
382 LONGOPTS is a vector of `struct option' terminated by an
383 element containing a name which is zero.
385 LONGIND returns the index in LONGOPT of the long-named option found.
386 It is only valid when a long-named option has been found by the most
389 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
390 long-named options. */
393 _getopt_internal_r (int argc
, char *const *argv
, const char *optstring
,
394 const struct option
*longopts
, int *longind
,
395 int long_only
, struct _getopt_data
*d
, int posixly_correct
)
397 int print_errors
= d
->opterr
;
398 if (optstring
[0] == ':')
406 if (d
->optind
== 0 || !d
->__initialized
)
409 d
->optind
= 1; /* Don't scan ARGV[0], the program name. */
410 optstring
= _getopt_initialize (argc
, argv
, optstring
, d
,
412 d
->__initialized
= 1;
415 /* Test whether ARGV[optind] points to a non-option argument.
416 Either it does not have option syntax, or there is an environment flag
417 from the shell indicating it is not an option. The later information
418 is only used when the used in the GNU libc. */
419 #if defined _LIBC && defined USE_NONOPTION_FLAGS
420 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
421 || (d->optind < d->__nonoption_flags_len \
422 && __getopt_nonoption_flags[d->optind] == '1'))
424 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
427 if (d
->__nextchar
== NULL
|| *d
->__nextchar
== '\0')
429 /* Advance to the next ARGV-element. */
431 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
432 moved back by the user (who may also have changed the arguments). */
433 if (d
->__last_nonopt
> d
->optind
)
434 d
->__last_nonopt
= d
->optind
;
435 if (d
->__first_nonopt
> d
->optind
)
436 d
->__first_nonopt
= d
->optind
;
438 if (d
->__ordering
== PERMUTE
)
440 /* If we have just processed some options following some non-options,
441 exchange them so that the options come first. */
443 if (d
->__first_nonopt
!= d
->__last_nonopt
444 && d
->__last_nonopt
!= d
->optind
)
445 exchange ((char **) argv
, d
);
446 else if (d
->__last_nonopt
!= d
->optind
)
447 d
->__first_nonopt
= d
->optind
;
449 /* Skip any additional non-options
450 and extend the range of non-options previously skipped. */
452 while (d
->optind
< argc
&& NONOPTION_P
)
454 d
->__last_nonopt
= d
->optind
;
457 /* The special ARGV-element `--' means premature end of options.
458 Skip it like a null option,
459 then exchange with previous non-options as if it were an option,
460 then skip everything else like a non-option. */
462 if (d
->optind
!= argc
&& !strcmp (argv
[d
->optind
], "--"))
466 if (d
->__first_nonopt
!= d
->__last_nonopt
467 && d
->__last_nonopt
!= d
->optind
)
468 exchange ((char **) argv
, d
);
469 else if (d
->__first_nonopt
== d
->__last_nonopt
)
470 d
->__first_nonopt
= d
->optind
;
471 d
->__last_nonopt
= argc
;
476 /* If we have done all the ARGV-elements, stop the scan
477 and back over any non-options that we skipped and permuted. */
479 if (d
->optind
== argc
)
481 /* Set the next-arg-index to point at the non-options
482 that we previously skipped, so the caller will digest them. */
483 if (d
->__first_nonopt
!= d
->__last_nonopt
)
484 d
->optind
= d
->__first_nonopt
;
488 /* If we have come to a non-option and did not permute it,
489 either stop the scan or describe it to the caller and pass it by. */
493 if (d
->__ordering
== REQUIRE_ORDER
)
495 d
->optarg
= argv
[d
->optind
++];
499 /* We have found another option-ARGV-element.
500 Skip the initial punctuation. */
502 d
->__nextchar
= (argv
[d
->optind
] + 1
503 + (longopts
!= NULL
&& argv
[d
->optind
][1] == '-'));
506 /* Decode the current option-ARGV-element. */
508 /* Check whether the ARGV-element is a long option.
510 If long_only and the ARGV-element has the form "-f", where f is
511 a valid short option, don't consider it an abbreviated form of
512 a long option that starts with f. Otherwise there would be no
513 way to give the -f short option.
515 On the other hand, if there's a long option "fubar" and
516 the ARGV-element is "-fu", do consider that an abbreviation of
517 the long option, just like "--fu", and not "-f" with arg "u".
519 This distinction seems to be the most useful approach. */
522 && (argv
[d
->optind
][1] == '-'
523 || (long_only
&& (argv
[d
->optind
][2]
524 || !strchr (optstring
, argv
[d
->optind
][1])))))
527 const struct option
*p
;
528 const struct option
*pfound
= NULL
;
534 for (nameend
= d
->__nextchar
; *nameend
&& *nameend
!= '='; nameend
++)
537 /* Test all long options for either exact match
538 or abbreviated matches. */
539 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
540 if (!strncmp (p
->name
, d
->__nextchar
, nameend
- d
->__nextchar
))
542 if ((unsigned int) (nameend
- d
->__nextchar
)
543 == (unsigned int) strlen (p
->name
))
545 /* Exact match found. */
547 indfound
= option_index
;
551 else if (pfound
== NULL
)
553 /* First nonexact match found. */
555 indfound
= option_index
;
558 || pfound
->has_arg
!= p
->has_arg
559 || pfound
->flag
!= p
->flag
560 || pfound
->val
!= p
->val
)
561 /* Second or later nonexact match found. */
569 #if defined _LIBC && defined USE_IN_LIBIO
572 if (__asprintf (&buf
, _("%s: option '%s' is ambiguous\n"),
573 argv
[0], argv
[d
->optind
]) >= 0)
575 _IO_flockfile (stderr
);
577 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
578 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
580 __fxprintf (NULL
, "%s", buf
);
582 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
583 _IO_funlockfile (stderr
);
588 fprintf (stderr
, _("%s: option '%s' is ambiguous\n"),
589 argv
[0], argv
[d
->optind
]);
592 d
->__nextchar
+= strlen (d
->__nextchar
);
600 option_index
= indfound
;
604 /* Don't test has_arg with >, because some C compilers don't
605 allow it to be used on enums. */
607 d
->optarg
= nameend
+ 1;
612 #if defined _LIBC && defined USE_IN_LIBIO
617 if (argv
[d
->optind
- 1][1] == '-')
620 #if defined _LIBC && defined USE_IN_LIBIO
621 n
= __asprintf (&buf
, _("\
622 %s: option '--%s' doesn't allow an argument\n"),
623 argv
[0], pfound
->name
);
625 fprintf (stderr
, _("\
626 %s: option '--%s' doesn't allow an argument\n"),
627 argv
[0], pfound
->name
);
632 /* +option or -option */
633 #if defined _LIBC && defined USE_IN_LIBIO
634 n
= __asprintf (&buf
, _("\
635 %s: option '%c%s' doesn't allow an argument\n"),
636 argv
[0], argv
[d
->optind
- 1][0],
639 fprintf (stderr
, _("\
640 %s: option '%c%s' doesn't allow an argument\n"),
641 argv
[0], argv
[d
->optind
- 1][0],
646 #if defined _LIBC && defined USE_IN_LIBIO
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
);
665 d
->__nextchar
+= strlen (d
->__nextchar
);
667 d
->optopt
= pfound
->val
;
671 else if (pfound
->has_arg
== 1)
673 if (d
->optind
< argc
)
674 d
->optarg
= argv
[d
->optind
++];
679 #if defined _LIBC && defined USE_IN_LIBIO
682 if (__asprintf (&buf
, _("\
683 %s: option '--%s' requires an argument\n"),
684 argv
[0], pfound
->name
) >= 0)
686 _IO_flockfile (stderr
);
688 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
689 ((_IO_FILE
*) stderr
)->_flags2
690 |= _IO_FLAGS2_NOTCANCEL
;
692 __fxprintf (NULL
, "%s", buf
);
694 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
695 _IO_funlockfile (stderr
);
701 _("%s: option '--%s' requires an argument\n"),
702 argv
[0], pfound
->name
);
705 d
->__nextchar
+= strlen (d
->__nextchar
);
706 d
->optopt
= pfound
->val
;
707 return optstring
[0] == ':' ? ':' : '?';
710 d
->__nextchar
+= strlen (d
->__nextchar
);
712 *longind
= option_index
;
715 *(pfound
->flag
) = pfound
->val
;
721 /* Can't find it as a long option. If this is not getopt_long_only,
722 or the option starts with '--' or is not a valid short
723 option, then it's an error.
724 Otherwise interpret it as a short option. */
725 if (!long_only
|| argv
[d
->optind
][1] == '-'
726 || strchr (optstring
, *d
->__nextchar
) == NULL
)
730 #if defined _LIBC && defined USE_IN_LIBIO
735 if (argv
[d
->optind
][1] == '-')
738 #if defined _LIBC && defined USE_IN_LIBIO
739 n
= __asprintf (&buf
, _("%s: unrecognized option '--%s'\n"),
740 argv
[0], d
->__nextchar
);
742 fprintf (stderr
, _("%s: unrecognized option '--%s'\n"),
743 argv
[0], d
->__nextchar
);
748 /* +option or -option */
749 #if defined _LIBC && defined USE_IN_LIBIO
750 n
= __asprintf (&buf
, _("%s: unrecognized option '%c%s'\n"),
751 argv
[0], argv
[d
->optind
][0], d
->__nextchar
);
753 fprintf (stderr
, _("%s: unrecognized option '%c%s'\n"),
754 argv
[0], argv
[d
->optind
][0], d
->__nextchar
);
758 #if defined _LIBC && defined USE_IN_LIBIO
761 _IO_flockfile (stderr
);
763 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
764 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
766 __fxprintf (NULL
, "%s", buf
);
768 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
769 _IO_funlockfile (stderr
);
775 d
->__nextchar
= (char *) "";
782 /* Look at and handle the next short option-character. */
785 char c
= *d
->__nextchar
++;
786 char *temp
= strchr (optstring
, c
);
788 /* Increment `optind' when we start to process its last character. */
789 if (*d
->__nextchar
== '\0')
792 if (temp
== NULL
|| c
== ':')
796 #if defined _LIBC && defined USE_IN_LIBIO
801 #if defined _LIBC && defined USE_IN_LIBIO
802 n
= __asprintf (&buf
, _("%s: invalid option -- '%c'\n"),
805 fprintf (stderr
, _("%s: invalid option -- '%c'\n"), argv
[0], c
);
808 #if defined _LIBC && defined USE_IN_LIBIO
811 _IO_flockfile (stderr
);
813 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
814 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
816 __fxprintf (NULL
, "%s", buf
);
818 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
819 _IO_funlockfile (stderr
);
828 /* Convenience. Treat POSIX -W foo same as long option --foo */
829 if (temp
[0] == 'W' && temp
[1] == ';')
832 const struct option
*p
;
833 const struct option
*pfound
= NULL
;
839 /* This is an option that requires an argument. */
840 if (*d
->__nextchar
!= '\0')
842 d
->optarg
= d
->__nextchar
;
843 /* If we end this ARGV-element by taking the rest as an arg,
844 we must advance to the next element now. */
847 else if (d
->optind
== argc
)
851 #if defined _LIBC && defined USE_IN_LIBIO
854 if (__asprintf (&buf
,
855 _("%s: option requires an argument -- '%c'\n"),
858 _IO_flockfile (stderr
);
860 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
861 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
863 __fxprintf (NULL
, "%s", buf
);
865 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
866 _IO_funlockfile (stderr
);
872 _("%s: option requires an argument -- '%c'\n"),
877 if (optstring
[0] == ':')
884 /* We already incremented `d->optind' once;
885 increment it again when taking next ARGV-elt as argument. */
886 d
->optarg
= argv
[d
->optind
++];
888 /* optarg is now the argument, see if it's in the
889 table of longopts. */
891 for (d
->__nextchar
= nameend
= d
->optarg
; *nameend
&& *nameend
!= '=';
895 /* Test all long options for either exact match
896 or abbreviated matches. */
897 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
898 if (!strncmp (p
->name
, d
->__nextchar
, nameend
- d
->__nextchar
))
900 if ((unsigned int) (nameend
- d
->__nextchar
) == strlen (p
->name
))
902 /* Exact match found. */
904 indfound
= option_index
;
908 else if (pfound
== NULL
)
910 /* First nonexact match found. */
912 indfound
= option_index
;
915 || pfound
->has_arg
!= p
->has_arg
916 || pfound
->flag
!= p
->flag
917 || pfound
->val
!= p
->val
)
918 /* Second or later nonexact match found. */
925 #if defined _LIBC && defined USE_IN_LIBIO
928 if (__asprintf (&buf
, _("%s: option '-W %s' is ambiguous\n"),
929 argv
[0], d
->optarg
) >= 0)
931 _IO_flockfile (stderr
);
933 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
934 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
936 __fxprintf (NULL
, "%s", buf
);
938 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
939 _IO_funlockfile (stderr
);
944 fprintf (stderr
, _("%s: option '-W %s' is ambiguous\n"),
948 d
->__nextchar
+= strlen (d
->__nextchar
);
954 option_index
= indfound
;
957 /* Don't test has_arg with >, because some C compilers don't
958 allow it to be used on enums. */
960 d
->optarg
= nameend
+ 1;
965 #if defined _LIBC && defined USE_IN_LIBIO
968 if (__asprintf (&buf
, _("\
969 %s: option '-W %s' doesn't allow an argument\n"),
970 argv
[0], pfound
->name
) >= 0)
972 _IO_flockfile (stderr
);
974 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
975 ((_IO_FILE
*) stderr
)->_flags2
976 |= _IO_FLAGS2_NOTCANCEL
;
978 __fxprintf (NULL
, "%s", buf
);
980 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
981 _IO_funlockfile (stderr
);
986 fprintf (stderr
, _("\
987 %s: option '-W %s' doesn't allow an argument\n"),
988 argv
[0], pfound
->name
);
992 d
->__nextchar
+= strlen (d
->__nextchar
);
996 else if (pfound
->has_arg
== 1)
998 if (d
->optind
< argc
)
999 d
->optarg
= argv
[d
->optind
++];
1004 #if defined _LIBC && defined USE_IN_LIBIO
1007 if (__asprintf (&buf
, _("\
1008 %s: option '-W %s' requires an argument\n"),
1009 argv
[0], pfound
->name
) >= 0)
1011 _IO_flockfile (stderr
);
1013 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
1014 ((_IO_FILE
*) stderr
)->_flags2
1015 |= _IO_FLAGS2_NOTCANCEL
;
1017 __fxprintf (NULL
, "%s", buf
);
1019 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
1020 _IO_funlockfile (stderr
);
1025 fprintf (stderr
, _("\
1026 %s: option '-W %s' requires an argument\n"),
1027 argv
[0], pfound
->name
);
1030 d
->__nextchar
+= strlen (d
->__nextchar
);
1031 return optstring
[0] == ':' ? ':' : '?';
1036 d
->__nextchar
+= strlen (d
->__nextchar
);
1037 if (longind
!= NULL
)
1038 *longind
= option_index
;
1041 *(pfound
->flag
) = pfound
->val
;
1046 d
->__nextchar
= NULL
;
1047 return 'W'; /* Let the application handle it. */
1053 /* This is an option that accepts an argument optionally. */
1054 if (*d
->__nextchar
!= '\0')
1056 d
->optarg
= d
->__nextchar
;
1061 d
->__nextchar
= NULL
;
1065 /* This is an option that requires an argument. */
1066 if (*d
->__nextchar
!= '\0')
1068 d
->optarg
= d
->__nextchar
;
1069 /* If we end this ARGV-element by taking the rest as an arg,
1070 we must advance to the next element now. */
1073 else if (d
->optind
== argc
)
1077 #if defined _LIBC && defined USE_IN_LIBIO
1080 if (__asprintf (&buf
, _("\
1081 %s: option requires an argument -- '%c'\n"),
1084 _IO_flockfile (stderr
);
1086 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
1087 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
1089 __fxprintf (NULL
, "%s", buf
);
1091 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
1092 _IO_funlockfile (stderr
);
1098 _("%s: option requires an argument -- '%c'\n"),
1103 if (optstring
[0] == ':')
1109 /* We already incremented `optind' once;
1110 increment it again when taking next ARGV-elt as argument. */
1111 d
->optarg
= argv
[d
->optind
++];
1112 d
->__nextchar
= NULL
;
1120 _getopt_internal (int argc
, char *const *argv
, const char *optstring
,
1121 const struct option
*longopts
, int *longind
, int long_only
,
1122 int posixly_correct
)
1126 getopt_data
.optind
= optind
;
1127 getopt_data
.opterr
= opterr
;
1129 result
= _getopt_internal_r (argc
, argv
, optstring
, longopts
,
1130 longind
, long_only
, &getopt_data
,
1133 optind
= getopt_data
.optind
;
1134 optarg
= getopt_data
.optarg
;
1135 optopt
= getopt_data
.optopt
;
1141 getopt (int argc
, char *const *argv
, const char *optstring
)
1143 return _getopt_internal (argc
, argv
, optstring
,
1144 (const struct option
*) 0,
1151 __posix_getopt (int argc
, char *const *argv
, const char *optstring
)
1153 return _getopt_internal (argc
, argv
, optstring
,
1154 (const struct option
*) 0,
1160 #endif /* Not ELIDE_CODE. */
1164 /* Compile with -DTEST to make an executable for use in testing
1165 the above definition of `getopt'. */
1168 main (int argc
, char **argv
)
1171 int digit_optind
= 0;
1175 int this_option_optind
= optind
? optind
: 1;
1177 c
= getopt (argc
, argv
, "abc:d:0123456789");
1193 if (digit_optind
!= 0 && digit_optind
!= this_option_optind
)
1194 printf ("digits occur in two different argv-elements.\n");
1195 digit_optind
= this_option_optind
;
1196 printf ("option %c\n", c
);
1200 printf ("option a\n");
1204 printf ("option b\n");
1208 printf ("option c with value '%s'\n", optarg
);
1215 printf ("?? getopt returned character code 0%o ??\n", c
);
1221 printf ("non-option ARGV-elements: ");
1222 while (optind
< argc
)
1223 printf ("%s ", argv
[optind
++]);