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 Free Software Foundation, Inc.
6 This file is part of the GNU C Library.
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 The GNU C Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with the GNU C Library; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
24 Ditto for AIX 3.2 and <stdlib.h>. */
35 /* Comment out all this code if we are using the GNU C Library, and are not
36 actually compiling the library itself. This code is part of the GNU C
37 Library, but also included in many other GNU distributions. Compiling
38 and linking in this code is a waste when using the GNU C library
39 (especially if it is a shared library). Rather than having every GNU
40 program understand `configure --with-gnu-libc' and omit the object files,
41 it is simpler to just do this in the source for each such file. */
43 #define GETOPT_INTERFACE_VERSION 2
44 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
45 # include <gnu-versions.h>
46 # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
54 /* This needs to come after some library #include
55 to get __GNU_LIBRARY__ defined. */
56 #ifdef __GNU_LIBRARY__
57 /* Don't include stdlib.h for non-GNU C libraries because some of them
58 contain conflicting prototypes for getopt. */
61 #endif /* GNU C library. */
73 # define _(msgid) gettext (msgid)
76 #if defined _LIBC && defined USE_IN_LIBIO
80 #ifndef attribute_hidden
81 # define attribute_hidden
84 /* This version of `getopt' appears to the caller like standard Unix `getopt'
85 but it behaves differently for the user, since it allows the user
86 to intersperse the options with the other arguments.
88 As `getopt' works, it permutes the elements of ARGV so that,
89 when it is done, all the options precede everything else. Thus
90 all application programs are extended to handle flexible argument order.
92 Setting the environment variable POSIXLY_CORRECT disables permutation.
93 Then the behavior is completely standard.
95 GNU application programs can use a third alternative mode in which
96 they can distinguish the relative order of options and other arguments. */
99 #include "getopt_int.h"
101 /* For communication from `getopt' to the caller.
102 When `getopt' finds an option that takes an argument,
103 the argument value is returned here.
104 Also, when `ordering' is RETURN_IN_ORDER,
105 each non-option ARGV-element is returned here. */
109 /* Index in ARGV of the next element to be scanned.
110 This is used for communication to and from the caller
111 and for communication between successive calls to `getopt'.
113 On entry to `getopt', zero means this is the first call; initialize.
115 When `getopt' returns -1, this is the index of the first of the
116 non-option elements that the caller should itself scan.
118 Otherwise, `optind' communicates from one call to the next
119 how much of ARGV has been scanned so far. */
121 /* 1003.2 says this must be 1 before any call. */
124 /* Callers store zero here to inhibit the error message
125 for unrecognized options. */
129 /* Set to an option character which was unrecognized.
130 This must be initialized on some systems to avoid linking in the
131 system's own getopt implementation. */
135 /* Keep a global copy of all internal members of getopt_data. */
137 static struct _getopt_data getopt_data
;
140 #ifndef __GNU_LIBRARY__
142 /* Avoid depending on library functions or files
143 whose names are inconsistent. */
146 extern char *getenv ();
149 #endif /* not __GNU_LIBRARY__ */
152 /* Stored original parameters.
153 XXX This is no good solution. We should rather copy the args so
154 that we can compare them later. But we must not use malloc(3). */
155 extern int __libc_argc
;
156 extern char **__libc_argv
;
158 /* Bash 2.0 gives us an environment variable containing flags
159 indicating ARGV elements that should not be considered arguments. */
161 # ifdef USE_NONOPTION_FLAGS
162 /* Defined in getopt_init.c */
163 extern char *__getopt_nonoption_flags
;
166 # ifdef USE_NONOPTION_FLAGS
167 # define SWAP_FLAGS(ch1, ch2) \
168 if (d->__nonoption_flags_len > 0) \
170 char __tmp = __getopt_nonoption_flags[ch1]; \
171 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
172 __getopt_nonoption_flags[ch2] = __tmp; \
175 # define SWAP_FLAGS(ch1, ch2)
178 # define SWAP_FLAGS(ch1, ch2)
181 /* Exchange two adjacent subsequences of ARGV.
182 One subsequence is elements [first_nonopt,last_nonopt)
183 which contains all the non-options that have been skipped so far.
184 The other is elements [last_nonopt,optind), which contains all
185 the options processed since those non-options were skipped.
187 `first_nonopt' and `last_nonopt' are relocated so that they describe
188 the new indices of the non-options in ARGV after they are moved. */
191 exchange (char **argv
, struct _getopt_data
*d
)
193 int bottom
= d
->__first_nonopt
;
194 int middle
= d
->__last_nonopt
;
198 /* Exchange the shorter segment with the far end of the longer segment.
199 That puts the shorter segment into the right place.
200 It leaves the longer segment in the right place overall,
201 but it consists of two parts that need to be swapped next. */
203 #if defined _LIBC && defined USE_NONOPTION_FLAGS
204 /* First make sure the handling of the `__getopt_nonoption_flags'
205 string can work normally. Our top argument must be in the range
207 if (d
->__nonoption_flags_len
> 0 && top
>= d
->__nonoption_flags_max_len
)
209 /* We must extend the array. The user plays games with us and
210 presents new arguments. */
211 char *new_str
= malloc (top
+ 1);
213 d
->__nonoption_flags_len
= d
->__nonoption_flags_max_len
= 0;
216 memset (__mempcpy (new_str
, __getopt_nonoption_flags
,
217 d
->__nonoption_flags_max_len
),
218 '\0', top
+ 1 - d
->__nonoption_flags_max_len
);
219 d
->__nonoption_flags_max_len
= top
+ 1;
220 __getopt_nonoption_flags
= new_str
;
225 while (top
> middle
&& middle
> bottom
)
227 if (top
- middle
> middle
- bottom
)
229 /* Bottom segment is the short one. */
230 int len
= middle
- bottom
;
233 /* Swap it with the top part of the top segment. */
234 for (i
= 0; i
< len
; i
++)
236 tem
= argv
[bottom
+ i
];
237 argv
[bottom
+ i
] = argv
[top
- (middle
- bottom
) + i
];
238 argv
[top
- (middle
- bottom
) + i
] = tem
;
239 SWAP_FLAGS (bottom
+ i
, top
- (middle
- bottom
) + i
);
241 /* Exclude the moved bottom segment from further swapping. */
246 /* Top segment is the short one. */
247 int len
= top
- middle
;
250 /* Swap it with the bottom part of the bottom segment. */
251 for (i
= 0; i
< len
; i
++)
253 tem
= argv
[bottom
+ i
];
254 argv
[bottom
+ i
] = argv
[middle
+ i
];
255 argv
[middle
+ i
] = tem
;
256 SWAP_FLAGS (bottom
+ i
, middle
+ i
);
258 /* Exclude the moved top segment from further swapping. */
263 /* Update records for the slots the non-options now occupy. */
265 d
->__first_nonopt
+= (d
->optind
- d
->__last_nonopt
);
266 d
->__last_nonopt
= d
->optind
;
269 /* Initialize the internal data when the first call is made. */
272 _getopt_initialize (int argc
, char *const *argv
, const char *optstring
,
273 struct _getopt_data
*d
, int posixly_correct
)
275 /* Start processing options with ARGV-element 1 (since ARGV-element 0
276 is the program name); the sequence of previously skipped
277 non-option ARGV-elements is empty. */
279 d
->__first_nonopt
= d
->__last_nonopt
= d
->optind
;
281 d
->__nextchar
= NULL
;
283 d
->__posixly_correct
= posixly_correct
| !!getenv ("POSIXLY_CORRECT");
285 /* Determine how to handle the ordering of options and nonoptions. */
287 if (optstring
[0] == '-')
289 d
->__ordering
= RETURN_IN_ORDER
;
292 else if (optstring
[0] == '+')
294 d
->__ordering
= REQUIRE_ORDER
;
297 else if (d
->__posixly_correct
)
298 d
->__ordering
= REQUIRE_ORDER
;
300 d
->__ordering
= PERMUTE
;
302 #if defined _LIBC && defined USE_NONOPTION_FLAGS
303 if (!d
->__posixly_correct
304 && argc
== __libc_argc
&& argv
== __libc_argv
)
306 if (d
->__nonoption_flags_max_len
== 0)
308 if (__getopt_nonoption_flags
== NULL
309 || __getopt_nonoption_flags
[0] == '\0')
310 d
->__nonoption_flags_max_len
= -1;
313 const char *orig_str
= __getopt_nonoption_flags
;
314 int len
= d
->__nonoption_flags_max_len
= strlen (orig_str
);
315 if (d
->__nonoption_flags_max_len
< argc
)
316 d
->__nonoption_flags_max_len
= argc
;
317 __getopt_nonoption_flags
=
318 (char *) malloc (d
->__nonoption_flags_max_len
);
319 if (__getopt_nonoption_flags
== NULL
)
320 d
->__nonoption_flags_max_len
= -1;
322 memset (__mempcpy (__getopt_nonoption_flags
, orig_str
, len
),
323 '\0', d
->__nonoption_flags_max_len
- len
);
326 d
->__nonoption_flags_len
= d
->__nonoption_flags_max_len
;
329 d
->__nonoption_flags_len
= 0;
335 /* Scan elements of ARGV (whose length is ARGC) for option characters
338 If an element of ARGV starts with '-', and is not exactly "-" or "--",
339 then it is an option element. The characters of this element
340 (aside from the initial '-') are option characters. If `getopt'
341 is called repeatedly, it returns successively each of the option characters
342 from each of the option elements.
344 If `getopt' finds another option character, it returns that character,
345 updating `optind' and `nextchar' so that the next call to `getopt' can
346 resume the scan with the following option character or ARGV-element.
348 If there are no more option characters, `getopt' returns -1.
349 Then `optind' is the index in ARGV of the first ARGV-element
350 that is not an option. (The ARGV-elements have been permuted
351 so that those that are not options now come last.)
353 OPTSTRING is a string containing the legitimate option characters.
354 If an option character is seen that is not listed in OPTSTRING,
355 return '?' after printing an error message. If you set `opterr' to
356 zero, the error message is suppressed but we still return '?'.
358 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
359 so the following text in the same ARGV-element, or the text of the following
360 ARGV-element, is returned in `optarg'. Two colons mean an option that
361 wants an optional arg; if there is text in the current ARGV-element,
362 it is returned in `optarg', otherwise `optarg' is set to zero.
364 If OPTSTRING starts with `-' or `+', it requests different methods of
365 handling the non-option ARGV-elements.
366 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
368 Long-named options begin with `--' instead of `-'.
369 Their names may be abbreviated as long as the abbreviation is unique
370 or is an exact match for some defined option. If they have an
371 argument, it follows the option name in the same ARGV-element, separated
372 from the option name by a `=', or else the in next ARGV-element.
373 When `getopt' finds a long-named option, it returns 0 if that option's
374 `flag' field is nonzero, the value of the option's `val' field
375 if the `flag' field is zero.
377 The elements of ARGV aren't really const, because we permute them.
378 But we pretend they're const in the prototype to be compatible
381 LONGOPTS is a vector of `struct option' terminated by an
382 element containing a name which is zero.
384 LONGIND returns the index in LONGOPT of the long-named option found.
385 It is only valid when a long-named option has been found by the most
388 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
389 long-named options. */
392 _getopt_internal_r (int argc
, char *const *argv
, const char *optstring
,
393 const struct option
*longopts
, int *longind
,
394 int long_only
, struct _getopt_data
*d
, int posixly_correct
)
396 int print_errors
= d
->opterr
;
397 if (optstring
[0] == ':')
405 if (d
->optind
== 0 || !d
->__initialized
)
408 d
->optind
= 1; /* Don't scan ARGV[0], the program name. */
409 optstring
= _getopt_initialize (argc
, argv
, optstring
, d
,
411 d
->__initialized
= 1;
414 /* Test whether ARGV[optind] points to a non-option argument.
415 Either it does not have option syntax, or there is an environment flag
416 from the shell indicating it is not an option. The later information
417 is only used when the used in the GNU libc. */
418 #if defined _LIBC && defined USE_NONOPTION_FLAGS
419 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
420 || (d->optind < d->__nonoption_flags_len \
421 && __getopt_nonoption_flags[d->optind] == '1'))
423 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
426 if (d
->__nextchar
== NULL
|| *d
->__nextchar
== '\0')
428 /* Advance to the next ARGV-element. */
430 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
431 moved back by the user (who may also have changed the arguments). */
432 if (d
->__last_nonopt
> d
->optind
)
433 d
->__last_nonopt
= d
->optind
;
434 if (d
->__first_nonopt
> d
->optind
)
435 d
->__first_nonopt
= d
->optind
;
437 if (d
->__ordering
== PERMUTE
)
439 /* If we have just processed some options following some non-options,
440 exchange them so that the options come first. */
442 if (d
->__first_nonopt
!= d
->__last_nonopt
443 && d
->__last_nonopt
!= d
->optind
)
444 exchange ((char **) argv
, d
);
445 else if (d
->__last_nonopt
!= d
->optind
)
446 d
->__first_nonopt
= d
->optind
;
448 /* Skip any additional non-options
449 and extend the range of non-options previously skipped. */
451 while (d
->optind
< argc
&& NONOPTION_P
)
453 d
->__last_nonopt
= d
->optind
;
456 /* The special ARGV-element `--' means premature end of options.
457 Skip it like a null option,
458 then exchange with previous non-options as if it were an option,
459 then skip everything else like a non-option. */
461 if (d
->optind
!= argc
&& !strcmp (argv
[d
->optind
], "--"))
465 if (d
->__first_nonopt
!= d
->__last_nonopt
466 && d
->__last_nonopt
!= d
->optind
)
467 exchange ((char **) argv
, d
);
468 else if (d
->__first_nonopt
== d
->__last_nonopt
)
469 d
->__first_nonopt
= d
->optind
;
470 d
->__last_nonopt
= argc
;
475 /* If we have done all the ARGV-elements, stop the scan
476 and back over any non-options that we skipped and permuted. */
478 if (d
->optind
== argc
)
480 /* Set the next-arg-index to point at the non-options
481 that we previously skipped, so the caller will digest them. */
482 if (d
->__first_nonopt
!= d
->__last_nonopt
)
483 d
->optind
= d
->__first_nonopt
;
487 /* If we have come to a non-option and did not permute it,
488 either stop the scan or describe it to the caller and pass it by. */
492 if (d
->__ordering
== REQUIRE_ORDER
)
494 d
->optarg
= argv
[d
->optind
++];
498 /* We have found another option-ARGV-element.
499 Skip the initial punctuation. */
501 d
->__nextchar
= (argv
[d
->optind
] + 1
502 + (longopts
!= NULL
&& argv
[d
->optind
][1] == '-'));
505 /* Decode the current option-ARGV-element. */
507 /* Check whether the ARGV-element is a long option.
509 If long_only and the ARGV-element has the form "-f", where f is
510 a valid short option, don't consider it an abbreviated form of
511 a long option that starts with f. Otherwise there would be no
512 way to give the -f short option.
514 On the other hand, if there's a long option "fubar" and
515 the ARGV-element is "-fu", do consider that an abbreviation of
516 the long option, just like "--fu", and not "-f" with arg "u".
518 This distinction seems to be the most useful approach. */
521 && (argv
[d
->optind
][1] == '-'
522 || (long_only
&& (argv
[d
->optind
][2]
523 || !strchr (optstring
, argv
[d
->optind
][1])))))
526 const struct option
*p
;
527 const struct option
*pfound
= NULL
;
533 for (nameend
= d
->__nextchar
; *nameend
&& *nameend
!= '='; nameend
++)
536 /* Test all long options for either exact match
537 or abbreviated matches. */
538 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
539 if (!strncmp (p
->name
, d
->__nextchar
, nameend
- d
->__nextchar
))
541 if ((unsigned int) (nameend
- d
->__nextchar
)
542 == (unsigned int) strlen (p
->name
))
544 /* Exact match found. */
546 indfound
= option_index
;
550 else if (pfound
== NULL
)
552 /* First nonexact match found. */
554 indfound
= option_index
;
557 || pfound
->has_arg
!= p
->has_arg
558 || pfound
->flag
!= p
->flag
559 || pfound
->val
!= p
->val
)
560 /* Second or later nonexact match found. */
568 #if defined _LIBC && defined USE_IN_LIBIO
571 if (__asprintf (&buf
, _("%s: option '%s' is ambiguous\n"),
572 argv
[0], argv
[d
->optind
]) >= 0)
574 _IO_flockfile (stderr
);
576 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
577 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
579 __fxprintf (NULL
, "%s", buf
);
581 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
582 _IO_funlockfile (stderr
);
587 fprintf (stderr
, _("%s: option '%s' is ambiguous\n"),
588 argv
[0], argv
[d
->optind
]);
591 d
->__nextchar
+= strlen (d
->__nextchar
);
599 option_index
= indfound
;
603 /* Don't test has_arg with >, because some C compilers don't
604 allow it to be used on enums. */
606 d
->optarg
= nameend
+ 1;
611 #if defined _LIBC && defined USE_IN_LIBIO
616 if (argv
[d
->optind
- 1][1] == '-')
619 #if defined _LIBC && defined USE_IN_LIBIO
620 n
= __asprintf (&buf
, _("\
621 %s: option '--%s' doesn't allow an argument\n"),
622 argv
[0], pfound
->name
);
624 fprintf (stderr
, _("\
625 %s: option '--%s' doesn't allow an argument\n"),
626 argv
[0], pfound
->name
);
631 /* +option or -option */
632 #if defined _LIBC && defined USE_IN_LIBIO
633 n
= __asprintf (&buf
, _("\
634 %s: option '%c%s' doesn't allow an argument\n"),
635 argv
[0], argv
[d
->optind
- 1][0],
638 fprintf (stderr
, _("\
639 %s: option '%c%s' doesn't allow an argument\n"),
640 argv
[0], argv
[d
->optind
- 1][0],
645 #if defined _LIBC && defined USE_IN_LIBIO
648 _IO_flockfile (stderr
);
650 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
651 ((_IO_FILE
*) stderr
)->_flags2
652 |= _IO_FLAGS2_NOTCANCEL
;
654 __fxprintf (NULL
, "%s", buf
);
656 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
657 _IO_funlockfile (stderr
);
664 d
->__nextchar
+= strlen (d
->__nextchar
);
666 d
->optopt
= pfound
->val
;
670 else if (pfound
->has_arg
== 1)
672 if (d
->optind
< argc
)
673 d
->optarg
= argv
[d
->optind
++];
678 #if defined _LIBC && defined USE_IN_LIBIO
681 if (__asprintf (&buf
, _("\
682 %s: option '%s' requires an argument\n"),
683 argv
[0], argv
[d
->optind
- 1]) >= 0)
685 _IO_flockfile (stderr
);
687 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
688 ((_IO_FILE
*) stderr
)->_flags2
689 |= _IO_FLAGS2_NOTCANCEL
;
691 __fxprintf (NULL
, "%s", buf
);
693 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
694 _IO_funlockfile (stderr
);
700 _("%s: option '%s' requires an argument\n"),
701 argv
[0], argv
[d
->optind
- 1]);
704 d
->__nextchar
+= strlen (d
->__nextchar
);
705 d
->optopt
= pfound
->val
;
706 return optstring
[0] == ':' ? ':' : '?';
709 d
->__nextchar
+= strlen (d
->__nextchar
);
711 *longind
= option_index
;
714 *(pfound
->flag
) = pfound
->val
;
720 /* Can't find it as a long option. If this is not getopt_long_only,
721 or the option starts with '--' or is not a valid short
722 option, then it's an error.
723 Otherwise interpret it as a short option. */
724 if (!long_only
|| argv
[d
->optind
][1] == '-'
725 || strchr (optstring
, *d
->__nextchar
) == NULL
)
729 #if defined _LIBC && defined USE_IN_LIBIO
734 if (argv
[d
->optind
][1] == '-')
737 #if defined _LIBC && defined USE_IN_LIBIO
738 n
= __asprintf (&buf
, _("%s: unrecognized option '--%s'\n"),
739 argv
[0], d
->__nextchar
);
741 fprintf (stderr
, _("%s: unrecognized option '--%s'\n"),
742 argv
[0], d
->__nextchar
);
747 /* +option or -option */
748 #if defined _LIBC && defined USE_IN_LIBIO
749 n
= __asprintf (&buf
, _("%s: unrecognized option '%c%s'\n"),
750 argv
[0], argv
[d
->optind
][0], d
->__nextchar
);
752 fprintf (stderr
, _("%s: unrecognized option '%c%s'\n"),
753 argv
[0], argv
[d
->optind
][0], d
->__nextchar
);
757 #if defined _LIBC && defined USE_IN_LIBIO
760 _IO_flockfile (stderr
);
762 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
763 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
765 __fxprintf (NULL
, "%s", buf
);
767 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
768 _IO_funlockfile (stderr
);
774 d
->__nextchar
= (char *) "";
781 /* Look at and handle the next short option-character. */
784 char c
= *d
->__nextchar
++;
785 char *temp
= strchr (optstring
, c
);
787 /* Increment `optind' when we start to process its last character. */
788 if (*d
->__nextchar
== '\0')
791 if (temp
== NULL
|| c
== ':')
795 #if defined _LIBC && defined USE_IN_LIBIO
800 #if defined _LIBC && defined USE_IN_LIBIO
801 n
= __asprintf (&buf
, _("%s: invalid option -- '%c'\n"),
804 fprintf (stderr
, _("%s: invalid option -- '%c'\n"), argv
[0], c
);
807 #if defined _LIBC && defined USE_IN_LIBIO
810 _IO_flockfile (stderr
);
812 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
813 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
815 __fxprintf (NULL
, "%s", buf
);
817 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
818 _IO_funlockfile (stderr
);
827 /* Convenience. Treat POSIX -W foo same as long option --foo */
828 if (temp
[0] == 'W' && temp
[1] == ';')
831 const struct option
*p
;
832 const struct option
*pfound
= NULL
;
838 /* This is an option that requires an argument. */
839 if (*d
->__nextchar
!= '\0')
841 d
->optarg
= d
->__nextchar
;
842 /* If we end this ARGV-element by taking the rest as an arg,
843 we must advance to the next element now. */
846 else if (d
->optind
== argc
)
850 #if defined _LIBC && defined USE_IN_LIBIO
853 if (__asprintf (&buf
,
854 _("%s: option requires an argument -- '%c'\n"),
857 _IO_flockfile (stderr
);
859 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
860 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
862 __fxprintf (NULL
, "%s", buf
);
864 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
865 _IO_funlockfile (stderr
);
871 _("%s: option requires an argument -- '%c'\n"),
876 if (optstring
[0] == ':')
883 /* We already incremented `d->optind' once;
884 increment it again when taking next ARGV-elt as argument. */
885 d
->optarg
= argv
[d
->optind
++];
887 /* optarg is now the argument, see if it's in the
888 table of longopts. */
890 for (d
->__nextchar
= nameend
= d
->optarg
; *nameend
&& *nameend
!= '=';
894 /* Test all long options for either exact match
895 or abbreviated matches. */
896 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
897 if (!strncmp (p
->name
, d
->__nextchar
, nameend
- d
->__nextchar
))
899 if ((unsigned int) (nameend
- d
->__nextchar
) == strlen (p
->name
))
901 /* Exact match found. */
903 indfound
= option_index
;
907 else if (pfound
== NULL
)
909 /* First nonexact match found. */
911 indfound
= option_index
;
914 /* Second or later nonexact match found. */
921 #if defined _LIBC && defined USE_IN_LIBIO
924 if (__asprintf (&buf
, _("%s: option '-W %s' is ambiguous\n"),
925 argv
[0], argv
[d
->optind
]) >= 0)
927 _IO_flockfile (stderr
);
929 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
930 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
932 __fxprintf (NULL
, "%s", buf
);
934 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
935 _IO_funlockfile (stderr
);
940 fprintf (stderr
, _("%s: option '-W %s' is ambiguous\n"),
941 argv
[0], argv
[d
->optind
]);
944 d
->__nextchar
+= strlen (d
->__nextchar
);
950 option_index
= indfound
;
953 /* Don't test has_arg with >, because some C compilers don't
954 allow it to be used on enums. */
956 d
->optarg
= nameend
+ 1;
961 #if defined _LIBC && defined USE_IN_LIBIO
964 if (__asprintf (&buf
, _("\
965 %s: option '-W %s' doesn't allow an argument\n"),
966 argv
[0], pfound
->name
) >= 0)
968 _IO_flockfile (stderr
);
970 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
971 ((_IO_FILE
*) stderr
)->_flags2
972 |= _IO_FLAGS2_NOTCANCEL
;
974 __fxprintf (NULL
, "%s", buf
);
976 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
977 _IO_funlockfile (stderr
);
982 fprintf (stderr
, _("\
983 %s: option '-W %s' doesn't allow an argument\n"),
984 argv
[0], pfound
->name
);
988 d
->__nextchar
+= strlen (d
->__nextchar
);
992 else if (pfound
->has_arg
== 1)
994 if (d
->optind
< argc
)
995 d
->optarg
= argv
[d
->optind
++];
1000 #if defined _LIBC && defined USE_IN_LIBIO
1003 if (__asprintf (&buf
, _("\
1004 %s: option '%s' requires an argument\n"),
1005 argv
[0], argv
[d
->optind
- 1]) >= 0)
1007 _IO_flockfile (stderr
);
1009 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
1010 ((_IO_FILE
*) stderr
)->_flags2
1011 |= _IO_FLAGS2_NOTCANCEL
;
1013 __fxprintf (NULL
, "%s", buf
);
1015 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
1016 _IO_funlockfile (stderr
);
1022 _("%s: option '%s' requires an argument\n"),
1023 argv
[0], argv
[d
->optind
- 1]);
1026 d
->__nextchar
+= strlen (d
->__nextchar
);
1027 return optstring
[0] == ':' ? ':' : '?';
1030 d
->__nextchar
+= strlen (d
->__nextchar
);
1031 if (longind
!= NULL
)
1032 *longind
= option_index
;
1035 *(pfound
->flag
) = pfound
->val
;
1040 d
->__nextchar
= NULL
;
1041 return 'W'; /* Let the application handle it. */
1047 /* This is an option that accepts an argument optionally. */
1048 if (*d
->__nextchar
!= '\0')
1050 d
->optarg
= d
->__nextchar
;
1055 d
->__nextchar
= NULL
;
1059 /* This is an option that requires an argument. */
1060 if (*d
->__nextchar
!= '\0')
1062 d
->optarg
= d
->__nextchar
;
1063 /* If we end this ARGV-element by taking the rest as an arg,
1064 we must advance to the next element now. */
1067 else if (d
->optind
== argc
)
1071 #if defined _LIBC && defined USE_IN_LIBIO
1074 if (__asprintf (&buf
, _("\
1075 %s: option requires an argument -- '%c'\n"),
1078 _IO_flockfile (stderr
);
1080 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
1081 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
1083 __fxprintf (NULL
, "%s", buf
);
1085 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
1086 _IO_funlockfile (stderr
);
1092 _("%s: option requires an argument -- '%c'\n"),
1097 if (optstring
[0] == ':')
1103 /* We already incremented `optind' once;
1104 increment it again when taking next ARGV-elt as argument. */
1105 d
->optarg
= argv
[d
->optind
++];
1106 d
->__nextchar
= NULL
;
1114 _getopt_internal (int argc
, char *const *argv
, const char *optstring
,
1115 const struct option
*longopts
, int *longind
, int long_only
,
1116 int posixly_correct
)
1120 getopt_data
.optind
= optind
;
1121 getopt_data
.opterr
= opterr
;
1123 result
= _getopt_internal_r (argc
, argv
, optstring
, longopts
,
1124 longind
, long_only
, &getopt_data
,
1127 optind
= getopt_data
.optind
;
1128 optarg
= getopt_data
.optarg
;
1129 optopt
= getopt_data
.optopt
;
1135 getopt (int argc
, char *const *argv
, const char *optstring
)
1137 return _getopt_internal (argc
, argv
, optstring
,
1138 (const struct option
*) 0,
1145 __posix_getopt (int argc
, char *const *argv
, const char *optstring
)
1147 return _getopt_internal (argc
, argv
, optstring
,
1148 (const struct option
*) 0,
1154 #endif /* Not ELIDE_CODE. */
1158 /* Compile with -DTEST to make an executable for use in testing
1159 the above definition of `getopt'. */
1162 main (int argc
, char **argv
)
1165 int digit_optind
= 0;
1169 int this_option_optind
= optind
? optind
: 1;
1171 c
= getopt (argc
, argv
, "abc:d:0123456789");
1187 if (digit_optind
!= 0 && digit_optind
!= this_option_optind
)
1188 printf ("digits occur in two different argv-elements.\n");
1189 digit_optind
= this_option_optind
;
1190 printf ("option %c\n", c
);
1194 printf ("option a\n");
1198 printf ("option b\n");
1202 printf ("option c with value '%s'\n", optarg
);
1209 printf ("?? getopt returned character code 0%o ??\n", c
);
1215 printf ("non-option ARGV-elements: ");
1216 while (optind
< argc
)
1217 printf ("%s ", argv
[optind
++]);