2 Copyright (C) 1987-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library and is also part of gnulib.
4 Patches to this file should be submitted to both projects.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
31 /* When used as part of glibc, error printing must be done differently
32 for standards compliance. getopt is not a cancellation point, so
33 it must not call functions that are, and it is specified by an
34 older standard than stdio locking, so it must not refer to
35 functions in the "user namespace" related to stdio locking.
36 Finally, it must use glibc's internal message translation so that
37 the messages are looked up in the proper text domain. */
39 # define fprintf __fxprintf_nocancel
40 # define flockfile(fp) _IO_flockfile (fp)
41 # define funlockfile(fp) _IO_funlockfile (fp)
43 # define _(msgid) (msgid)
44 /* When used standalone, flockfile and funlockfile might not be
46 # ifndef _POSIX_THREAD_SAFE_FUNCTIONS
47 # define flockfile(fp) /* nop */
48 # define funlockfile(fp) /* nop */
50 /* When used standalone, do not attempt to use alloca. */
51 # define __libc_use_alloca(size) 0
53 # define alloca(size) (abort (), (void *)0)
56 /* This implementation of 'getopt' has three modes for handling
57 options interspersed with non-option arguments. It can stop
58 scanning for options at the first non-option argument encountered,
59 as POSIX specifies. It can continue scanning for options after the
60 first non-option argument, but permute 'argv' as it goes so that,
61 after 'getopt' is done, all the options precede all the non-option
62 arguments and 'optind' points to the first non-option argument.
63 Or, it can report non-option arguments as if they were arguments to
64 the option character '\x01'.
66 The default behavior of 'getopt_long' is to permute the argument list.
67 When this implementation is used standalone, the default behavior of
68 'getopt' is to stop at the first non-option argument, but when it is
69 used as part of GNU libc it also permutes the argument list. In both
70 cases, setting the environment variable POSIXLY_CORRECT to any value
73 If the first character of the OPTSTRING argument to 'getopt' or
74 'getopt_long' is '+', both functions will stop at the first
75 non-option argument. If it is '-', both functions will report
76 non-option arguments as arguments to the option character '\x01'. */
78 #include "getopt_int.h"
80 /* For communication from 'getopt' to the caller.
81 When 'getopt' finds an option that takes an argument,
82 the argument value is returned here.
83 Also, when 'ordering' is RETURN_IN_ORDER,
84 each non-option ARGV-element is returned here. */
88 /* Index in ARGV of the next element to be scanned.
89 This is used for communication to and from the caller
90 and for communication between successive calls to 'getopt'.
92 On entry to 'getopt', zero means this is the first call; initialize.
94 When 'getopt' returns -1, this is the index of the first of the
95 non-option elements that the caller should itself scan.
97 Otherwise, 'optind' communicates from one call to the next
98 how much of ARGV has been scanned so far. */
100 /* 1003.2 says this must be 1 before any call. */
103 /* Callers store zero here to inhibit the error message
104 for unrecognized options. */
108 /* Set to an option character which was unrecognized.
109 This must be initialized on some systems to avoid linking in the
110 system's own getopt implementation. */
114 /* Keep a global copy of all internal members of getopt_data. */
116 static struct _getopt_data getopt_data
;
118 /* Exchange two adjacent subsequences of ARGV.
119 One subsequence is elements [first_nonopt,last_nonopt)
120 which contains all the non-options that have been skipped so far.
121 The other is elements [last_nonopt,optind), which contains all
122 the options processed since those non-options were skipped.
124 'first_nonopt' and 'last_nonopt' are relocated so that they describe
125 the new indices of the non-options in ARGV after they are moved. */
128 exchange (char **argv
, struct _getopt_data
*d
)
130 int bottom
= d
->__first_nonopt
;
131 int middle
= d
->__last_nonopt
;
135 /* Exchange the shorter segment with the far end of the longer segment.
136 That puts the shorter segment into the right place.
137 It leaves the longer segment in the right place overall,
138 but it consists of two parts that need to be swapped next. */
140 while (top
> middle
&& middle
> bottom
)
142 if (top
- middle
> middle
- bottom
)
144 /* Bottom segment is the short one. */
145 int len
= middle
- bottom
;
148 /* Swap it with the top part of the top segment. */
149 for (i
= 0; i
< len
; i
++)
151 tem
= argv
[bottom
+ i
];
152 argv
[bottom
+ i
] = argv
[top
- (middle
- bottom
) + i
];
153 argv
[top
- (middle
- bottom
) + i
] = tem
;
155 /* Exclude the moved bottom segment from further swapping. */
160 /* Top segment is the short one. */
161 int len
= top
- middle
;
164 /* Swap it with the bottom part of the bottom segment. */
165 for (i
= 0; i
< len
; i
++)
167 tem
= argv
[bottom
+ i
];
168 argv
[bottom
+ i
] = argv
[middle
+ i
];
169 argv
[middle
+ i
] = tem
;
171 /* Exclude the moved top segment from further swapping. */
176 /* Update records for the slots the non-options now occupy. */
178 d
->__first_nonopt
+= (d
->optind
- d
->__last_nonopt
);
179 d
->__last_nonopt
= d
->optind
;
182 /* Process the argument starting with d->__nextchar as a long option.
183 d->optind should *not* have been advanced over this argument.
185 If the value returned is -1, it was not actually a long option, the
186 state is unchanged, and the argument should be processed as a set
187 of short options (this can only happen when long_only is true).
188 Otherwise, the option (and its argument, if any) have been consumed
189 and the return value is the value to return from _getopt_internal_r. */
191 process_long_option (int argc
, char **argv
, const char *optstring
,
192 const struct option
*longopts
, int *longind
,
193 int long_only
, struct _getopt_data
*d
,
194 int print_errors
, const char *prefix
)
198 const struct option
*p
;
199 const struct option
*pfound
= NULL
;
203 for (nameend
= d
->__nextchar
; *nameend
&& *nameend
!= '='; nameend
++)
205 namelen
= nameend
- d
->__nextchar
;
207 /* First look for an exact match, counting the options as a side
209 for (p
= longopts
, n_options
= 0; p
->name
; p
++, n_options
++)
210 if (!strncmp (p
->name
, d
->__nextchar
, namelen
)
211 && namelen
== strlen (p
->name
))
213 /* Exact match found. */
215 option_index
= n_options
;
221 /* Didn't find an exact match, so look for abbreviations. */
222 unsigned char *ambig_set
= NULL
;
223 int ambig_malloced
= 0;
224 int ambig_fallback
= 0;
227 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
228 if (!strncmp (p
->name
, d
->__nextchar
, namelen
))
232 /* First nonexact match found. */
234 indfound
= option_index
;
237 || pfound
->has_arg
!= p
->has_arg
238 || pfound
->flag
!= p
->flag
239 || pfound
->val
!= p
->val
)
241 /* Second or later nonexact match found. */
245 /* Don't waste effort tracking the ambig set if
246 we're not going to print it anyway. */
250 if (__libc_use_alloca (n_options
))
251 ambig_set
= alloca (n_options
);
252 else if ((ambig_set
= malloc (n_options
)) == NULL
)
253 /* Fall back to simpler error message. */
260 memset (ambig_set
, 0, n_options
);
261 ambig_set
[indfound
] = 1;
265 ambig_set
[option_index
] = 1;
270 if (ambig_set
|| ambig_fallback
)
275 fprintf (stderr
, _("%s: option '%s%s' is ambiguous\n"),
276 argv
[0], prefix
, d
->__nextchar
);
281 _("%s: option '%s%s' is ambiguous; possibilities:"),
282 argv
[0], prefix
, d
->__nextchar
);
284 for (option_index
= 0; option_index
< n_options
; option_index
++)
285 if (ambig_set
[option_index
])
286 fprintf (stderr
, " '%s%s'",
287 prefix
, longopts
[option_index
].name
);
289 /* This must use 'fprintf' even though it's only
290 printing a single character, so that it goes through
291 __fxprintf_nocancel when compiled as part of glibc. */
292 fprintf (stderr
, "\n");
293 funlockfile (stderr
);
298 d
->__nextchar
+= strlen (d
->__nextchar
);
304 option_index
= indfound
;
309 /* Can't find it as a long option. If this is not getopt_long_only,
310 or the option starts with '--' or is not a valid short option,
311 then it's an error. */
312 if (!long_only
|| argv
[d
->optind
][1] == '-'
313 || strchr (optstring
, *d
->__nextchar
) == NULL
)
316 fprintf (stderr
, _("%s: unrecognized option '%s%s'\n"),
317 argv
[0], prefix
, d
->__nextchar
);
319 d
->__nextchar
= NULL
;
325 /* Otherwise interpret it as a short option. */
329 /* We have found a matching long option. Consume it. */
331 d
->__nextchar
= NULL
;
334 /* Don't test has_arg with >, because some C compilers don't
335 allow it to be used on enums. */
337 d
->optarg
= nameend
+ 1;
342 _("%s: option '%s%s' doesn't allow an argument\n"),
343 argv
[0], prefix
, pfound
->name
);
345 d
->optopt
= pfound
->val
;
349 else if (pfound
->has_arg
== 1)
351 if (d
->optind
< argc
)
352 d
->optarg
= argv
[d
->optind
++];
357 _("%s: option '%s%s' requires an argument\n"),
358 argv
[0], prefix
, pfound
->name
);
360 d
->optopt
= pfound
->val
;
361 return optstring
[0] == ':' ? ':' : '?';
366 *longind
= option_index
;
369 *(pfound
->flag
) = pfound
->val
;
375 /* Initialize internal data upon the first call to getopt. */
378 _getopt_initialize (int argc
,
379 char **argv
, const char *optstring
,
380 struct _getopt_data
*d
, int posixly_correct
)
382 /* Start processing options with ARGV-element 1 (since ARGV-element 0
383 is the program name); the sequence of previously skipped
384 non-option ARGV-elements is empty. */
388 d
->__first_nonopt
= d
->__last_nonopt
= d
->optind
;
389 d
->__nextchar
= NULL
;
391 /* Determine how to handle the ordering of options and nonoptions. */
392 if (optstring
[0] == '-')
394 d
->__ordering
= RETURN_IN_ORDER
;
397 else if (optstring
[0] == '+')
399 d
->__ordering
= REQUIRE_ORDER
;
402 else if (posixly_correct
|| !!getenv ("POSIXLY_CORRECT"))
403 d
->__ordering
= REQUIRE_ORDER
;
405 d
->__ordering
= PERMUTE
;
407 d
->__initialized
= 1;
411 /* Scan elements of ARGV (whose length is ARGC) for option characters
414 If an element of ARGV starts with '-', and is not exactly "-" or "--",
415 then it is an option element. The characters of this element
416 (aside from the initial '-') are option characters. If 'getopt'
417 is called repeatedly, it returns successively each of the option characters
418 from each of the option elements.
420 If 'getopt' finds another option character, it returns that character,
421 updating 'optind' and 'nextchar' so that the next call to 'getopt' can
422 resume the scan with the following option character or ARGV-element.
424 If there are no more option characters, 'getopt' returns -1.
425 Then 'optind' is the index in ARGV of the first ARGV-element
426 that is not an option. (The ARGV-elements have been permuted
427 so that those that are not options now come last.)
429 OPTSTRING is a string containing the legitimate option characters.
430 If an option character is seen that is not listed in OPTSTRING,
431 return '?' after printing an error message. If you set 'opterr' to
432 zero, the error message is suppressed but we still return '?'.
434 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
435 so the following text in the same ARGV-element, or the text of the following
436 ARGV-element, is returned in 'optarg'. Two colons mean an option that
437 wants an optional arg; if there is text in the current ARGV-element,
438 it is returned in 'optarg', otherwise 'optarg' is set to zero.
440 If OPTSTRING starts with '-' or '+', it requests different methods of
441 handling the non-option ARGV-elements.
442 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
444 Long-named options begin with '--' instead of '-'.
445 Their names may be abbreviated as long as the abbreviation is unique
446 or is an exact match for some defined option. If they have an
447 argument, it follows the option name in the same ARGV-element, separated
448 from the option name by a '=', or else the in next ARGV-element.
449 When 'getopt' finds a long-named option, it returns 0 if that option's
450 'flag' field is nonzero, the value of the option's 'val' field
451 if the 'flag' field is zero.
453 The elements of ARGV aren't really const, because we permute them.
454 But we pretend they're const in the prototype to be compatible
457 LONGOPTS is a vector of 'struct option' terminated by an
458 element containing a name which is zero.
460 LONGIND returns the index in LONGOPT of the long-named option found.
461 It is only valid when a long-named option has been found by the most
464 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
465 long-named options. */
468 _getopt_internal_r (int argc
, char **argv
, const char *optstring
,
469 const struct option
*longopts
, int *longind
,
470 int long_only
, struct _getopt_data
*d
, int posixly_correct
)
472 int print_errors
= d
->opterr
;
479 if (d
->optind
== 0 || !d
->__initialized
)
480 optstring
= _getopt_initialize (argc
, argv
, optstring
, d
, posixly_correct
);
481 else if (optstring
[0] == '-' || optstring
[0] == '+')
484 if (optstring
[0] == ':')
487 /* Test whether ARGV[optind] points to a non-option argument. */
488 #define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
490 if (d
->__nextchar
== NULL
|| *d
->__nextchar
== '\0')
492 /* Advance to the next ARGV-element. */
494 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
495 moved back by the user (who may also have changed the arguments). */
496 if (d
->__last_nonopt
> d
->optind
)
497 d
->__last_nonopt
= d
->optind
;
498 if (d
->__first_nonopt
> d
->optind
)
499 d
->__first_nonopt
= d
->optind
;
501 if (d
->__ordering
== PERMUTE
)
503 /* If we have just processed some options following some non-options,
504 exchange them so that the options come first. */
506 if (d
->__first_nonopt
!= d
->__last_nonopt
507 && d
->__last_nonopt
!= d
->optind
)
509 else if (d
->__last_nonopt
!= d
->optind
)
510 d
->__first_nonopt
= d
->optind
;
512 /* Skip any additional non-options
513 and extend the range of non-options previously skipped. */
515 while (d
->optind
< argc
&& NONOPTION_P
)
517 d
->__last_nonopt
= d
->optind
;
520 /* The special ARGV-element '--' means premature end of options.
521 Skip it like a null option,
522 then exchange with previous non-options as if it were an option,
523 then skip everything else like a non-option. */
525 if (d
->optind
!= argc
&& !strcmp (argv
[d
->optind
], "--"))
529 if (d
->__first_nonopt
!= d
->__last_nonopt
530 && d
->__last_nonopt
!= d
->optind
)
532 else if (d
->__first_nonopt
== d
->__last_nonopt
)
533 d
->__first_nonopt
= d
->optind
;
534 d
->__last_nonopt
= argc
;
539 /* If we have done all the ARGV-elements, stop the scan
540 and back over any non-options that we skipped and permuted. */
542 if (d
->optind
== argc
)
544 /* Set the next-arg-index to point at the non-options
545 that we previously skipped, so the caller will digest them. */
546 if (d
->__first_nonopt
!= d
->__last_nonopt
)
547 d
->optind
= d
->__first_nonopt
;
551 /* If we have come to a non-option and did not permute it,
552 either stop the scan or describe it to the caller and pass it by. */
556 if (d
->__ordering
== REQUIRE_ORDER
)
558 d
->optarg
= argv
[d
->optind
++];
562 /* We have found another option-ARGV-element.
563 Check whether it might be a long option. */
566 if (argv
[d
->optind
][1] == '-')
568 /* "--foo" is always a long option. The special option
569 "--" was handled above. */
570 d
->__nextchar
= argv
[d
->optind
] + 2;
571 return process_long_option (argc
, argv
, optstring
, longopts
,
572 longind
, long_only
, d
,
576 /* If long_only and the ARGV-element has the form "-f",
577 where f is a valid short option, don't consider it an
578 abbreviated form of a long option that starts with f.
579 Otherwise there would be no way to give the -f short
582 On the other hand, if there's a long option "fubar" and
583 the ARGV-element is "-fu", do consider that an
584 abbreviation of the long option, just like "--fu", and
585 not "-f" with arg "u".
587 This distinction seems to be the most useful approach. */
588 if (long_only
&& (argv
[d
->optind
][2]
589 || !strchr (optstring
, argv
[d
->optind
][1])))
592 d
->__nextchar
= argv
[d
->optind
] + 1;
593 code
= process_long_option (argc
, argv
, optstring
, longopts
,
594 longind
, long_only
, d
,
601 /* It is not a long option. Skip the initial punctuation. */
602 d
->__nextchar
= argv
[d
->optind
] + 1;
605 /* Look at and handle the next short option-character. */
608 char c
= *d
->__nextchar
++;
609 const char *temp
= strchr (optstring
, c
);
611 /* Increment 'optind' when we start to process its last character. */
612 if (*d
->__nextchar
== '\0')
615 if (temp
== NULL
|| c
== ':' || c
== ';')
618 fprintf (stderr
, _("%s: invalid option -- '%c'\n"), argv
[0], c
);
623 /* Convenience. Treat POSIX -W foo same as long option --foo */
624 if (temp
[0] == 'W' && temp
[1] == ';' && longopts
!= NULL
)
626 /* This is an option that requires an argument. */
627 if (*d
->__nextchar
!= '\0')
628 d
->optarg
= d
->__nextchar
;
629 else if (d
->optind
== argc
)
633 _("%s: option requires an argument -- '%c'\n"),
637 if (optstring
[0] == ':')
644 d
->optarg
= argv
[d
->optind
];
646 d
->__nextchar
= d
->optarg
;
648 return process_long_option (argc
, argv
, optstring
, longopts
, longind
,
649 0 /* long_only */, d
, print_errors
, "-W ");
655 /* This is an option that accepts an argument optionally. */
656 if (*d
->__nextchar
!= '\0')
658 d
->optarg
= d
->__nextchar
;
663 d
->__nextchar
= NULL
;
667 /* This is an option that requires an argument. */
668 if (*d
->__nextchar
!= '\0')
670 d
->optarg
= d
->__nextchar
;
671 /* If we end this ARGV-element by taking the rest as an arg,
672 we must advance to the next element now. */
675 else if (d
->optind
== argc
)
679 _("%s: option requires an argument -- '%c'\n"),
683 if (optstring
[0] == ':')
689 /* We already incremented 'optind' once;
690 increment it again when taking next ARGV-elt as argument. */
691 d
->optarg
= argv
[d
->optind
++];
692 d
->__nextchar
= NULL
;
700 _getopt_internal (int argc
, char **argv
, const char *optstring
,
701 const struct option
*longopts
, int *longind
, int long_only
,
706 getopt_data
.optind
= optind
;
707 getopt_data
.opterr
= opterr
;
709 result
= _getopt_internal_r (argc
, argv
, optstring
, longopts
,
710 longind
, long_only
, &getopt_data
,
713 optind
= getopt_data
.optind
;
714 optarg
= getopt_data
.optarg
;
715 optopt
= getopt_data
.optopt
;
720 /* glibc gets a LSB-compliant getopt and a POSIX-complaint __posix_getopt.
721 Standalone applications just get a POSIX-compliant getopt.
722 POSIX and LSB both require these functions to take 'char *const *argv'
723 even though this is incorrect (because of the permutation). */
724 #define GETOPT_ENTRY(NAME, POSIXLY_CORRECT) \
726 NAME (int argc, char *const *argv, const char *optstring) \
728 return _getopt_internal (argc, (char **)argv, optstring, \
729 0, 0, 0, POSIXLY_CORRECT); \
733 GETOPT_ENTRY(getopt
, 0)
734 GETOPT_ENTRY(__posix_getopt
, 1)
736 GETOPT_ENTRY(getopt
, 1)
742 /* Compile with -DTEST to make an executable for use in testing
743 the above definition of 'getopt'. */
746 main (int argc
, char **argv
)
749 int digit_optind
= 0;
753 int this_option_optind
= optind
? optind
: 1;
755 c
= getopt (argc
, argv
, "abc:d:0123456789");
771 if (digit_optind
!= 0 && digit_optind
!= this_option_optind
)
772 printf ("digits occur in two different argv-elements.\n");
773 digit_optind
= this_option_optind
;
774 printf ("option %c\n", c
);
778 printf ("option a\n");
782 printf ("option b\n");
786 printf ("option c with value '%s'\n", optarg
);
793 printf ("?? getopt returned character code 0%o ??\n", c
);
799 printf ("non-option ARGV-elements: ");
800 while (optind
< argc
)
801 printf ("%s ", argv
[optind
++]);