Automatically update website with --help output. Cosmetic
[monitoring-plugins.git] / gl / getopt.c
blob3580ad825c64792235a1d94f134235fc834c2b81
1 /* Getopt for GNU.
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
4 before changing it!
5 Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004,2006
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)
12 any later version.
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 #ifndef _LIBC
24 # include <config.h>
25 #endif
27 #include "getopt.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #ifdef __VMS
35 # include <unixlib.h>
36 #endif
38 #ifdef _LIBC
39 # include <libintl.h>
40 #else
41 # include "gettext.h"
42 # define _(msgid) gettext (msgid)
43 #endif
45 #if defined _LIBC && defined USE_IN_LIBIO
46 # include <wchar.h>
47 #endif
49 #ifndef attribute_hidden
50 # define attribute_hidden
51 #endif
53 /* Unlike standard Unix `getopt', functions like `getopt_long'
54 let the user intersperse the options with the other arguments.
56 As `getopt_long' works, it permutes the elements of ARGV so that,
57 when it is done, all the options precede everything else. Thus
58 all application programs are extended to handle flexible argument order.
60 Using `getopt' or setting the environment variable POSIXLY_CORRECT
61 disables permutation.
62 Then the application's behavior is completely standard.
64 GNU application programs can use a third alternative mode in which
65 they can distinguish the relative order of options and other arguments. */
67 #include "getopt_int.h"
69 /* For communication from `getopt' to the caller.
70 When `getopt' finds an option that takes an argument,
71 the argument value is returned here.
72 Also, when `ordering' is RETURN_IN_ORDER,
73 each non-option ARGV-element is returned here. */
75 char *optarg;
77 /* Index in ARGV of the next element to be scanned.
78 This is used for communication to and from the caller
79 and for communication between successive calls to `getopt'.
81 On entry to `getopt', zero means this is the first call; initialize.
83 When `getopt' returns -1, this is the index of the first of the
84 non-option elements that the caller should itself scan.
86 Otherwise, `optind' communicates from one call to the next
87 how much of ARGV has been scanned so far. */
89 /* 1003.2 says this must be 1 before any call. */
90 int optind = 1;
92 /* Callers store zero here to inhibit the error message
93 for unrecognized options. */
95 int opterr = 1;
97 /* Set to an option character which was unrecognized.
98 This must be initialized on some systems to avoid linking in the
99 system's own getopt implementation. */
101 int optopt = '?';
103 /* Keep a global copy of all internal members of getopt_data. */
105 static struct _getopt_data getopt_data;
108 #if defined HAVE_DECL_GETENV && !HAVE_DECL_GETENV
109 extern char *getenv ();
110 #endif
112 #ifdef _LIBC
113 /* Stored original parameters.
114 XXX This is no good solution. We should rather copy the args so
115 that we can compare them later. But we must not use malloc(3). */
116 extern int __libc_argc;
117 extern char **__libc_argv;
119 /* Bash 2.0 gives us an environment variable containing flags
120 indicating ARGV elements that should not be considered arguments. */
122 # ifdef USE_NONOPTION_FLAGS
123 /* Defined in getopt_init.c */
124 extern char *__getopt_nonoption_flags;
125 # endif
127 # ifdef USE_NONOPTION_FLAGS
128 # define SWAP_FLAGS(ch1, ch2) \
129 if (d->__nonoption_flags_len > 0) \
131 char __tmp = __getopt_nonoption_flags[ch1]; \
132 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
133 __getopt_nonoption_flags[ch2] = __tmp; \
135 # else
136 # define SWAP_FLAGS(ch1, ch2)
137 # endif
138 #else /* !_LIBC */
139 # define SWAP_FLAGS(ch1, ch2)
140 #endif /* _LIBC */
142 /* Exchange two adjacent subsequences of ARGV.
143 One subsequence is elements [first_nonopt,last_nonopt)
144 which contains all the non-options that have been skipped so far.
145 The other is elements [last_nonopt,optind), which contains all
146 the options processed since those non-options were skipped.
148 `first_nonopt' and `last_nonopt' are relocated so that they describe
149 the new indices of the non-options in ARGV after they are moved. */
151 static void
152 exchange (char **argv, struct _getopt_data *d)
154 int bottom = d->__first_nonopt;
155 int middle = d->__last_nonopt;
156 int top = d->optind;
157 char *tem;
159 /* Exchange the shorter segment with the far end of the longer segment.
160 That puts the shorter segment into the right place.
161 It leaves the longer segment in the right place overall,
162 but it consists of two parts that need to be swapped next. */
164 #if defined _LIBC && defined USE_NONOPTION_FLAGS
165 /* First make sure the handling of the `__getopt_nonoption_flags'
166 string can work normally. Our top argument must be in the range
167 of the string. */
168 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
170 /* We must extend the array. The user plays games with us and
171 presents new arguments. */
172 char *new_str = malloc (top + 1);
173 if (new_str == NULL)
174 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
175 else
177 memset (__mempcpy (new_str, __getopt_nonoption_flags,
178 d->__nonoption_flags_max_len),
179 '\0', top + 1 - d->__nonoption_flags_max_len);
180 d->__nonoption_flags_max_len = top + 1;
181 __getopt_nonoption_flags = new_str;
184 #endif
186 while (top > middle && middle > bottom)
188 if (top - middle > middle - bottom)
190 /* Bottom segment is the short one. */
191 int len = middle - bottom;
192 register int i;
194 /* Swap it with the top part of the top segment. */
195 for (i = 0; i < len; i++)
197 tem = argv[bottom + i];
198 argv[bottom + i] = argv[top - (middle - bottom) + i];
199 argv[top - (middle - bottom) + i] = tem;
200 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
202 /* Exclude the moved bottom segment from further swapping. */
203 top -= len;
205 else
207 /* Top segment is the short one. */
208 int len = top - middle;
209 register int i;
211 /* Swap it with the bottom part of the bottom segment. */
212 for (i = 0; i < len; i++)
214 tem = argv[bottom + i];
215 argv[bottom + i] = argv[middle + i];
216 argv[middle + i] = tem;
217 SWAP_FLAGS (bottom + i, middle + i);
219 /* Exclude the moved top segment from further swapping. */
220 bottom += len;
224 /* Update records for the slots the non-options now occupy. */
226 d->__first_nonopt += (d->optind - d->__last_nonopt);
227 d->__last_nonopt = d->optind;
230 /* Initialize the internal data when the first call is made. */
232 static const char *
233 _getopt_initialize (int argc, char **argv, const char *optstring,
234 int posixly_correct, struct _getopt_data *d)
236 /* Start processing options with ARGV-element 1 (since ARGV-element 0
237 is the program name); the sequence of previously skipped
238 non-option ARGV-elements is empty. */
240 d->__first_nonopt = d->__last_nonopt = d->optind;
242 d->__nextchar = NULL;
244 d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
246 /* Determine how to handle the ordering of options and nonoptions. */
248 if (optstring[0] == '-')
250 d->__ordering = RETURN_IN_ORDER;
251 ++optstring;
253 else if (optstring[0] == '+')
255 d->__ordering = REQUIRE_ORDER;
256 ++optstring;
258 else if (d->__posixly_correct)
259 d->__ordering = REQUIRE_ORDER;
260 else
261 d->__ordering = PERMUTE;
263 #if defined _LIBC && defined USE_NONOPTION_FLAGS
264 if (!d->__posixly_correct
265 && argc == __libc_argc && argv == __libc_argv)
267 if (d->__nonoption_flags_max_len == 0)
269 if (__getopt_nonoption_flags == NULL
270 || __getopt_nonoption_flags[0] == '\0')
271 d->__nonoption_flags_max_len = -1;
272 else
274 const char *orig_str = __getopt_nonoption_flags;
275 int len = d->__nonoption_flags_max_len = strlen (orig_str);
276 if (d->__nonoption_flags_max_len < argc)
277 d->__nonoption_flags_max_len = argc;
278 __getopt_nonoption_flags =
279 (char *) malloc (d->__nonoption_flags_max_len);
280 if (__getopt_nonoption_flags == NULL)
281 d->__nonoption_flags_max_len = -1;
282 else
283 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
284 '\0', d->__nonoption_flags_max_len - len);
287 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
289 else
290 d->__nonoption_flags_len = 0;
291 #endif
293 return optstring;
296 /* Scan elements of ARGV (whose length is ARGC) for option characters
297 given in OPTSTRING.
299 If an element of ARGV starts with '-', and is not exactly "-" or "--",
300 then it is an option element. The characters of this element
301 (aside from the initial '-') are option characters. If `getopt'
302 is called repeatedly, it returns successively each of the option characters
303 from each of the option elements.
305 If `getopt' finds another option character, it returns that character,
306 updating `optind' and `nextchar' so that the next call to `getopt' can
307 resume the scan with the following option character or ARGV-element.
309 If there are no more option characters, `getopt' returns -1.
310 Then `optind' is the index in ARGV of the first ARGV-element
311 that is not an option. (The ARGV-elements have been permuted
312 so that those that are not options now come last.)
314 OPTSTRING is a string containing the legitimate option characters.
315 If an option character is seen that is not listed in OPTSTRING,
316 return '?' after printing an error message. If you set `opterr' to
317 zero, the error message is suppressed but we still return '?'.
319 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
320 so the following text in the same ARGV-element, or the text of the following
321 ARGV-element, is returned in `optarg'. Two colons mean an option that
322 wants an optional arg; if there is text in the current ARGV-element,
323 it is returned in `optarg', otherwise `optarg' is set to zero.
325 If OPTSTRING starts with `-' or `+', it requests different methods of
326 handling the non-option ARGV-elements.
327 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
329 Long-named options begin with `--' instead of `-'.
330 Their names may be abbreviated as long as the abbreviation is unique
331 or is an exact match for some defined option. If they have an
332 argument, it follows the option name in the same ARGV-element, separated
333 from the option name by a `=', or else the in next ARGV-element.
334 When `getopt' finds a long-named option, it returns 0 if that option's
335 `flag' field is nonzero, the value of the option's `val' field
336 if the `flag' field is zero.
338 LONGOPTS is a vector of `struct option' terminated by an
339 element containing a name which is zero.
341 LONGIND returns the index in LONGOPT of the long-named option found.
342 It is only valid when a long-named option has been found by the most
343 recent call.
345 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
346 long-named options.
348 If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
349 environment variable were set. */
352 _getopt_internal_r (int argc, char **argv, const char *optstring,
353 const struct option *longopts, int *longind,
354 int long_only, int posixly_correct, struct _getopt_data *d)
356 int print_errors = d->opterr;
357 if (optstring[0] == ':')
358 print_errors = 0;
360 if (argc < 1)
361 return -1;
363 d->optarg = NULL;
365 if (d->optind == 0 || !d->__initialized)
367 if (d->optind == 0)
368 d->optind = 1; /* Don't scan ARGV[0], the program name. */
369 optstring = _getopt_initialize (argc, argv, optstring,
370 posixly_correct, d);
371 d->__initialized = 1;
374 /* Test whether ARGV[optind] points to a non-option argument.
375 Either it does not have option syntax, or there is an environment flag
376 from the shell indicating it is not an option. The later information
377 is only used when the used in the GNU libc. */
378 #if defined _LIBC && defined USE_NONOPTION_FLAGS
379 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
380 || (d->optind < d->__nonoption_flags_len \
381 && __getopt_nonoption_flags[d->optind] == '1'))
382 #else
383 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
384 #endif
386 if (d->__nextchar == NULL || *d->__nextchar == '\0')
388 /* Advance to the next ARGV-element. */
390 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
391 moved back by the user (who may also have changed the arguments). */
392 if (d->__last_nonopt > d->optind)
393 d->__last_nonopt = d->optind;
394 if (d->__first_nonopt > d->optind)
395 d->__first_nonopt = d->optind;
397 if (d->__ordering == PERMUTE)
399 /* If we have just processed some options following some non-options,
400 exchange them so that the options come first. */
402 if (d->__first_nonopt != d->__last_nonopt
403 && d->__last_nonopt != d->optind)
404 exchange ((char **) argv, d);
405 else if (d->__last_nonopt != d->optind)
406 d->__first_nonopt = d->optind;
408 /* Skip any additional non-options
409 and extend the range of non-options previously skipped. */
411 while (d->optind < argc && NONOPTION_P)
412 d->optind++;
413 d->__last_nonopt = d->optind;
416 /* The special ARGV-element `--' means premature end of options.
417 Skip it like a null option,
418 then exchange with previous non-options as if it were an option,
419 then skip everything else like a non-option. */
421 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
423 d->optind++;
425 if (d->__first_nonopt != d->__last_nonopt
426 && d->__last_nonopt != d->optind)
427 exchange ((char **) argv, d);
428 else if (d->__first_nonopt == d->__last_nonopt)
429 d->__first_nonopt = d->optind;
430 d->__last_nonopt = argc;
432 d->optind = argc;
435 /* If we have done all the ARGV-elements, stop the scan
436 and back over any non-options that we skipped and permuted. */
438 if (d->optind == argc)
440 /* Set the next-arg-index to point at the non-options
441 that we previously skipped, so the caller will digest them. */
442 if (d->__first_nonopt != d->__last_nonopt)
443 d->optind = d->__first_nonopt;
444 return -1;
447 /* If we have come to a non-option and did not permute it,
448 either stop the scan or describe it to the caller and pass it by. */
450 if (NONOPTION_P)
452 if (d->__ordering == REQUIRE_ORDER)
453 return -1;
454 d->optarg = argv[d->optind++];
455 return 1;
458 /* We have found another option-ARGV-element.
459 Skip the initial punctuation. */
461 d->__nextchar = (argv[d->optind] + 1
462 + (longopts != NULL && argv[d->optind][1] == '-'));
465 /* Decode the current option-ARGV-element. */
467 /* Check whether the ARGV-element is a long option.
469 If long_only and the ARGV-element has the form "-f", where f is
470 a valid short option, don't consider it an abbreviated form of
471 a long option that starts with f. Otherwise there would be no
472 way to give the -f short option.
474 On the other hand, if there's a long option "fubar" and
475 the ARGV-element is "-fu", do consider that an abbreviation of
476 the long option, just like "--fu", and not "-f" with arg "u".
478 This distinction seems to be the most useful approach. */
480 if (longopts != NULL
481 && (argv[d->optind][1] == '-'
482 || (long_only && (argv[d->optind][2]
483 || !strchr (optstring, argv[d->optind][1])))))
485 char *nameend;
486 const struct option *p;
487 const struct option *pfound = NULL;
488 int exact = 0;
489 int ambig = 0;
490 int indfound = -1;
491 int option_index;
493 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
494 /* Do nothing. */ ;
496 /* Test all long options for either exact match
497 or abbreviated matches. */
498 for (p = longopts, option_index = 0; p->name; p++, option_index++)
499 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
501 if ((unsigned int) (nameend - d->__nextchar)
502 == (unsigned int) strlen (p->name))
504 /* Exact match found. */
505 pfound = p;
506 indfound = option_index;
507 exact = 1;
508 break;
510 else if (pfound == NULL)
512 /* First nonexact match found. */
513 pfound = p;
514 indfound = option_index;
516 else if (long_only
517 || pfound->has_arg != p->has_arg
518 || pfound->flag != p->flag
519 || pfound->val != p->val)
520 /* Second or later nonexact match found. */
521 ambig = 1;
524 if (ambig && !exact)
526 if (print_errors)
528 #if defined _LIBC && defined USE_IN_LIBIO
529 char *buf;
531 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
532 argv[0], argv[d->optind]) >= 0)
534 _IO_flockfile (stderr);
536 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
537 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
539 __fxprintf (NULL, "%s", buf);
541 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
542 _IO_funlockfile (stderr);
544 free (buf);
546 #else
547 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
548 argv[0], argv[d->optind]);
549 #endif
551 d->__nextchar += strlen (d->__nextchar);
552 d->optind++;
553 d->optopt = 0;
554 return '?';
557 if (pfound != NULL)
559 option_index = indfound;
560 d->optind++;
561 if (*nameend)
563 /* Don't test has_arg with >, because some C compilers don't
564 allow it to be used on enums. */
565 if (pfound->has_arg)
566 d->optarg = nameend + 1;
567 else
569 if (print_errors)
571 #if defined _LIBC && defined USE_IN_LIBIO
572 char *buf;
573 int n;
574 #endif
576 if (argv[d->optind - 1][1] == '-')
578 /* --option */
579 #if defined _LIBC && defined USE_IN_LIBIO
580 n = __asprintf (&buf, _("\
581 %s: option `--%s' doesn't allow an argument\n"),
582 argv[0], pfound->name);
583 #else
584 fprintf (stderr, _("\
585 %s: option `--%s' doesn't allow an argument\n"),
586 argv[0], pfound->name);
587 #endif
589 else
591 /* +option or -option */
592 #if defined _LIBC && defined USE_IN_LIBIO
593 n = __asprintf (&buf, _("\
594 %s: option `%c%s' doesn't allow an argument\n"),
595 argv[0], argv[d->optind - 1][0],
596 pfound->name);
597 #else
598 fprintf (stderr, _("\
599 %s: option `%c%s' doesn't allow an argument\n"),
600 argv[0], argv[d->optind - 1][0],
601 pfound->name);
602 #endif
605 #if defined _LIBC && defined USE_IN_LIBIO
606 if (n >= 0)
608 _IO_flockfile (stderr);
610 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
611 ((_IO_FILE *) stderr)->_flags2
612 |= _IO_FLAGS2_NOTCANCEL;
614 __fxprintf (NULL, "%s", buf);
616 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
617 _IO_funlockfile (stderr);
619 free (buf);
621 #endif
624 d->__nextchar += strlen (d->__nextchar);
626 d->optopt = pfound->val;
627 return '?';
630 else if (pfound->has_arg == 1)
632 if (d->optind < argc)
633 d->optarg = argv[d->optind++];
634 else
636 if (print_errors)
638 #if defined _LIBC && defined USE_IN_LIBIO
639 char *buf;
641 if (__asprintf (&buf, _("\
642 %s: option `%s' requires an argument\n"),
643 argv[0], argv[d->optind - 1]) >= 0)
645 _IO_flockfile (stderr);
647 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
648 ((_IO_FILE *) stderr)->_flags2
649 |= _IO_FLAGS2_NOTCANCEL;
651 __fxprintf (NULL, "%s", buf);
653 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
654 _IO_funlockfile (stderr);
656 free (buf);
658 #else
659 fprintf (stderr,
660 _("%s: option `%s' requires an argument\n"),
661 argv[0], argv[d->optind - 1]);
662 #endif
664 d->__nextchar += strlen (d->__nextchar);
665 d->optopt = pfound->val;
666 return optstring[0] == ':' ? ':' : '?';
669 d->__nextchar += strlen (d->__nextchar);
670 if (longind != NULL)
671 *longind = option_index;
672 if (pfound->flag)
674 *(pfound->flag) = pfound->val;
675 return 0;
677 return pfound->val;
680 /* Can't find it as a long option. If this is not getopt_long_only,
681 or the option starts with '--' or is not a valid short
682 option, then it's an error.
683 Otherwise interpret it as a short option. */
684 if (!long_only || argv[d->optind][1] == '-'
685 || strchr (optstring, *d->__nextchar) == NULL)
687 if (print_errors)
689 #if defined _LIBC && defined USE_IN_LIBIO
690 char *buf;
691 int n;
692 #endif
694 if (argv[d->optind][1] == '-')
696 /* --option */
697 #if defined _LIBC && defined USE_IN_LIBIO
698 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
699 argv[0], d->__nextchar);
700 #else
701 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
702 argv[0], d->__nextchar);
703 #endif
705 else
707 /* +option or -option */
708 #if defined _LIBC && defined USE_IN_LIBIO
709 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
710 argv[0], argv[d->optind][0], d->__nextchar);
711 #else
712 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
713 argv[0], argv[d->optind][0], d->__nextchar);
714 #endif
717 #if defined _LIBC && defined USE_IN_LIBIO
718 if (n >= 0)
720 _IO_flockfile (stderr);
722 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
723 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
725 __fxprintf (NULL, "%s", buf);
727 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
728 _IO_funlockfile (stderr);
730 free (buf);
732 #endif
734 d->__nextchar = (char *) "";
735 d->optind++;
736 d->optopt = 0;
737 return '?';
741 /* Look at and handle the next short option-character. */
744 char c = *d->__nextchar++;
745 char *temp = strchr (optstring, c);
747 /* Increment `optind' when we start to process its last character. */
748 if (*d->__nextchar == '\0')
749 ++d->optind;
751 if (temp == NULL || c == ':')
753 if (print_errors)
755 #if defined _LIBC && defined USE_IN_LIBIO
756 char *buf;
757 int n;
758 #endif
760 if (d->__posixly_correct)
762 /* 1003.2 specifies the format of this message. */
763 #if defined _LIBC && defined USE_IN_LIBIO
764 n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
765 argv[0], c);
766 #else
767 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
768 #endif
770 else
772 #if defined _LIBC && defined USE_IN_LIBIO
773 n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
774 argv[0], c);
775 #else
776 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
777 #endif
780 #if defined _LIBC && defined USE_IN_LIBIO
781 if (n >= 0)
783 _IO_flockfile (stderr);
785 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
786 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
788 __fxprintf (NULL, "%s", buf);
790 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
791 _IO_funlockfile (stderr);
793 free (buf);
795 #endif
797 d->optopt = c;
798 return '?';
800 /* Convenience. Treat POSIX -W foo same as long option --foo */
801 if (temp[0] == 'W' && temp[1] == ';')
803 char *nameend;
804 const struct option *p;
805 const struct option *pfound = NULL;
806 int exact = 0;
807 int ambig = 0;
808 int indfound = 0;
809 int option_index;
811 /* This is an option that requires an argument. */
812 if (*d->__nextchar != '\0')
814 d->optarg = d->__nextchar;
815 /* If we end this ARGV-element by taking the rest as an arg,
816 we must advance to the next element now. */
817 d->optind++;
819 else if (d->optind == argc)
821 if (print_errors)
823 /* 1003.2 specifies the format of this message. */
824 #if defined _LIBC && defined USE_IN_LIBIO
825 char *buf;
827 if (__asprintf (&buf,
828 _("%s: option requires an argument -- %c\n"),
829 argv[0], c) >= 0)
831 _IO_flockfile (stderr);
833 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
834 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
836 __fxprintf (NULL, "%s", buf);
838 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
839 _IO_funlockfile (stderr);
841 free (buf);
843 #else
844 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
845 argv[0], c);
846 #endif
848 d->optopt = c;
849 if (optstring[0] == ':')
850 c = ':';
851 else
852 c = '?';
853 return c;
855 else
856 /* We already incremented `d->optind' once;
857 increment it again when taking next ARGV-elt as argument. */
858 d->optarg = argv[d->optind++];
860 /* optarg is now the argument, see if it's in the
861 table of longopts. */
863 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
864 nameend++)
865 /* Do nothing. */ ;
867 /* Test all long options for either exact match
868 or abbreviated matches. */
869 for (p = longopts, option_index = 0; p->name; p++, option_index++)
870 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
872 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
874 /* Exact match found. */
875 pfound = p;
876 indfound = option_index;
877 exact = 1;
878 break;
880 else if (pfound == NULL)
882 /* First nonexact match found. */
883 pfound = p;
884 indfound = option_index;
886 else
887 /* Second or later nonexact match found. */
888 ambig = 1;
890 if (ambig && !exact)
892 if (print_errors)
894 #if defined _LIBC && defined USE_IN_LIBIO
895 char *buf;
897 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
898 argv[0], argv[d->optind]) >= 0)
900 _IO_flockfile (stderr);
902 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
903 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
905 __fxprintf (NULL, "%s", buf);
907 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
908 _IO_funlockfile (stderr);
910 free (buf);
912 #else
913 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
914 argv[0], argv[d->optind]);
915 #endif
917 d->__nextchar += strlen (d->__nextchar);
918 d->optind++;
919 return '?';
921 if (pfound != NULL)
923 option_index = indfound;
924 if (*nameend)
926 /* Don't test has_arg with >, because some C compilers don't
927 allow it to be used on enums. */
928 if (pfound->has_arg)
929 d->optarg = nameend + 1;
930 else
932 if (print_errors)
934 #if defined _LIBC && defined USE_IN_LIBIO
935 char *buf;
937 if (__asprintf (&buf, _("\
938 %s: option `-W %s' doesn't allow an argument\n"),
939 argv[0], pfound->name) >= 0)
941 _IO_flockfile (stderr);
943 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
944 ((_IO_FILE *) stderr)->_flags2
945 |= _IO_FLAGS2_NOTCANCEL;
947 __fxprintf (NULL, "%s", buf);
949 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
950 _IO_funlockfile (stderr);
952 free (buf);
954 #else
955 fprintf (stderr, _("\
956 %s: option `-W %s' doesn't allow an argument\n"),
957 argv[0], pfound->name);
958 #endif
961 d->__nextchar += strlen (d->__nextchar);
962 return '?';
965 else if (pfound->has_arg == 1)
967 if (d->optind < argc)
968 d->optarg = argv[d->optind++];
969 else
971 if (print_errors)
973 #if defined _LIBC && defined USE_IN_LIBIO
974 char *buf;
976 if (__asprintf (&buf, _("\
977 %s: option `%s' requires an argument\n"),
978 argv[0], argv[d->optind - 1]) >= 0)
980 _IO_flockfile (stderr);
982 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
983 ((_IO_FILE *) stderr)->_flags2
984 |= _IO_FLAGS2_NOTCANCEL;
986 __fxprintf (NULL, "%s", buf);
988 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
989 _IO_funlockfile (stderr);
991 free (buf);
993 #else
994 fprintf (stderr,
995 _("%s: option `%s' requires an argument\n"),
996 argv[0], argv[d->optind - 1]);
997 #endif
999 d->__nextchar += strlen (d->__nextchar);
1000 return optstring[0] == ':' ? ':' : '?';
1003 d->__nextchar += strlen (d->__nextchar);
1004 if (longind != NULL)
1005 *longind = option_index;
1006 if (pfound->flag)
1008 *(pfound->flag) = pfound->val;
1009 return 0;
1011 return pfound->val;
1013 d->__nextchar = NULL;
1014 return 'W'; /* Let the application handle it. */
1016 if (temp[1] == ':')
1018 if (temp[2] == ':')
1020 /* This is an option that accepts an argument optionally. */
1021 if (*d->__nextchar != '\0')
1023 d->optarg = d->__nextchar;
1024 d->optind++;
1026 else
1027 d->optarg = NULL;
1028 d->__nextchar = NULL;
1030 else
1032 /* This is an option that requires an argument. */
1033 if (*d->__nextchar != '\0')
1035 d->optarg = d->__nextchar;
1036 /* If we end this ARGV-element by taking the rest as an arg,
1037 we must advance to the next element now. */
1038 d->optind++;
1040 else if (d->optind == argc)
1042 if (print_errors)
1044 /* 1003.2 specifies the format of this message. */
1045 #if defined _LIBC && defined USE_IN_LIBIO
1046 char *buf;
1048 if (__asprintf (&buf, _("\
1049 %s: option requires an argument -- %c\n"),
1050 argv[0], c) >= 0)
1052 _IO_flockfile (stderr);
1054 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1055 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1057 __fxprintf (NULL, "%s", buf);
1059 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1060 _IO_funlockfile (stderr);
1062 free (buf);
1064 #else
1065 fprintf (stderr,
1066 _("%s: option requires an argument -- %c\n"),
1067 argv[0], c);
1068 #endif
1070 d->optopt = c;
1071 if (optstring[0] == ':')
1072 c = ':';
1073 else
1074 c = '?';
1076 else
1077 /* We already incremented `optind' once;
1078 increment it again when taking next ARGV-elt as argument. */
1079 d->optarg = argv[d->optind++];
1080 d->__nextchar = NULL;
1083 return c;
1088 _getopt_internal (int argc, char **argv, const char *optstring,
1089 const struct option *longopts, int *longind,
1090 int long_only, int posixly_correct)
1092 int result;
1094 getopt_data.optind = optind;
1095 getopt_data.opterr = opterr;
1097 result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
1098 long_only, posixly_correct, &getopt_data);
1100 optind = getopt_data.optind;
1101 optarg = getopt_data.optarg;
1102 optopt = getopt_data.optopt;
1104 return result;
1107 /* glibc gets a LSB-compliant getopt.
1108 Standalone applications get a POSIX-compliant getopt. */
1109 #if _LIBC
1110 enum { POSIXLY_CORRECT = 0 };
1111 #else
1112 enum { POSIXLY_CORRECT = 1 };
1113 #endif
1116 getopt (int argc, char *const *argv, const char *optstring)
1118 return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
1119 POSIXLY_CORRECT);
1123 #ifdef TEST
1125 /* Compile with -DTEST to make an executable for use in testing
1126 the above definition of `getopt'. */
1129 main (int argc, char **argv)
1131 int c;
1132 int digit_optind = 0;
1134 while (1)
1136 int this_option_optind = optind ? optind : 1;
1138 c = getopt (argc, argv, "abc:d:0123456789");
1139 if (c == -1)
1140 break;
1142 switch (c)
1144 case '0':
1145 case '1':
1146 case '2':
1147 case '3':
1148 case '4':
1149 case '5':
1150 case '6':
1151 case '7':
1152 case '8':
1153 case '9':
1154 if (digit_optind != 0 && digit_optind != this_option_optind)
1155 printf ("digits occur in two different argv-elements.\n");
1156 digit_optind = this_option_optind;
1157 printf ("option %c\n", c);
1158 break;
1160 case 'a':
1161 printf ("option a\n");
1162 break;
1164 case 'b':
1165 printf ("option b\n");
1166 break;
1168 case 'c':
1169 printf ("option c with value `%s'\n", optarg);
1170 break;
1172 case '?':
1173 break;
1175 default:
1176 printf ("?? getopt returned character code 0%o ??\n", c);
1180 if (optind < argc)
1182 printf ("non-option ARGV-elements: ");
1183 while (optind < argc)
1184 printf ("%s ", argv[optind++]);
1185 printf ("\n");
1188 exit (0);
1191 #endif /* TEST */