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 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation,
21 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
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 /* This needs to come after some library #include
36 to get __GNU_LIBRARY__ defined. */
37 #ifdef __GNU_LIBRARY__
38 /* Don't include stdlib.h for non-GNU C libraries because some of them
39 contain conflicting prototypes for getopt. */
42 #endif /* GNU C library. */
54 # define _(msgid) gettext (msgid)
57 #if defined _LIBC && defined USE_IN_LIBIO
61 #ifndef attribute_hidden
62 # define attribute_hidden
65 /* Unlike standard Unix `getopt', functions like `getopt_long'
66 let the user intersperse the options with the other arguments.
68 As `getopt_long' works, it permutes the elements of ARGV so that,
69 when it is done, all the options precede everything else. Thus
70 all application programs are extended to handle flexible argument order.
72 Using `getopt' or setting the environment variable POSIXLY_CORRECT
74 Then the application's behavior is completely standard.
76 GNU application programs can use a third alternative mode in which
77 they can distinguish the relative order of options and other arguments. */
80 #include "getopt_int.h"
82 /* For communication from `getopt' to the caller.
83 When `getopt' finds an option that takes an argument,
84 the argument value is returned here.
85 Also, when `ordering' is RETURN_IN_ORDER,
86 each non-option ARGV-element is returned here. */
90 /* Index in ARGV of the next element to be scanned.
91 This is used for communication to and from the caller
92 and for communication between successive calls to `getopt'.
94 On entry to `getopt', zero means this is the first call; initialize.
96 When `getopt' returns -1, this is the index of the first of the
97 non-option elements that the caller should itself scan.
99 Otherwise, `optind' communicates from one call to the next
100 how much of ARGV has been scanned so far. */
102 /* 1003.2 says this must be 1 before any call. */
105 /* Callers store zero here to inhibit the error message
106 for unrecognized options. */
110 /* Set to an option character which was unrecognized.
111 This must be initialized on some systems to avoid linking in the
112 system's own getopt implementation. */
116 /* Keep a global copy of all internal members of getopt_data. */
118 static struct _getopt_data getopt_data
;
120 #ifndef __GNU_LIBRARY__
122 /* Avoid depending on library functions or files
123 whose names are inconsistent. */
126 extern char *getenv ();
129 #endif /* not __GNU_LIBRARY__ */
132 /* Stored original parameters.
133 XXX This is no good solution. We should rather copy the args so
134 that we can compare them later. But we must not use malloc(3). */
135 extern int __libc_argc
;
136 extern char **__libc_argv
;
138 /* Bash 2.0 gives us an environment variable containing flags
139 indicating ARGV elements that should not be considered arguments. */
141 # ifdef USE_NONOPTION_FLAGS
142 /* Defined in getopt_init.c */
143 extern char *__getopt_nonoption_flags
;
146 # ifdef USE_NONOPTION_FLAGS
147 # define SWAP_FLAGS(ch1, ch2) \
148 if (d->__nonoption_flags_len > 0) \
150 char __tmp = __getopt_nonoption_flags[ch1]; \
151 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
152 __getopt_nonoption_flags[ch2] = __tmp; \
155 # define SWAP_FLAGS(ch1, ch2)
158 # define SWAP_FLAGS(ch1, ch2)
161 /* Exchange two adjacent subsequences of ARGV.
162 One subsequence is elements [first_nonopt,last_nonopt)
163 which contains all the non-options that have been skipped so far.
164 The other is elements [last_nonopt,optind), which contains all
165 the options processed since those non-options were skipped.
167 `first_nonopt' and `last_nonopt' are relocated so that they describe
168 the new indices of the non-options in ARGV after they are moved. */
171 exchange (char **argv
, struct _getopt_data
*d
)
173 int bottom
= d
->__first_nonopt
;
174 int middle
= d
->__last_nonopt
;
178 /* Exchange the shorter segment with the far end of the longer segment.
179 That puts the shorter segment into the right place.
180 It leaves the longer segment in the right place overall,
181 but it consists of two parts that need to be swapped next. */
183 #if defined _LIBC && defined USE_NONOPTION_FLAGS
184 /* First make sure the handling of the `__getopt_nonoption_flags'
185 string can work normally. Our top argument must be in the range
187 if (d
->__nonoption_flags_len
> 0 && top
>= d
->__nonoption_flags_max_len
)
189 /* We must extend the array. The user plays games with us and
190 presents new arguments. */
191 char *new_str
= malloc (top
+ 1);
193 d
->__nonoption_flags_len
= d
->__nonoption_flags_max_len
= 0;
196 memset (__mempcpy (new_str
, __getopt_nonoption_flags
,
197 d
->__nonoption_flags_max_len
),
198 '\0', top
+ 1 - d
->__nonoption_flags_max_len
);
199 d
->__nonoption_flags_max_len
= top
+ 1;
200 __getopt_nonoption_flags
= new_str
;
205 while (top
> middle
&& middle
> bottom
)
207 if (top
- middle
> middle
- bottom
)
209 /* Bottom segment is the short one. */
210 int len
= middle
- bottom
;
213 /* Swap it with the top part of the top segment. */
214 for (i
= 0; i
< len
; i
++)
216 tem
= argv
[bottom
+ i
];
217 argv
[bottom
+ i
] = argv
[top
- (middle
- bottom
) + i
];
218 argv
[top
- (middle
- bottom
) + i
] = tem
;
219 SWAP_FLAGS (bottom
+ i
, top
- (middle
- bottom
) + i
);
221 /* Exclude the moved bottom segment from further swapping. */
226 /* Top segment is the short one. */
227 int len
= top
- middle
;
230 /* Swap it with the bottom part of the bottom segment. */
231 for (i
= 0; i
< len
; i
++)
233 tem
= argv
[bottom
+ i
];
234 argv
[bottom
+ i
] = argv
[middle
+ i
];
235 argv
[middle
+ i
] = tem
;
236 SWAP_FLAGS (bottom
+ i
, middle
+ i
);
238 /* Exclude the moved top segment from further swapping. */
243 /* Update records for the slots the non-options now occupy. */
245 d
->__first_nonopt
+= (d
->optind
- d
->__last_nonopt
);
246 d
->__last_nonopt
= d
->optind
;
249 /* Initialize the internal data when the first call is made. */
252 _getopt_initialize (int argc
, char **argv
, const char *optstring
,
253 int posixly_correct
, struct _getopt_data
*d
)
255 /* Start processing options with ARGV-element 1 (since ARGV-element 0
256 is the program name); the sequence of previously skipped
257 non-option ARGV-elements is empty. */
259 d
->__first_nonopt
= d
->__last_nonopt
= d
->optind
;
261 d
->__nextchar
= NULL
;
263 d
->__posixly_correct
= posixly_correct
|| !!getenv ("POSIXLY_CORRECT");
265 /* Determine how to handle the ordering of options and nonoptions. */
267 if (optstring
[0] == '-')
269 d
->__ordering
= RETURN_IN_ORDER
;
272 else if (optstring
[0] == '+')
274 d
->__ordering
= REQUIRE_ORDER
;
277 else if (d
->__posixly_correct
)
278 d
->__ordering
= REQUIRE_ORDER
;
280 d
->__ordering
= PERMUTE
;
282 #if defined _LIBC && defined USE_NONOPTION_FLAGS
283 if (!d
->__posixly_correct
284 && argc
== __libc_argc
&& argv
== __libc_argv
)
286 if (d
->__nonoption_flags_max_len
== 0)
288 if (__getopt_nonoption_flags
== NULL
289 || __getopt_nonoption_flags
[0] == '\0')
290 d
->__nonoption_flags_max_len
= -1;
293 const char *orig_str
= __getopt_nonoption_flags
;
294 int len
= d
->__nonoption_flags_max_len
= strlen (orig_str
);
295 if (d
->__nonoption_flags_max_len
< argc
)
296 d
->__nonoption_flags_max_len
= argc
;
297 __getopt_nonoption_flags
=
298 (char *) malloc (d
->__nonoption_flags_max_len
);
299 if (__getopt_nonoption_flags
== NULL
)
300 d
->__nonoption_flags_max_len
= -1;
302 memset (__mempcpy (__getopt_nonoption_flags
, orig_str
, len
),
303 '\0', d
->__nonoption_flags_max_len
- len
);
306 d
->__nonoption_flags_len
= d
->__nonoption_flags_max_len
;
309 d
->__nonoption_flags_len
= 0;
315 /* Scan elements of ARGV (whose length is ARGC) for option characters
318 If an element of ARGV starts with '-', and is not exactly "-" or "--",
319 then it is an option element. The characters of this element
320 (aside from the initial '-') are option characters. If `getopt'
321 is called repeatedly, it returns successively each of the option characters
322 from each of the option elements.
324 If `getopt' finds another option character, it returns that character,
325 updating `optind' and `nextchar' so that the next call to `getopt' can
326 resume the scan with the following option character or ARGV-element.
328 If there are no more option characters, `getopt' returns -1.
329 Then `optind' is the index in ARGV of the first ARGV-element
330 that is not an option. (The ARGV-elements have been permuted
331 so that those that are not options now come last.)
333 OPTSTRING is a string containing the legitimate option characters.
334 If an option character is seen that is not listed in OPTSTRING,
335 return '?' after printing an error message. If you set `opterr' to
336 zero, the error message is suppressed but we still return '?'.
338 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
339 so the following text in the same ARGV-element, or the text of the following
340 ARGV-element, is returned in `optarg'. Two colons mean an option that
341 wants an optional arg; if there is text in the current ARGV-element,
342 it is returned in `optarg', otherwise `optarg' is set to zero.
344 If OPTSTRING starts with `-' or `+', it requests different methods of
345 handling the non-option ARGV-elements.
346 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
348 Long-named options begin with `--' instead of `-'.
349 Their names may be abbreviated as long as the abbreviation is unique
350 or is an exact match for some defined option. If they have an
351 argument, it follows the option name in the same ARGV-element, separated
352 from the option name by a `=', or else the in next ARGV-element.
353 When `getopt' finds a long-named option, it returns 0 if that option's
354 `flag' field is nonzero, the value of the option's `val' field
355 if the `flag' field is zero.
357 LONGOPTS is a vector of `struct option' terminated by an
358 element containing a name which is zero.
360 LONGIND returns the index in LONGOPT of the long-named option found.
361 It is only valid when a long-named option has been found by the most
364 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
367 If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
368 environment variable were set. */
371 _getopt_internal_r (int argc
, char **argv
, const char *optstring
,
372 const struct option
*longopts
, int *longind
,
373 int long_only
, int posixly_correct
, struct _getopt_data
*d
)
375 int print_errors
= d
->opterr
;
376 if (optstring
[0] == ':')
384 if (d
->optind
== 0 || !d
->__initialized
)
387 d
->optind
= 1; /* Don't scan ARGV[0], the program name. */
388 optstring
= _getopt_initialize (argc
, argv
, optstring
,
390 d
->__initialized
= 1;
393 /* Test whether ARGV[optind] points to a non-option argument.
394 Either it does not have option syntax, or there is an environment flag
395 from the shell indicating it is not an option. The later information
396 is only used when the used in the GNU libc. */
397 #if defined _LIBC && defined USE_NONOPTION_FLAGS
398 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
399 || (d->optind < d->__nonoption_flags_len \
400 && __getopt_nonoption_flags[d->optind] == '1'))
402 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
405 if (d
->__nextchar
== NULL
|| *d
->__nextchar
== '\0')
407 /* Advance to the next ARGV-element. */
409 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
410 moved back by the user (who may also have changed the arguments). */
411 if (d
->__last_nonopt
> d
->optind
)
412 d
->__last_nonopt
= d
->optind
;
413 if (d
->__first_nonopt
> d
->optind
)
414 d
->__first_nonopt
= d
->optind
;
416 if (d
->__ordering
== PERMUTE
)
418 /* If we have just processed some options following some non-options,
419 exchange them so that the options come first. */
421 if (d
->__first_nonopt
!= d
->__last_nonopt
422 && d
->__last_nonopt
!= d
->optind
)
423 exchange ((char **) argv
, d
);
424 else if (d
->__last_nonopt
!= d
->optind
)
425 d
->__first_nonopt
= d
->optind
;
427 /* Skip any additional non-options
428 and extend the range of non-options previously skipped. */
430 while (d
->optind
< argc
&& NONOPTION_P
)
432 d
->__last_nonopt
= d
->optind
;
435 /* The special ARGV-element `--' means premature end of options.
436 Skip it like a null option,
437 then exchange with previous non-options as if it were an option,
438 then skip everything else like a non-option. */
440 if (d
->optind
!= argc
&& !strcmp (argv
[d
->optind
], "--"))
444 if (d
->__first_nonopt
!= d
->__last_nonopt
445 && d
->__last_nonopt
!= d
->optind
)
446 exchange ((char **) argv
, d
);
447 else if (d
->__first_nonopt
== d
->__last_nonopt
)
448 d
->__first_nonopt
= d
->optind
;
449 d
->__last_nonopt
= argc
;
454 /* If we have done all the ARGV-elements, stop the scan
455 and back over any non-options that we skipped and permuted. */
457 if (d
->optind
== argc
)
459 /* Set the next-arg-index to point at the non-options
460 that we previously skipped, so the caller will digest them. */
461 if (d
->__first_nonopt
!= d
->__last_nonopt
)
462 d
->optind
= d
->__first_nonopt
;
466 /* If we have come to a non-option and did not permute it,
467 either stop the scan or describe it to the caller and pass it by. */
471 if (d
->__ordering
== REQUIRE_ORDER
)
473 d
->optarg
= argv
[d
->optind
++];
477 /* We have found another option-ARGV-element.
478 Skip the initial punctuation. */
480 d
->__nextchar
= (argv
[d
->optind
] + 1
481 + (longopts
!= NULL
&& argv
[d
->optind
][1] == '-'));
484 /* Decode the current option-ARGV-element. */
486 /* Check whether the ARGV-element is a long option.
488 If long_only and the ARGV-element has the form "-f", where f is
489 a valid short option, don't consider it an abbreviated form of
490 a long option that starts with f. Otherwise there would be no
491 way to give the -f short option.
493 On the other hand, if there's a long option "fubar" and
494 the ARGV-element is "-fu", do consider that an abbreviation of
495 the long option, just like "--fu", and not "-f" with arg "u".
497 This distinction seems to be the most useful approach. */
500 && (argv
[d
->optind
][1] == '-'
501 || (long_only
&& (argv
[d
->optind
][2]
502 || !strchr (optstring
, argv
[d
->optind
][1])))))
505 const struct option
*p
;
506 const struct option
*pfound
= NULL
;
512 for (nameend
= d
->__nextchar
; *nameend
&& *nameend
!= '='; nameend
++)
515 /* Test all long options for either exact match
516 or abbreviated matches. */
517 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
518 if (!strncmp (p
->name
, d
->__nextchar
, nameend
- d
->__nextchar
))
520 if ((unsigned int) (nameend
- d
->__nextchar
)
521 == (unsigned int) strlen (p
->name
))
523 /* Exact match found. */
525 indfound
= option_index
;
529 else if (pfound
== NULL
)
531 /* First nonexact match found. */
533 indfound
= option_index
;
536 || pfound
->has_arg
!= p
->has_arg
537 || pfound
->flag
!= p
->flag
538 || pfound
->val
!= p
->val
)
539 /* Second or later nonexact match found. */
547 #if defined _LIBC && defined USE_IN_LIBIO
550 if (__asprintf (&buf
, _("%s: option `%s' is ambiguous\n"),
551 argv
[0], argv
[d
->optind
]) >= 0)
553 _IO_flockfile (stderr
);
555 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
556 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
558 if (_IO_fwide (stderr
, 0) > 0)
559 __fwprintf (stderr
, L
"%s", buf
);
563 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
564 _IO_funlockfile (stderr
);
569 fprintf (stderr
, _("%s: option `%s' is ambiguous\n"),
570 argv
[0], argv
[d
->optind
]);
573 d
->__nextchar
+= strlen (d
->__nextchar
);
581 option_index
= indfound
;
585 /* Don't test has_arg with >, because some C compilers don't
586 allow it to be used on enums. */
588 d
->optarg
= nameend
+ 1;
593 #if defined _LIBC && defined USE_IN_LIBIO
598 if (argv
[d
->optind
- 1][1] == '-')
601 #if defined _LIBC && defined USE_IN_LIBIO
602 n
= __asprintf (&buf
, _("\
603 %s: option `--%s' doesn't allow an argument\n"),
604 argv
[0], pfound
->name
);
606 fprintf (stderr
, _("\
607 %s: option `--%s' doesn't allow an argument\n"),
608 argv
[0], pfound
->name
);
613 /* +option or -option */
614 #if defined _LIBC && defined USE_IN_LIBIO
615 n
= __asprintf (&buf
, _("\
616 %s: option `%c%s' doesn't allow an argument\n"),
617 argv
[0], argv
[d
->optind
- 1][0],
620 fprintf (stderr
, _("\
621 %s: option `%c%s' doesn't allow an argument\n"),
622 argv
[0], argv
[d
->optind
- 1][0],
627 #if defined _LIBC && defined USE_IN_LIBIO
630 _IO_flockfile (stderr
);
632 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
633 ((_IO_FILE
*) stderr
)->_flags2
634 |= _IO_FLAGS2_NOTCANCEL
;
636 if (_IO_fwide (stderr
, 0) > 0)
637 __fwprintf (stderr
, L
"%s", buf
);
641 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
642 _IO_funlockfile (stderr
);
649 d
->__nextchar
+= strlen (d
->__nextchar
);
651 d
->optopt
= pfound
->val
;
655 else if (pfound
->has_arg
== 1)
657 if (d
->optind
< argc
)
658 d
->optarg
= argv
[d
->optind
++];
663 #if defined _LIBC && defined USE_IN_LIBIO
666 if (__asprintf (&buf
, _("\
667 %s: option `%s' requires an argument\n"),
668 argv
[0], argv
[d
->optind
- 1]) >= 0)
670 _IO_flockfile (stderr
);
672 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
673 ((_IO_FILE
*) stderr
)->_flags2
674 |= _IO_FLAGS2_NOTCANCEL
;
676 if (_IO_fwide (stderr
, 0) > 0)
677 __fwprintf (stderr
, L
"%s", buf
);
681 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
682 _IO_funlockfile (stderr
);
688 _("%s: option `%s' requires an argument\n"),
689 argv
[0], argv
[d
->optind
- 1]);
692 d
->__nextchar
+= strlen (d
->__nextchar
);
693 d
->optopt
= pfound
->val
;
694 return optstring
[0] == ':' ? ':' : '?';
697 d
->__nextchar
+= strlen (d
->__nextchar
);
699 *longind
= option_index
;
702 *(pfound
->flag
) = pfound
->val
;
708 /* Can't find it as a long option. If this is not getopt_long_only,
709 or the option starts with '--' or is not a valid short
710 option, then it's an error.
711 Otherwise interpret it as a short option. */
712 if (!long_only
|| argv
[d
->optind
][1] == '-'
713 || strchr (optstring
, *d
->__nextchar
) == NULL
)
717 #if defined _LIBC && defined USE_IN_LIBIO
722 if (argv
[d
->optind
][1] == '-')
725 #if defined _LIBC && defined USE_IN_LIBIO
726 n
= __asprintf (&buf
, _("%s: unrecognized option `--%s'\n"),
727 argv
[0], d
->__nextchar
);
729 fprintf (stderr
, _("%s: unrecognized option `--%s'\n"),
730 argv
[0], d
->__nextchar
);
735 /* +option or -option */
736 #if defined _LIBC && defined USE_IN_LIBIO
737 n
= __asprintf (&buf
, _("%s: unrecognized option `%c%s'\n"),
738 argv
[0], argv
[d
->optind
][0], d
->__nextchar
);
740 fprintf (stderr
, _("%s: unrecognized option `%c%s'\n"),
741 argv
[0], argv
[d
->optind
][0], d
->__nextchar
);
745 #if defined _LIBC && defined USE_IN_LIBIO
748 _IO_flockfile (stderr
);
750 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
751 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
753 if (_IO_fwide (stderr
, 0) > 0)
754 __fwprintf (stderr
, L
"%s", buf
);
758 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
759 _IO_funlockfile (stderr
);
765 d
->__nextchar
= (char *) "";
772 /* Look at and handle the next short option-character. */
775 char c
= *d
->__nextchar
++;
776 char *temp
= strchr (optstring
, c
);
778 /* Increment `optind' when we start to process its last character. */
779 if (*d
->__nextchar
== '\0')
782 if (temp
== NULL
|| c
== ':')
786 #if defined _LIBC && defined USE_IN_LIBIO
791 if (d
->__posixly_correct
)
793 /* 1003.2 specifies the format of this message. */
794 #if defined _LIBC && defined USE_IN_LIBIO
795 n
= __asprintf (&buf
, _("%s: illegal option -- %c\n"),
798 fprintf (stderr
, _("%s: illegal option -- %c\n"), argv
[0], c
);
803 #if defined _LIBC && defined USE_IN_LIBIO
804 n
= __asprintf (&buf
, _("%s: invalid option -- %c\n"),
807 fprintf (stderr
, _("%s: invalid option -- %c\n"), argv
[0], c
);
811 #if defined _LIBC && defined USE_IN_LIBIO
814 _IO_flockfile (stderr
);
816 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
817 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
819 if (_IO_fwide (stderr
, 0) > 0)
820 __fwprintf (stderr
, L
"%s", buf
);
824 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
825 _IO_funlockfile (stderr
);
834 /* Convenience. Treat POSIX -W foo same as long option --foo */
835 if (temp
[0] == 'W' && temp
[1] == ';')
838 const struct option
*p
;
839 const struct option
*pfound
= NULL
;
845 /* This is an option that requires an argument. */
846 if (*d
->__nextchar
!= '\0')
848 d
->optarg
= d
->__nextchar
;
849 /* If we end this ARGV-element by taking the rest as an arg,
850 we must advance to the next element now. */
853 else if (d
->optind
== argc
)
857 /* 1003.2 specifies the format of this message. */
858 #if defined _LIBC && defined USE_IN_LIBIO
861 if (__asprintf (&buf
,
862 _("%s: option requires an argument -- %c\n"),
865 _IO_flockfile (stderr
);
867 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
868 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
870 if (_IO_fwide (stderr
, 0) > 0)
871 __fwprintf (stderr
, L
"%s", buf
);
875 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
876 _IO_funlockfile (stderr
);
881 fprintf (stderr
, _("%s: option requires an argument -- %c\n"),
886 if (optstring
[0] == ':')
893 /* We already incremented `d->optind' once;
894 increment it again when taking next ARGV-elt as argument. */
895 d
->optarg
= argv
[d
->optind
++];
897 /* optarg is now the argument, see if it's in the
898 table of longopts. */
900 for (d
->__nextchar
= nameend
= d
->optarg
; *nameend
&& *nameend
!= '=';
904 /* Test all long options for either exact match
905 or abbreviated matches. */
906 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
907 if (!strncmp (p
->name
, d
->__nextchar
, nameend
- d
->__nextchar
))
909 if ((unsigned int) (nameend
- d
->__nextchar
) == strlen (p
->name
))
911 /* Exact match found. */
913 indfound
= option_index
;
917 else if (pfound
== NULL
)
919 /* First nonexact match found. */
921 indfound
= option_index
;
924 /* Second or later nonexact match found. */
931 #if defined _LIBC && defined USE_IN_LIBIO
934 if (__asprintf (&buf
, _("%s: option `-W %s' is ambiguous\n"),
935 argv
[0], argv
[d
->optind
]) >= 0)
937 _IO_flockfile (stderr
);
939 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
940 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
942 if (_IO_fwide (stderr
, 0) > 0)
943 __fwprintf (stderr
, L
"%s", buf
);
947 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
948 _IO_funlockfile (stderr
);
953 fprintf (stderr
, _("%s: option `-W %s' is ambiguous\n"),
954 argv
[0], argv
[d
->optind
]);
957 d
->__nextchar
+= strlen (d
->__nextchar
);
963 option_index
= indfound
;
966 /* Don't test has_arg with >, because some C compilers don't
967 allow it to be used on enums. */
969 d
->optarg
= nameend
+ 1;
974 #if defined _LIBC && defined USE_IN_LIBIO
977 if (__asprintf (&buf
, _("\
978 %s: option `-W %s' doesn't allow an argument\n"),
979 argv
[0], pfound
->name
) >= 0)
981 _IO_flockfile (stderr
);
983 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
984 ((_IO_FILE
*) stderr
)->_flags2
985 |= _IO_FLAGS2_NOTCANCEL
;
987 if (_IO_fwide (stderr
, 0) > 0)
988 __fwprintf (stderr
, L
"%s", buf
);
992 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
993 _IO_funlockfile (stderr
);
998 fprintf (stderr
, _("\
999 %s: option `-W %s' doesn't allow an argument\n"),
1000 argv
[0], pfound
->name
);
1004 d
->__nextchar
+= strlen (d
->__nextchar
);
1008 else if (pfound
->has_arg
== 1)
1010 if (d
->optind
< argc
)
1011 d
->optarg
= argv
[d
->optind
++];
1016 #if defined _LIBC && defined USE_IN_LIBIO
1019 if (__asprintf (&buf
, _("\
1020 %s: option `%s' requires an argument\n"),
1021 argv
[0], argv
[d
->optind
- 1]) >= 0)
1023 _IO_flockfile (stderr
);
1025 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
1026 ((_IO_FILE
*) stderr
)->_flags2
1027 |= _IO_FLAGS2_NOTCANCEL
;
1029 if (_IO_fwide (stderr
, 0) > 0)
1030 __fwprintf (stderr
, L
"%s", buf
);
1032 fputs (buf
, stderr
);
1034 ((_IO_FILE
*) stderr
)->_flags2
= old_flags2
;
1035 _IO_funlockfile (stderr
);
1041 _("%s: option `%s' requires an argument\n"),
1042 argv
[0], argv
[d
->optind
- 1]);
1045 d
->__nextchar
+= strlen (d
->__nextchar
);
1046 return optstring
[0] == ':' ? ':' : '?';
1049 d
->__nextchar
+= strlen (d
->__nextchar
);
1050 if (longind
!= NULL
)
1051 *longind
= option_index
;
1054 *(pfound
->flag
) = pfound
->val
;
1059 d
->__nextchar
= NULL
;
1060 return 'W'; /* Let the application handle it. */
1066 /* This is an option that accepts an argument optionally. */
1067 if (*d
->__nextchar
!= '\0')
1069 d
->optarg
= d
->__nextchar
;
1074 d
->__nextchar
= NULL
;
1078 /* This is an option that requires an argument. */
1079 if (*d
->__nextchar
!= '\0')
1081 d
->optarg
= d
->__nextchar
;
1082 /* If we end this ARGV-element by taking the rest as an arg,
1083 we must advance to the next element now. */
1086 else if (d
->optind
== argc
)
1090 /* 1003.2 specifies the format of this message. */
1091 #if defined _LIBC && defined USE_IN_LIBIO
1094 if (__asprintf (&buf
, _("\
1095 %s: option requires an argument -- %c\n"),
1098 _IO_flockfile (stderr
);
1100 int old_flags2
= ((_IO_FILE
*) stderr
)->_flags2
;
1101 ((_IO_FILE
*) stderr
)->_flags2
|= _IO_FLAGS2_NOTCANCEL
;
1103 if (_IO_fwide (stderr
, 0) > 0)
1104 __fwprintf (stderr
, L
"%s", buf
);
1106 fputs (buf
, stderr
);
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 **argv
, const char *optstring
,
1138 const struct option
*longopts
, int *longind
,
1139 int long_only
, int posixly_correct
)
1143 getopt_data
.optind
= optind
;
1144 getopt_data
.opterr
= opterr
;
1146 result
= _getopt_internal_r (argc
, argv
, optstring
, longopts
, longind
,
1147 long_only
, posixly_correct
, &getopt_data
);
1149 optind
= getopt_data
.optind
;
1150 optarg
= getopt_data
.optarg
;
1151 optopt
= getopt_data
.optopt
;
1156 /* glibc gets a LSB-compliant getopt.
1157 Standalone applications get a POSIX-compliant getopt. */
1159 enum { POSIXLY_CORRECT
= 0 };
1161 enum { POSIXLY_CORRECT
= 1 };
1165 getopt (int argc
, char *const *argv
, const char *optstring
)
1167 return _getopt_internal (argc
, (char **) argv
, optstring
, NULL
, NULL
, 0,
1173 /* Compile with -DTEST to make an executable for use in testing
1174 the above definition of `getopt'. */
1177 main (int argc
, char **argv
)
1180 int digit_optind
= 0;
1184 int this_option_optind
= optind
? optind
: 1;
1186 c
= getopt (argc
, argv
, "abc:d:0123456789");
1202 if (digit_optind
!= 0 && digit_optind
!= this_option_optind
)
1203 printf ("digits occur in two different argv-elements.\n");
1204 digit_optind
= this_option_optind
;
1205 printf ("option %c\n", c
);
1209 printf ("option a\n");
1213 printf ("option b\n");
1217 printf ("option c with value `%s'\n", optarg
);
1224 printf ("?? getopt returned character code 0%o ??\n", c
);
1230 printf ("non-option ARGV-elements: ");
1231 while (optind
< argc
)
1232 printf ("%s ", argv
[optind
++]);