Add [BZ #21828] to ChangeLog
[glibc.git] / posix / getopt.c
blob543c8e7284d7247db1dbd00e15cbf6406f9a6f8b
1 /* Getopt for GNU.
2 Copyright (C) 1987-2017 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/>. */
20 #ifndef _LIBC
21 # include <config.h>
22 #endif
24 #include "getopt.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #ifdef _LIBC
32 /* When used as part of glibc, error printing must be done differently
33 for standards compliance. getopt is not a cancellation point, so
34 it must not call functions that are, and it is specified by an
35 older standard than stdio locking, so it must not refer to
36 functions in the "user namespace" related to stdio locking.
37 Finally, it must use glibc's internal message translation so that
38 the messages are looked up in the proper text domain. */
39 # include <libintl.h>
40 # define fprintf __fxprintf_nocancel
41 # define flockfile(fp) _IO_flockfile (fp)
42 # define funlockfile(fp) _IO_funlockfile (fp)
43 #else
44 # include "gettext.h"
45 # define _(msgid) gettext (msgid)
46 /* When used standalone, flockfile and funlockfile might not be
47 available. */
48 # ifndef _POSIX_THREAD_SAFE_FUNCTIONS
49 # define flockfile(fp) /* nop */
50 # define funlockfile(fp) /* nop */
51 # endif
52 /* When used standalone, do not attempt to use alloca. */
53 # define __libc_use_alloca(size) 0
54 # undef alloca
55 # define alloca(size) (abort (), (void *)0)
56 #endif
58 /* This implementation of 'getopt' has three modes for handling
59 options interspersed with non-option arguments. It can stop
60 scanning for options at the first non-option argument encountered,
61 as POSIX specifies. It can continue scanning for options after the
62 first non-option argument, but permute 'argv' as it goes so that,
63 after 'getopt' is done, all the options precede all the non-option
64 arguments and 'optind' points to the first non-option argument.
65 Or, it can report non-option arguments as if they were arguments to
66 the option character '\x01'.
68 The default behavior of 'getopt_long' is to permute the argument list.
69 When this implementation is used standalone, the default behavior of
70 'getopt' is to stop at the first non-option argument, but when it is
71 used as part of GNU libc it also permutes the argument list. In both
72 cases, setting the environment variable POSIXLY_CORRECT to any value
73 disables permutation.
75 If the first character of the OPTSTRING argument to 'getopt' or
76 'getopt_long' is '+', both functions will stop at the first
77 non-option argument. If it is '-', both functions will report
78 non-option arguments as arguments to the option character '\x01'. */
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. */
88 char *optarg;
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. */
103 int optind = 1;
105 /* Callers store zero here to inhibit the error message
106 for unrecognized options. */
108 int opterr = 1;
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. */
114 int optopt = '?';
116 /* Keep a global copy of all internal members of getopt_data. */
118 static struct _getopt_data getopt_data;
120 /* Exchange two adjacent subsequences of ARGV.
121 One subsequence is elements [first_nonopt,last_nonopt)
122 which contains all the non-options that have been skipped so far.
123 The other is elements [last_nonopt,optind), which contains all
124 the options processed since those non-options were skipped.
126 'first_nonopt' and 'last_nonopt' are relocated so that they describe
127 the new indices of the non-options in ARGV after they are moved. */
129 static void
130 exchange (char **argv, struct _getopt_data *d)
132 int bottom = d->__first_nonopt;
133 int middle = d->__last_nonopt;
134 int top = d->optind;
135 char *tem;
137 /* Exchange the shorter segment with the far end of the longer segment.
138 That puts the shorter segment into the right place.
139 It leaves the longer segment in the right place overall,
140 but it consists of two parts that need to be swapped next. */
142 while (top > middle && middle > bottom)
144 if (top - middle > middle - bottom)
146 /* Bottom segment is the short one. */
147 int len = middle - bottom;
148 int i;
150 /* Swap it with the top part of the top segment. */
151 for (i = 0; i < len; i++)
153 tem = argv[bottom + i];
154 argv[bottom + i] = argv[top - (middle - bottom) + i];
155 argv[top - (middle - bottom) + i] = tem;
157 /* Exclude the moved bottom segment from further swapping. */
158 top -= len;
160 else
162 /* Top segment is the short one. */
163 int len = top - middle;
164 int i;
166 /* Swap it with the bottom part of the bottom segment. */
167 for (i = 0; i < len; i++)
169 tem = argv[bottom + i];
170 argv[bottom + i] = argv[middle + i];
171 argv[middle + i] = tem;
173 /* Exclude the moved top segment from further swapping. */
174 bottom += len;
178 /* Update records for the slots the non-options now occupy. */
180 d->__first_nonopt += (d->optind - d->__last_nonopt);
181 d->__last_nonopt = d->optind;
184 /* Process the argument starting with d->__nextchar as a long option.
185 d->optind should *not* have been advanced over this argument.
187 If the value returned is -1, it was not actually a long option, the
188 state is unchanged, and the argument should be processed as a set
189 of short options (this can only happen when long_only is true).
190 Otherwise, the option (and its argument, if any) have been consumed
191 and the return value is the value to return from _getopt_internal_r. */
192 static int
193 process_long_option (int argc, char **argv, const char *optstring,
194 const struct option *longopts, int *longind,
195 int long_only, struct _getopt_data *d,
196 int print_errors, const char *prefix)
198 char *nameend;
199 size_t namelen;
200 const struct option *p;
201 const struct option *pfound = NULL;
202 int n_options;
203 int option_index;
205 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
206 /* Do nothing. */ ;
207 namelen = nameend - d->__nextchar;
209 /* First look for an exact match, counting the options as a side
210 effect. */
211 for (p = longopts, n_options = 0; p->name; p++, n_options++)
212 if (!strncmp (p->name, d->__nextchar, namelen)
213 && namelen == strlen (p->name))
215 /* Exact match found. */
216 pfound = p;
217 option_index = n_options;
218 break;
221 if (pfound == NULL)
223 /* Didn't find an exact match, so look for abbreviations. */
224 unsigned char *ambig_set = NULL;
225 int ambig_malloced = 0;
226 int ambig_fallback = 0;
227 int indfound = -1;
229 for (p = longopts, option_index = 0; p->name; p++, option_index++)
230 if (!strncmp (p->name, d->__nextchar, namelen))
232 if (pfound == NULL)
234 /* First nonexact match found. */
235 pfound = p;
236 indfound = option_index;
238 else if (long_only
239 || pfound->has_arg != p->has_arg
240 || pfound->flag != p->flag
241 || pfound->val != p->val)
243 /* Second or later nonexact match found. */
244 if (!ambig_fallback)
246 if (!print_errors)
247 /* Don't waste effort tracking the ambig set if
248 we're not going to print it anyway. */
249 ambig_fallback = 1;
250 else if (!ambig_set)
252 if (__libc_use_alloca (n_options))
253 ambig_set = alloca (n_options);
254 else if ((ambig_set = malloc (n_options)) == NULL)
255 /* Fall back to simpler error message. */
256 ambig_fallback = 1;
257 else
258 ambig_malloced = 1;
260 if (ambig_set)
262 memset (ambig_set, 0, n_options);
263 ambig_set[indfound] = 1;
266 if (ambig_set)
267 ambig_set[option_index] = 1;
272 if (ambig_set || ambig_fallback)
274 if (print_errors)
276 if (ambig_fallback)
277 fprintf (stderr, _("%s: option '%s%s' is ambiguous\n"),
278 argv[0], prefix, d->__nextchar);
279 else
281 flockfile (stderr);
282 fprintf (stderr,
283 _("%s: option '%s%s' is ambiguous; possibilities:"),
284 argv[0], prefix, d->__nextchar);
286 for (option_index = 0; option_index < n_options; option_index++)
287 if (ambig_set[option_index])
288 fprintf (stderr, " '%s%s'",
289 prefix, longopts[option_index].name);
291 /* This must use 'fprintf' even though it's only
292 printing a single character, so that it goes through
293 __fxprintf_nocancel when compiled as part of glibc. */
294 fprintf (stderr, "\n");
295 funlockfile (stderr);
298 if (ambig_malloced)
299 free (ambig_set);
300 d->__nextchar += strlen (d->__nextchar);
301 d->optind++;
302 d->optopt = 0;
303 return '?';
306 option_index = indfound;
309 if (pfound == NULL)
311 /* Can't find it as a long option. If this is not getopt_long_only,
312 or the option starts with '--' or is not a valid short option,
313 then it's an error. */
314 if (!long_only || argv[d->optind][1] == '-'
315 || strchr (optstring, *d->__nextchar) == NULL)
317 if (print_errors)
318 fprintf (stderr, _("%s: unrecognized option '%s%s'\n"),
319 argv[0], prefix, d->__nextchar);
321 d->__nextchar = NULL;
322 d->optind++;
323 d->optopt = 0;
324 return '?';
327 /* Otherwise interpret it as a short option. */
328 return -1;
331 /* We have found a matching long option. Consume it. */
332 d->optind++;
333 d->__nextchar = NULL;
334 if (*nameend)
336 /* Don't test has_arg with >, because some C compilers don't
337 allow it to be used on enums. */
338 if (pfound->has_arg)
339 d->optarg = nameend + 1;
340 else
342 if (print_errors)
343 fprintf (stderr,
344 _("%s: option '%s%s' doesn't allow an argument\n"),
345 argv[0], prefix, pfound->name);
347 d->optopt = pfound->val;
348 return '?';
351 else if (pfound->has_arg == 1)
353 if (d->optind < argc)
354 d->optarg = argv[d->optind++];
355 else
357 if (print_errors)
358 fprintf (stderr,
359 _("%s: option '%s%s' requires an argument\n"),
360 argv[0], prefix, pfound->name);
362 d->optopt = pfound->val;
363 return optstring[0] == ':' ? ':' : '?';
367 if (longind != NULL)
368 *longind = option_index;
369 if (pfound->flag)
371 *(pfound->flag) = pfound->val;
372 return 0;
374 return pfound->val;
377 /* Initialize internal data upon the first call to getopt. */
379 static const char *
380 _getopt_initialize (int argc _GL_UNUSED,
381 char **argv _GL_UNUSED, const char *optstring,
382 struct _getopt_data *d, int posixly_correct)
384 /* Start processing options with ARGV-element 1 (since ARGV-element 0
385 is the program name); the sequence of previously skipped
386 non-option ARGV-elements is empty. */
387 if (d->optind == 0)
388 d->optind = 1;
390 d->__first_nonopt = d->__last_nonopt = d->optind;
391 d->__nextchar = NULL;
393 /* Determine how to handle the ordering of options and nonoptions. */
394 if (optstring[0] == '-')
396 d->__ordering = RETURN_IN_ORDER;
397 ++optstring;
399 else if (optstring[0] == '+')
401 d->__ordering = REQUIRE_ORDER;
402 ++optstring;
404 else if (posixly_correct || !!getenv ("POSIXLY_CORRECT"))
405 d->__ordering = REQUIRE_ORDER;
406 else
407 d->__ordering = PERMUTE;
409 d->__initialized = 1;
410 return optstring;
413 /* Scan elements of ARGV (whose length is ARGC) for option characters
414 given in OPTSTRING.
416 If an element of ARGV starts with '-', and is not exactly "-" or "--",
417 then it is an option element. The characters of this element
418 (aside from the initial '-') are option characters. If 'getopt'
419 is called repeatedly, it returns successively each of the option characters
420 from each of the option elements.
422 If 'getopt' finds another option character, it returns that character,
423 updating 'optind' and 'nextchar' so that the next call to 'getopt' can
424 resume the scan with the following option character or ARGV-element.
426 If there are no more option characters, 'getopt' returns -1.
427 Then 'optind' is the index in ARGV of the first ARGV-element
428 that is not an option. (The ARGV-elements have been permuted
429 so that those that are not options now come last.)
431 OPTSTRING is a string containing the legitimate option characters.
432 If an option character is seen that is not listed in OPTSTRING,
433 return '?' after printing an error message. If you set 'opterr' to
434 zero, the error message is suppressed but we still return '?'.
436 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
437 so the following text in the same ARGV-element, or the text of the following
438 ARGV-element, is returned in 'optarg'. Two colons mean an option that
439 wants an optional arg; if there is text in the current ARGV-element,
440 it is returned in 'optarg', otherwise 'optarg' is set to zero.
442 If OPTSTRING starts with '-' or '+', it requests different methods of
443 handling the non-option ARGV-elements.
444 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
446 Long-named options begin with '--' instead of '-'.
447 Their names may be abbreviated as long as the abbreviation is unique
448 or is an exact match for some defined option. If they have an
449 argument, it follows the option name in the same ARGV-element, separated
450 from the option name by a '=', or else the in next ARGV-element.
451 When 'getopt' finds a long-named option, it returns 0 if that option's
452 'flag' field is nonzero, the value of the option's 'val' field
453 if the 'flag' field is zero.
455 The elements of ARGV aren't really const, because we permute them.
456 But we pretend they're const in the prototype to be compatible
457 with other systems.
459 LONGOPTS is a vector of 'struct option' terminated by an
460 element containing a name which is zero.
462 LONGIND returns the index in LONGOPT of the long-named option found.
463 It is only valid when a long-named option has been found by the most
464 recent call.
466 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
467 long-named options. */
470 _getopt_internal_r (int argc, char **argv, const char *optstring,
471 const struct option *longopts, int *longind,
472 int long_only, struct _getopt_data *d, int posixly_correct)
474 int print_errors = d->opterr;
476 if (argc < 1)
477 return -1;
479 d->optarg = NULL;
481 if (d->optind == 0 || !d->__initialized)
482 optstring = _getopt_initialize (argc, argv, optstring, d, posixly_correct);
483 else if (optstring[0] == '-' || optstring[0] == '+')
484 optstring++;
486 if (optstring[0] == ':')
487 print_errors = 0;
489 /* Test whether ARGV[optind] points to a non-option argument. */
490 #define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
492 if (d->__nextchar == NULL || *d->__nextchar == '\0')
494 /* Advance to the next ARGV-element. */
496 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
497 moved back by the user (who may also have changed the arguments). */
498 if (d->__last_nonopt > d->optind)
499 d->__last_nonopt = d->optind;
500 if (d->__first_nonopt > d->optind)
501 d->__first_nonopt = d->optind;
503 if (d->__ordering == PERMUTE)
505 /* If we have just processed some options following some non-options,
506 exchange them so that the options come first. */
508 if (d->__first_nonopt != d->__last_nonopt
509 && d->__last_nonopt != d->optind)
510 exchange (argv, d);
511 else if (d->__last_nonopt != d->optind)
512 d->__first_nonopt = d->optind;
514 /* Skip any additional non-options
515 and extend the range of non-options previously skipped. */
517 while (d->optind < argc && NONOPTION_P)
518 d->optind++;
519 d->__last_nonopt = d->optind;
522 /* The special ARGV-element '--' means premature end of options.
523 Skip it like a null option,
524 then exchange with previous non-options as if it were an option,
525 then skip everything else like a non-option. */
527 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
529 d->optind++;
531 if (d->__first_nonopt != d->__last_nonopt
532 && d->__last_nonopt != d->optind)
533 exchange (argv, d);
534 else if (d->__first_nonopt == d->__last_nonopt)
535 d->__first_nonopt = d->optind;
536 d->__last_nonopt = argc;
538 d->optind = argc;
541 /* If we have done all the ARGV-elements, stop the scan
542 and back over any non-options that we skipped and permuted. */
544 if (d->optind == argc)
546 /* Set the next-arg-index to point at the non-options
547 that we previously skipped, so the caller will digest them. */
548 if (d->__first_nonopt != d->__last_nonopt)
549 d->optind = d->__first_nonopt;
550 return -1;
553 /* If we have come to a non-option and did not permute it,
554 either stop the scan or describe it to the caller and pass it by. */
556 if (NONOPTION_P)
558 if (d->__ordering == REQUIRE_ORDER)
559 return -1;
560 d->optarg = argv[d->optind++];
561 return 1;
564 /* We have found another option-ARGV-element.
565 Check whether it might be a long option. */
566 if (longopts)
568 if (argv[d->optind][1] == '-')
570 /* "--foo" is always a long option. The special option
571 "--" was handled above. */
572 d->__nextchar = argv[d->optind] + 2;
573 return process_long_option (argc, argv, optstring, longopts,
574 longind, long_only, d,
575 print_errors, "--");
578 /* If long_only and the ARGV-element has the form "-f",
579 where f is a valid short option, don't consider it an
580 abbreviated form of a long option that starts with f.
581 Otherwise there would be no way to give the -f short
582 option.
584 On the other hand, if there's a long option "fubar" and
585 the ARGV-element is "-fu", do consider that an
586 abbreviation of the long option, just like "--fu", and
587 not "-f" with arg "u".
589 This distinction seems to be the most useful approach. */
590 if (long_only && (argv[d->optind][2]
591 || !strchr (optstring, argv[d->optind][1])))
593 int code;
594 d->__nextchar = argv[d->optind] + 1;
595 code = process_long_option (argc, argv, optstring, longopts,
596 longind, long_only, d,
597 print_errors, "-");
598 if (code != -1)
599 return code;
603 /* It is not a long option. Skip the initial punctuation. */
604 d->__nextchar = argv[d->optind] + 1;
607 /* Look at and handle the next short option-character. */
610 char c = *d->__nextchar++;
611 const char *temp = strchr (optstring, c);
613 /* Increment 'optind' when we start to process its last character. */
614 if (*d->__nextchar == '\0')
615 ++d->optind;
617 if (temp == NULL || c == ':' || c == ';')
619 if (print_errors)
620 fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
621 d->optopt = c;
622 return '?';
625 /* Convenience. Treat POSIX -W foo same as long option --foo */
626 if (temp[0] == 'W' && temp[1] == ';' && longopts != NULL)
628 /* This is an option that requires an argument. */
629 if (*d->__nextchar != '\0')
630 d->optarg = d->__nextchar;
631 else if (d->optind == argc)
633 if (print_errors)
634 fprintf (stderr,
635 _("%s: option requires an argument -- '%c'\n"),
636 argv[0], c);
638 d->optopt = c;
639 if (optstring[0] == ':')
640 c = ':';
641 else
642 c = '?';
643 return c;
645 else
646 d->optarg = argv[d->optind];
648 d->__nextchar = d->optarg;
649 d->optarg = NULL;
650 return process_long_option (argc, argv, optstring, longopts, longind,
651 0 /* long_only */, d, print_errors, "-W ");
653 if (temp[1] == ':')
655 if (temp[2] == ':')
657 /* This is an option that accepts an argument optionally. */
658 if (*d->__nextchar != '\0')
660 d->optarg = d->__nextchar;
661 d->optind++;
663 else
664 d->optarg = NULL;
665 d->__nextchar = NULL;
667 else
669 /* This is an option that requires an argument. */
670 if (*d->__nextchar != '\0')
672 d->optarg = d->__nextchar;
673 /* If we end this ARGV-element by taking the rest as an arg,
674 we must advance to the next element now. */
675 d->optind++;
677 else if (d->optind == argc)
679 if (print_errors)
680 fprintf (stderr,
681 _("%s: option requires an argument -- '%c'\n"),
682 argv[0], c);
684 d->optopt = c;
685 if (optstring[0] == ':')
686 c = ':';
687 else
688 c = '?';
690 else
691 /* We already incremented 'optind' once;
692 increment it again when taking next ARGV-elt as argument. */
693 d->optarg = argv[d->optind++];
694 d->__nextchar = NULL;
697 return c;
702 _getopt_internal (int argc, char **argv, const char *optstring,
703 const struct option *longopts, int *longind, int long_only,
704 int posixly_correct)
706 int result;
708 getopt_data.optind = optind;
709 getopt_data.opterr = opterr;
711 result = _getopt_internal_r (argc, argv, optstring, longopts,
712 longind, long_only, &getopt_data,
713 posixly_correct);
715 optind = getopt_data.optind;
716 optarg = getopt_data.optarg;
717 optopt = getopt_data.optopt;
719 return result;
722 /* glibc gets a LSB-compliant getopt and a POSIX-complaint __posix_getopt.
723 Standalone applications just get a POSIX-compliant getopt.
724 POSIX and LSB both require these functions to take 'char *const *argv'
725 even though this is incorrect (because of the permutation). */
726 #define GETOPT_ENTRY(NAME, POSIXLY_CORRECT) \
727 int \
728 NAME (int argc, char *const *argv, const char *optstring) \
730 return _getopt_internal (argc, (char **)argv, optstring, \
731 0, 0, 0, POSIXLY_CORRECT); \
734 #ifdef _LIBC
735 GETOPT_ENTRY(getopt, 0)
736 GETOPT_ENTRY(__posix_getopt, 1)
737 #else
738 GETOPT_ENTRY(getopt, 1)
739 #endif
742 #ifdef TEST
744 /* Compile with -DTEST to make an executable for use in testing
745 the above definition of 'getopt'. */
748 main (int argc, char **argv)
750 int c;
751 int digit_optind = 0;
753 while (1)
755 int this_option_optind = optind ? optind : 1;
757 c = getopt (argc, argv, "abc:d:0123456789");
758 if (c == -1)
759 break;
761 switch (c)
763 case '0':
764 case '1':
765 case '2':
766 case '3':
767 case '4':
768 case '5':
769 case '6':
770 case '7':
771 case '8':
772 case '9':
773 if (digit_optind != 0 && digit_optind != this_option_optind)
774 printf ("digits occur in two different argv-elements.\n");
775 digit_optind = this_option_optind;
776 printf ("option %c\n", c);
777 break;
779 case 'a':
780 printf ("option a\n");
781 break;
783 case 'b':
784 printf ("option b\n");
785 break;
787 case 'c':
788 printf ("option c with value '%s'\n", optarg);
789 break;
791 case '?':
792 break;
794 default:
795 printf ("?? getopt returned character code 0%o ??\n", c);
799 if (optind < argc)
801 printf ("non-option ARGV-elements: ");
802 while (optind < argc)
803 printf ("%s ", argv[optind++]);
804 printf ("\n");
807 exit (0);
810 #endif /* TEST */