fix umount2 compilation for alpha on Linux 5.x
[uclibc-ng.git] / libc / unistd / getopt.c
blob9d2df346a1519a03b4500f9fa945b39101336f11
1 /* Getopt for GNU.
2 Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
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 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <stdio.h>
26 /* Comment out all this code if we are using the GNU C Library, and are not
27 actually compiling the library itself. This code is part of the GNU C
28 Library, but also included in many other GNU distributions. Compiling
29 and linking in this code is a waste when using the GNU C library
30 (especially if it is a shared library). Rather than having every GNU
31 program understand `configure --with-gnu-libc' and omit the object files,
32 it is simpler to just do this in the source for each such file. */
34 #define GETOPT_INTERFACE_VERSION 2
35 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
36 # include <gnu-versions.h>
37 # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
38 # define ELIDE_CODE
39 # endif
40 #endif
42 #ifndef ELIDE_CODE
45 /* This needs to come after some library #include
46 to get __GNU_LIBRARY__ defined. */
47 #if defined __GNU_LIBRARY__ || defined __UCLIBC__
48 /* Don't include stdlib.h for non-GNU C libraries because some of them
49 contain conflicting prototypes for getopt. */
50 # include <stdlib.h>
51 # include <unistd.h>
52 #endif /* GNU C library. */
54 #include <string.h>
56 /* Treat '-W foo' the same as the long option '--foo',
57 * disabled for the moment since it costs about 2k... */
58 #undef SPECIAL_TREATMENT_FOR_W
60 #if defined _LIBC && defined USE_IN_LIBIO
61 # include <wchar.h>
62 #endif
64 #ifndef attribute_hidden
65 # define attribute_hidden
66 #endif
68 /* This version of `getopt' appears to the caller like standard Unix `getopt'
69 but it behaves differently for the user, since it allows the user
70 to intersperse the options with the other arguments.
72 As `getopt' works, it permutes the elements of ARGV so that,
73 when it is done, all the options precede everything else. Thus
74 all application programs are extended to handle flexible argument order.
76 Setting the environment variable POSIXLY_CORRECT disables permutation.
77 Then the behavior is completely standard.
79 GNU application programs can use a third alternative mode in which
80 they can distinguish the relative order of options and other arguments. */
82 #include <getopt.h>
83 #include <bits/getopt_int.h>
86 /* For communication from `getopt' to the caller.
87 When `getopt' finds an option that takes an argument,
88 the argument value is returned here.
89 Also, when `ordering' is RETURN_IN_ORDER,
90 each non-option ARGV-element is returned here. */
92 char *optarg;
94 /* Index in ARGV of the next element to be scanned.
95 This is used for communication to and from the caller
96 and for communication between successive calls to `getopt'.
98 On entry to `getopt', zero means this is the first call; initialize.
100 When `getopt' returns -1, this is the index of the first of the
101 non-option elements that the caller should itself scan.
103 Otherwise, `optind' communicates from one call to the next
104 how much of ARGV has been scanned so far. */
106 /* 1003.2 says this must be 1 before any call. */
107 int optind = 1;
109 /* Callers store zero here to inhibit the error message
110 for unrecognized options. */
112 int opterr = 1;
114 /* Set to an option character which was unrecognized.
115 This must be initialized on some systems to avoid linking in the
116 system's own getopt implementation. */
118 int optopt = '?';
120 /* Keep a global copy of all internal members of getopt_data. */
122 static struct _getopt_data getopt_data;
125 #if !defined __GNU_LIBRARY__ && !defined __UCLIBC__
127 /* Avoid depending on library functions or files
128 whose names are inconsistent. */
130 #ifndef getenv
131 extern char *getenv ();
132 #endif
134 #endif /* not __GNU_LIBRARY__ */
136 #ifdef _LIBC
137 /* Stored original parameters.
138 XXX This is no good solution. We should rather copy the args so
139 that we can compare them later. But we must not use malloc(3). */
140 # ifdef USE_NONOPTION_FLAGS
141 extern int __libc_argc;
142 extern char **__libc_argv;
144 /* Bash 2.0 gives us an environment variable containing flags
145 indicating ARGV elements that should not be considered arguments. */
147 /* Defined in getopt_init.c */
148 extern char *__getopt_nonoption_flags;
150 # define SWAP_FLAGS(ch1, ch2) \
151 if (d->__nonoption_flags_len > 0) \
153 char __tmp = __getopt_nonoption_flags[ch1]; \
154 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
155 __getopt_nonoption_flags[ch2] = __tmp; \
157 # else
158 # define SWAP_FLAGS(ch1, ch2)
159 # endif
160 #else /* !_LIBC */
161 # define SWAP_FLAGS(ch1, ch2)
162 #endif /* _LIBC */
164 /* Exchange two adjacent subsequences of ARGV.
165 One subsequence is elements [first_nonopt,last_nonopt)
166 which contains all the non-options that have been skipped so far.
167 The other is elements [last_nonopt,optind), which contains all
168 the options processed since those non-options were skipped.
170 `first_nonopt' and `last_nonopt' are relocated so that they describe
171 the new indices of the non-options in ARGV after they are moved. */
173 static void
174 exchange (char **argv, struct _getopt_data *d)
176 int bottom = d->__first_nonopt;
177 int middle = d->__last_nonopt;
178 int top = d->optind;
179 char *tem;
181 /* Exchange the shorter segment with the far end of the longer segment.
182 That puts the shorter segment into the right place.
183 It leaves the longer segment in the right place overall,
184 but it consists of two parts that need to be swapped next. */
186 #if defined _LIBC && defined USE_NONOPTION_FLAGS
187 /* First make sure the handling of the `__getopt_nonoption_flags'
188 string can work normally. Our top argument must be in the range
189 of the string. */
190 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
192 /* We must extend the array. The user plays games with us and
193 presents new arguments. */
194 char *new_str = malloc (top + 1);
195 if (new_str == NULL)
196 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
197 else
199 memset (mempcpy (new_str, __getopt_nonoption_flags,
200 d->__nonoption_flags_max_len),
201 '\0', top + 1 - d->__nonoption_flags_max_len);
202 d->__nonoption_flags_max_len = top + 1;
203 __getopt_nonoption_flags = new_str;
206 #endif
208 while (top > middle && middle > bottom)
210 if (top - middle > middle - bottom)
212 /* Bottom segment is the short one. */
213 int len = middle - bottom;
214 register int i;
216 /* Swap it with the top part of the top segment. */
217 for (i = 0; i < len; i++)
219 tem = argv[bottom + i];
220 argv[bottom + i] = argv[top - (middle - bottom) + i];
221 argv[top - (middle - bottom) + i] = tem;
222 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
224 /* Exclude the moved bottom segment from further swapping. */
225 top -= len;
227 else
229 /* Top segment is the short one. */
230 int len = top - middle;
231 register int i;
233 /* Swap it with the bottom part of the bottom segment. */
234 for (i = 0; i < len; i++)
236 tem = argv[bottom + i];
237 argv[bottom + i] = argv[middle + i];
238 argv[middle + i] = tem;
239 SWAP_FLAGS (bottom + i, middle + i);
241 /* Exclude the moved top segment from further swapping. */
242 bottom += len;
246 /* Update records for the slots the non-options now occupy. */
248 d->__first_nonopt += (d->optind - d->__last_nonopt);
249 d->__last_nonopt = d->optind;
252 /* Initialize the internal data when the first call is made. */
254 static const char *
255 _getopt_initialize (attribute_unused int argc, attribute_unused char *const *argv, const char *optstring,
256 struct _getopt_data *d)
258 /* Start processing options with ARGV-element 1 (since ARGV-element 0
259 is the program name); the sequence of previously skipped
260 non-option ARGV-elements is empty. */
262 d->__first_nonopt = d->__last_nonopt = d->optind;
264 d->__nextchar = NULL;
266 d->__posixly_correct = !!getenv ("POSIXLY_CORRECT");
268 /* Determine how to handle the ordering of options and nonoptions. */
270 if (optstring[0] == '-')
272 d->__ordering = RETURN_IN_ORDER;
273 ++optstring;
275 else if (optstring[0] == '+')
277 d->__ordering = REQUIRE_ORDER;
278 ++optstring;
280 else if (d->__posixly_correct)
281 d->__ordering = REQUIRE_ORDER;
282 else
283 d->__ordering = PERMUTE;
285 #if defined _LIBC && defined USE_NONOPTION_FLAGS
286 if (!d->__posixly_correct
287 && argc == __libc_argc && argv == __libc_argv)
289 if (d->__nonoption_flags_max_len == 0)
291 if (__getopt_nonoption_flags == NULL
292 || __getopt_nonoption_flags[0] == '\0')
293 d->__nonoption_flags_max_len = -1;
294 else
296 const char *orig_str = __getopt_nonoption_flags;
297 int len = d->__nonoption_flags_max_len = strlen (orig_str);
298 if (d->__nonoption_flags_max_len < argc)
299 d->__nonoption_flags_max_len = argc;
300 __getopt_nonoption_flags =
301 (char *) malloc (d->__nonoption_flags_max_len);
302 if (__getopt_nonoption_flags == NULL)
303 d->__nonoption_flags_max_len = -1;
304 else
305 memset (mempcpy (__getopt_nonoption_flags, orig_str, len),
306 '\0', d->__nonoption_flags_max_len - len);
309 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
311 else
312 d->__nonoption_flags_len = 0;
313 #endif
315 return optstring;
318 /* Scan elements of ARGV (whose length is ARGC) for option characters
319 given in OPTSTRING.
321 If an element of ARGV starts with '-', and is not exactly "-" or "--",
322 then it is an option element. The characters of this element
323 (aside from the initial '-') are option characters. If `getopt'
324 is called repeatedly, it returns successively each of the option characters
325 from each of the option elements.
327 If `getopt' finds another option character, it returns that character,
328 updating `optind' and `nextchar' so that the next call to `getopt' can
329 resume the scan with the following option character or ARGV-element.
331 If there are no more option characters, `getopt' returns -1.
332 Then `optind' is the index in ARGV of the first ARGV-element
333 that is not an option. (The ARGV-elements have been permuted
334 so that those that are not options now come last.)
336 OPTSTRING is a string containing the legitimate option characters.
337 If an option character is seen that is not listed in OPTSTRING,
338 return '?' after printing an error message. If you set `opterr' to
339 zero, the error message is suppressed but we still return '?'.
341 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
342 so the following text in the same ARGV-element, or the text of the following
343 ARGV-element, is returned in `optarg'. Two colons mean an option that
344 wants an optional arg; if there is text in the current ARGV-element,
345 it is returned in `optarg', otherwise `optarg' is set to zero.
347 If OPTSTRING starts with `-' or `+', it requests different methods of
348 handling the non-option ARGV-elements.
349 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
351 Long-named options begin with `--' instead of `-'.
352 Their names may be abbreviated as long as the abbreviation is unique
353 or is an exact match for some defined option. If they have an
354 argument, it follows the option name in the same ARGV-element, separated
355 from the option name by a `=', or else the in next ARGV-element.
356 When `getopt' finds a long-named option, it returns 0 if that option's
357 `flag' field is nonzero, the value of the option's `val' field
358 if the `flag' field is zero.
360 The elements of ARGV aren't really const, because we permute them.
361 But we pretend they're const in the prototype to be compatible
362 with other systems.
364 LONGOPTS is a vector of `struct option' terminated by an
365 element containing a name which is zero.
367 LONGIND returns the index in LONGOPT of the long-named option found.
368 It is only valid when a long-named option has been found by the most
369 recent call.
371 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
372 long-named options. */
374 static int
375 _getopt_internal_r (int argc, char *const *argv, const char *optstring,
376 const struct option *longopts, int *longind,
377 int long_only, struct _getopt_data *d)
379 int print_errors = d->opterr;
380 if (optstring[0] == ':')
381 print_errors = 0;
383 if (argc < 1)
384 return -1;
386 d->optarg = NULL;
388 if (d->optind == 0 || !d->__initialized)
390 if (d->optind == 0)
391 d->optind = 1; /* Don't scan ARGV[0], the program name. */
392 optstring = _getopt_initialize (argc, argv, optstring, d);
393 d->__initialized = 1;
396 /* Test whether ARGV[optind] points to a non-option argument.
397 Either it does not have option syntax, or there is an environment flag
398 from the shell indicating it is not an option. The later information
399 is only used when the used in the GNU libc. */
400 #if defined _LIBC && defined USE_NONOPTION_FLAGS
401 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
402 || (d->optind < d->__nonoption_flags_len \
403 && __getopt_nonoption_flags[d->optind] == '1'))
404 #else
405 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
406 #endif
408 if (d->__nextchar == NULL || *d->__nextchar == '\0')
410 /* Advance to the next ARGV-element. */
412 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
413 moved back by the user (who may also have changed the arguments). */
414 if (d->__last_nonopt > d->optind)
415 d->__last_nonopt = d->optind;
416 if (d->__first_nonopt > d->optind)
417 d->__first_nonopt = d->optind;
419 if (d->__ordering == PERMUTE)
421 /* If we have just processed some options following some non-options,
422 exchange them so that the options come first. */
424 if (d->__first_nonopt != d->__last_nonopt
425 && d->__last_nonopt != d->optind)
426 exchange ((char **) argv, d);
427 else if (d->__last_nonopt != d->optind)
428 d->__first_nonopt = d->optind;
430 /* Skip any additional non-options
431 and extend the range of non-options previously skipped. */
433 while (d->optind < argc && NONOPTION_P)
434 d->optind++;
435 d->__last_nonopt = d->optind;
438 /* The special ARGV-element `--' means premature end of options.
439 Skip it like a null option,
440 then exchange with previous non-options as if it were an option,
441 then skip everything else like a non-option. */
443 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
445 d->optind++;
447 if (d->__first_nonopt != d->__last_nonopt
448 && d->__last_nonopt != d->optind)
449 exchange ((char **) argv, d);
450 else if (d->__first_nonopt == d->__last_nonopt)
451 d->__first_nonopt = d->optind;
452 d->__last_nonopt = argc;
454 d->optind = argc;
457 /* If we have done all the ARGV-elements, stop the scan
458 and back over any non-options that we skipped and permuted. */
460 if (d->optind == argc)
462 /* Set the next-arg-index to point at the non-options
463 that we previously skipped, so the caller will digest them. */
464 if (d->__first_nonopt != d->__last_nonopt)
465 d->optind = d->__first_nonopt;
466 return -1;
469 /* If we have come to a non-option and did not permute it,
470 either stop the scan or describe it to the caller and pass it by. */
472 if (NONOPTION_P)
474 if (d->__ordering == REQUIRE_ORDER)
475 return -1;
476 d->optarg = argv[d->optind++];
477 return 1;
480 /* We have found another option-ARGV-element.
481 Skip the initial punctuation. */
483 d->__nextchar = (argv[d->optind] + 1
484 + (longopts != NULL && argv[d->optind][1] == '-'));
487 /* Decode the current option-ARGV-element. */
489 /* Check whether the ARGV-element is a long option.
491 If long_only and the ARGV-element has the form "-f", where f is
492 a valid short option, don't consider it an abbreviated form of
493 a long option that starts with f. Otherwise there would be no
494 way to give the -f short option.
496 On the other hand, if there's a long option "fubar" and
497 the ARGV-element is "-fu", do consider that an abbreviation of
498 the long option, just like "--fu", and not "-f" with arg "u".
500 This distinction seems to be the most useful approach. */
502 if (longopts != NULL
503 && (argv[d->optind][1] == '-'
504 || (long_only && (argv[d->optind][2]
505 || !strchr (optstring, argv[d->optind][1])))))
507 char *nameend;
508 const struct option *p;
509 const struct option *pfound = NULL;
510 int exact = 0;
511 int ambig = 0;
512 int indfound = -1;
513 int option_index;
515 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
516 /* Do nothing. */ ;
518 /* Test all long options for either exact match
519 or abbreviated matches. */
520 for (p = longopts, option_index = 0; p->name; p++, option_index++)
521 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
523 if ((unsigned int) (nameend - d->__nextchar)
524 == (unsigned int) strlen (p->name))
526 /* Exact match found. */
527 pfound = p;
528 indfound = option_index;
529 exact = 1;
530 break;
532 else if (pfound == NULL)
534 /* First nonexact match found. */
535 pfound = p;
536 indfound = option_index;
538 else if (long_only
539 || pfound->has_arg != p->has_arg
540 || pfound->flag != p->flag
541 || pfound->val != p->val)
542 /* Second or later nonexact match found. */
543 ambig = 1;
546 if (ambig && !exact)
548 if (print_errors)
550 #if defined _LIBC && defined USE_IN_LIBIO
551 char *buf;
553 if (__asprintf (&buf, "%s: option `%s' is ambiguous\n",
554 argv[0], argv[d->optind]) >= 0)
556 _IO_flockfile (stderr);
558 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
559 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
561 __fxprintf (NULL, "%s", buf);
563 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
564 _IO_funlockfile (stderr);
566 free (buf);
568 #else
569 fprintf (stderr, "%s: option `%s' is ambiguous\n",
570 argv[0], argv[d->optind]);
571 #endif
573 d->__nextchar += strlen (d->__nextchar);
574 d->optind++;
575 d->optopt = 0;
576 return '?';
579 if (pfound != NULL)
581 option_index = indfound;
582 d->optind++;
583 if (*nameend)
585 /* Don't test has_arg with >, because some C compilers don't
586 allow it to be used on enums. */
587 if (pfound->has_arg)
588 d->optarg = nameend + 1;
589 else
591 if (print_errors)
593 #if defined _LIBC && defined USE_IN_LIBIO
594 char *buf;
595 int n;
596 #endif
598 if (argv[d->optind - 1][1] == '-')
600 /* --option */
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);
605 #else
606 fprintf (stderr, "\
607 %s: option `--%s' doesn't allow an argument\n",
608 argv[0], pfound->name);
609 #endif
611 else
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],
618 pfound->name);
619 #else
620 fprintf (stderr, "\
621 %s: option `%c%s' doesn't allow an argument\n",
622 argv[0], argv[d->optind - 1][0],
623 pfound->name);
624 #endif
627 #if defined _LIBC && defined USE_IN_LIBIO
628 if (n >= 0)
630 _IO_flockfile (stderr);
632 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
633 ((_IO_FILE *) stderr)->_flags2
634 |= _IO_FLAGS2_NOTCANCEL;
636 __fxprintf (NULL, "%s", buf);
638 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
639 _IO_funlockfile (stderr);
641 free (buf);
643 #endif
646 d->__nextchar += strlen (d->__nextchar);
648 d->optopt = pfound->val;
649 return '?';
652 else if (pfound->has_arg == 1)
654 if (d->optind < argc)
655 d->optarg = argv[d->optind++];
656 else
658 if (print_errors)
660 #if defined _LIBC && defined USE_IN_LIBIO
661 char *buf;
663 if (__asprintf (&buf, "\
664 %s: option `%s' requires an argument\n",
665 argv[0], argv[d->optind - 1]) >= 0)
667 _IO_flockfile (stderr);
669 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
670 ((_IO_FILE *) stderr)->_flags2
671 |= _IO_FLAGS2_NOTCANCEL;
673 __fxprintf (NULL, "%s", buf);
675 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
676 _IO_funlockfile (stderr);
678 free (buf);
680 #else
681 fprintf (stderr,
682 "%s: option `%s' requires an argument\n",
683 argv[0], argv[d->optind - 1]);
684 #endif
686 d->__nextchar += strlen (d->__nextchar);
687 d->optopt = pfound->val;
688 return optstring[0] == ':' ? ':' : '?';
691 d->__nextchar += strlen (d->__nextchar);
692 if (longind != NULL)
693 *longind = option_index;
694 if (pfound->flag)
696 *(pfound->flag) = pfound->val;
697 return 0;
699 return pfound->val;
702 /* Can't find it as a long option. If this is not getopt_long_only,
703 or the option starts with '--' or is not a valid short
704 option, then it's an error.
705 Otherwise interpret it as a short option. */
706 if (!long_only || argv[d->optind][1] == '-'
707 || strchr (optstring, *d->__nextchar) == NULL)
709 if (print_errors)
711 #if defined _LIBC && defined USE_IN_LIBIO
712 char *buf;
713 int n;
714 #endif
716 if (argv[d->optind][1] == '-')
718 /* --option */
719 #if defined _LIBC && defined USE_IN_LIBIO
720 n = __asprintf (&buf, "%s: unrecognized option `--%s'\n",
721 argv[0], d->__nextchar);
722 #else
723 fprintf (stderr, "%s: unrecognized option `--%s'\n",
724 argv[0], d->__nextchar);
725 #endif
727 else
729 /* +option or -option */
730 #if defined _LIBC && defined USE_IN_LIBIO
731 n = __asprintf (&buf, "%s: unrecognized option `%c%s'\n",
732 argv[0], argv[d->optind][0], d->__nextchar);
733 #else
734 fprintf (stderr, "%s: unrecognized option `%c%s'\n",
735 argv[0], argv[d->optind][0], d->__nextchar);
736 #endif
739 #if defined _LIBC && defined USE_IN_LIBIO
740 if (n >= 0)
742 _IO_flockfile (stderr);
744 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
745 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
747 __fxprintf (NULL, "%s", buf);
749 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
750 _IO_funlockfile (stderr);
752 free (buf);
754 #endif
756 d->__nextchar = (char *) "";
757 d->optind++;
758 d->optopt = 0;
759 return '?';
763 /* Look at and handle the next short option-character. */
766 char c = *d->__nextchar++;
767 char *temp = strchr (optstring, c);
769 /* Increment `optind' when we start to process its last character. */
770 if (*d->__nextchar == '\0')
771 ++d->optind;
773 if (temp == NULL || c == ':')
775 if (print_errors)
777 #if defined _LIBC && defined USE_IN_LIBIO
778 char *buf;
779 int n;
780 #endif
782 if (d->__posixly_correct)
784 /* 1003.2 specifies the format of this message. */
785 #if defined _LIBC && defined USE_IN_LIBIO
786 n = __asprintf (&buf, "%s: illegal option -- %c\n",
787 argv[0], c);
788 #else
789 fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);
790 #endif
792 else
794 #if defined _LIBC && defined USE_IN_LIBIO
795 n = __asprintf (&buf, "%s: invalid option -- %c\n",
796 argv[0], c);
797 #else
798 fprintf (stderr, "%s: invalid option -- %c\n", argv[0], c);
799 #endif
802 #if defined _LIBC && defined USE_IN_LIBIO
803 if (n >= 0)
805 _IO_flockfile (stderr);
807 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
808 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
810 __fxprintf (NULL, "%s", buf);
812 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
813 _IO_funlockfile (stderr);
815 free (buf);
817 #endif
819 d->optopt = c;
820 return '?';
822 #ifdef SPECIAL_TREATMENT_FOR_W
823 /* Convenience. Treat POSIX -W foo same as long option --foo */
824 if (temp[0] == 'W' && temp[1] == ';')
826 char *nameend;
827 const struct option *p;
828 const struct option *pfound = NULL;
829 int exact = 0;
830 int ambig = 0;
831 int indfound = 0;
832 int option_index;
834 /* This is an option that requires an argument. */
835 if (*d->__nextchar != '\0')
837 d->optarg = d->__nextchar;
838 /* If we end this ARGV-element by taking the rest as an arg,
839 we must advance to the next element now. */
840 d->optind++;
842 else if (d->optind == argc)
844 if (print_errors)
846 /* 1003.2 specifies the format of this message. */
847 #if defined _LIBC && defined USE_IN_LIBIO
848 char *buf;
850 if (__asprintf (&buf,
851 "%s: option requires an argument -- %c\n",
852 argv[0], c) >= 0)
854 _IO_flockfile (stderr);
856 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
857 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
859 __fxprintf (NULL, "%s", buf);
861 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
862 _IO_funlockfile (stderr);
864 free (buf);
866 #else
867 fprintf (stderr, "%s: option requires an argument -- %c\n",
868 argv[0], c);
869 #endif
871 d->optopt = c;
872 if (optstring[0] == ':')
873 c = ':';
874 else
875 c = '?';
876 return c;
878 else
879 /* We already incremented `d->optind' once;
880 increment it again when taking next ARGV-elt as argument. */
881 d->optarg = argv[d->optind++];
883 /* optarg is now the argument, see if it's in the
884 table of longopts. */
886 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
887 nameend++)
888 /* Do nothing. */ ;
890 /* Test all long options for either exact match
891 or abbreviated matches. */
892 for (p = longopts, option_index = 0; p->name; p++, option_index++)
893 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
895 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
897 /* Exact match found. */
898 pfound = p;
899 indfound = option_index;
900 exact = 1;
901 break;
903 else if (pfound == NULL)
905 /* First nonexact match found. */
906 pfound = p;
907 indfound = option_index;
909 else
910 /* Second or later nonexact match found. */
911 ambig = 1;
913 if (ambig && !exact)
915 if (print_errors)
917 #if defined _LIBC && defined USE_IN_LIBIO
918 char *buf;
920 if (__asprintf (&buf, "%s: option `-W %s' is ambiguous\n",
921 argv[0], argv[d->optind]) >= 0)
923 _IO_flockfile (stderr);
925 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
926 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
928 __fxprintf (NULL, "%s", buf);
930 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
931 _IO_funlockfile (stderr);
933 free (buf);
935 #else
936 fprintf (stderr, "%s: option `-W %s' is ambiguous\n",
937 argv[0], argv[d->optind]);
938 #endif
940 d->__nextchar += strlen (d->__nextchar);
941 d->optind++;
942 return '?';
944 if (pfound != NULL)
946 option_index = indfound;
947 if (*nameend)
949 /* Don't test has_arg with >, because some C compilers don't
950 allow it to be used on enums. */
951 if (pfound->has_arg)
952 d->optarg = nameend + 1;
953 else
955 if (print_errors)
957 #if defined _LIBC && defined USE_IN_LIBIO
958 char *buf;
960 if (__asprintf (&buf, "\
961 %s: option `-W %s' doesn't allow an argument\n",
962 argv[0], pfound->name) >= 0)
964 _IO_flockfile (stderr);
966 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
967 ((_IO_FILE *) stderr)->_flags2
968 |= _IO_FLAGS2_NOTCANCEL;
970 __fxprintf (NULL, "%s", buf);
972 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
973 _IO_funlockfile (stderr);
975 free (buf);
977 #else
978 fprintf (stderr, "\
979 %s: option `-W %s' doesn't allow an argument\n",
980 argv[0], pfound->name);
981 #endif
984 d->__nextchar += strlen (d->__nextchar);
985 return '?';
988 else if (pfound->has_arg == 1)
990 if (d->optind < argc)
991 d->optarg = argv[d->optind++];
992 else
994 if (print_errors)
996 #if defined _LIBC && defined USE_IN_LIBIO
997 char *buf;
999 if (__asprintf (&buf, "\
1000 %s: option `%s' requires an argument\n",
1001 argv[0], argv[d->optind - 1]) >= 0)
1003 _IO_flockfile (stderr);
1005 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1006 ((_IO_FILE *) stderr)->_flags2
1007 |= _IO_FLAGS2_NOTCANCEL;
1009 __fxprintf (NULL, "%s", buf);
1011 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1012 _IO_funlockfile (stderr);
1014 free (buf);
1016 #else
1017 fprintf (stderr,
1018 "%s: option `%s' requires an argument\n",
1019 argv[0], argv[d->optind - 1]);
1020 #endif
1022 d->__nextchar += strlen (d->__nextchar);
1023 return optstring[0] == ':' ? ':' : '?';
1026 d->__nextchar += strlen (d->__nextchar);
1027 if (longind != NULL)
1028 *longind = option_index;
1029 if (pfound->flag)
1031 *(pfound->flag) = pfound->val;
1032 return 0;
1034 return pfound->val;
1036 d->__nextchar = NULL;
1037 return 'W'; /* Let the application handle it. */
1039 #endif
1040 if (temp[1] == ':')
1042 if (temp[2] == ':')
1044 /* This is an option that accepts an argument optionally. */
1045 if (*d->__nextchar != '\0')
1047 d->optarg = d->__nextchar;
1048 d->optind++;
1050 else
1051 d->optarg = NULL;
1052 d->__nextchar = NULL;
1054 else
1056 /* This is an option that requires an argument. */
1057 if (*d->__nextchar != '\0')
1059 d->optarg = d->__nextchar;
1060 /* If we end this ARGV-element by taking the rest as an arg,
1061 we must advance to the next element now. */
1062 d->optind++;
1064 else if (d->optind == argc)
1066 if (print_errors)
1068 /* 1003.2 specifies the format of this message. */
1069 #if defined _LIBC && defined USE_IN_LIBIO
1070 char *buf;
1072 if (__asprintf (&buf, "\
1073 %s: option requires an argument -- %c\n",
1074 argv[0], c) >= 0)
1076 _IO_flockfile (stderr);
1078 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1079 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1081 __fxprintf (NULL, "%s", buf);
1083 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1084 _IO_funlockfile (stderr);
1086 free (buf);
1088 #else
1089 fprintf (stderr,
1090 "%s: option requires an argument -- %c\n",
1091 argv[0], c);
1092 #endif
1094 d->optopt = c;
1095 if (optstring[0] == ':')
1096 c = ':';
1097 else
1098 c = '?';
1100 else
1101 /* We already incremented `optind' once;
1102 increment it again when taking next ARGV-elt as argument. */
1103 d->optarg = argv[d->optind++];
1104 d->__nextchar = NULL;
1107 return c;
1112 _getopt_internal (int argc, char *const *argv, const char *optstring,
1113 const struct option *longopts, int *longind, int long_only)
1115 int result;
1117 getopt_data.optind = optind;
1118 getopt_data.opterr = opterr;
1120 result = _getopt_internal_r (argc, argv, optstring, longopts,
1121 longind, long_only, &getopt_data);
1123 optind = getopt_data.optind;
1124 optarg = getopt_data.optarg;
1125 optopt = getopt_data.optopt;
1127 return result;
1131 getopt (int argc, char *const *argv, const char *optstring)
1133 return _getopt_internal (argc, argv, optstring,
1134 (const struct option *) 0,
1135 (int *) 0,
1138 libc_hidden_def(getopt)
1140 #if defined __UCLIBC_HAS_GETOPT_LONG__
1142 getopt_long (int argc, char *const *argv, const char *options,
1143 const struct option *long_options, int *opt_index)
1145 return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
1149 _getopt_long_r (int argc, char *const *argv, const char *options,
1150 const struct option *long_options, int *opt_index,
1151 struct _getopt_data *d)
1153 return _getopt_internal_r (argc, argv, options, long_options, opt_index,
1154 0, d);
1157 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
1158 If an option that starts with '-' (not '--') doesn't match a long option,
1159 but does match a short option, it is parsed as a short option
1160 instead. */
1163 getopt_long_only (int argc, char *const *argv, const char *options,
1164 const struct option *long_options, int *opt_index)
1166 return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
1168 #endif /* __UCLIBC_HAS_GETOPT_LONG__ */
1171 _getopt_long_only_r (int argc, char *const *argv, const char *options,
1172 const struct option *long_options, int *opt_index,
1173 struct _getopt_data *d)
1175 return _getopt_internal_r (argc, argv, options, long_options, opt_index, 1, d);
1178 #endif /* Not ELIDE_CODE. */