155
/* Describe how to deal with options that follow non-option ARGV-elements.
157
If the caller did not specify anything,
158
the default is REQUIRE_ORDER if the environment variable
159
POSIXLY_CORRECT is defined, PERMUTE otherwise.
161
REQUIRE_ORDER means don't recognize them as options;
162
stop option processing when the first non-option is seen.
163
This is what Unix does.
164
This mode of operation is selected by either setting the environment
165
variable POSIXLY_CORRECT, or using `+' as the first character
166
of the list of option characters.
168
PERMUTE is the default. We permute the contents of ARGV as we scan,
169
so that eventually all the non-options are at the end. This allows options
170
to be given in any order, even with programs that were not written to
173
RETURN_IN_ORDER is an option available to programs that were written
174
to expect options and other ARGV-elements in any order and that care about
175
the ordering of the two. We describe each non-option ARGV-element
176
as if it were the argument of an option with character code 1.
177
Using `-' as the first character of the list of option characters
178
selects this mode of operation.
180
The special argument `--' forces an end of option-scanning regardless
181
of the value of `ordering'. In the case of RETURN_IN_ORDER, only
182
`--' can cause `getopt' to return -1 with `optind' != ARGC. */
186
REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
189
/* Value of POSIXLY_CORRECT environment variable. */
190
static char *posixly_correct;
99
/* Keep a global copy of all internal members of getopt_data. */
101
static struct _getopt_data getopt_data;
192
#ifdef __GNU_LIBRARY__
193
/* We want to avoid inclusion of string.h with non-GNU libraries
194
because there are many ways it can cause trouble.
195
On some systems, it contains special magic macros that don't work
198
# define my_index strchr
204
# include <strings.h>
207
/* Avoid depending on library functions or files
208
whose names are inconsistent. */
104
#if defined HAVE_DECL_GETENV && !HAVE_DECL_GETENV
211
105
extern char *getenv ();
228
/* If using GCC, we can safely declare strlen this way.
229
If not using GCC, it is ok not to declare it. */
231
/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
232
That was relevant to code that was here before. */
233
# if (!defined __STDC__ || !__STDC__) && !defined strlen
234
/* gcc with -traditional declares the built-in strlen to return int,
235
and has done so at least since version 2.4.5. -- rms. */
236
extern int strlen (const char *);
237
# endif /* not __STDC__ */
238
#endif /* __GNUC__ */
240
#endif /* not __GNU_LIBRARY__ */
242
/* Handle permutation of arguments. */
244
/* Describe the part of ARGV that contains non-options that have
245
been skipped. `first_nonopt' is the index in ARGV of the first of them;
246
`last_nonopt' is the index after the last of them. */
248
static int first_nonopt;
249
static int last_nonopt;
109
/* Stored original parameters.
110
XXX This is no good solution. We should rather copy the args so
111
that we can compare them later. But we must not use malloc(3). */
112
extern int __libc_argc;
113
extern char **__libc_argv;
252
115
/* Bash 2.0 gives us an environment variable containing flags
253
116
indicating ARGV elements that should not be considered arguments. */
255
#ifdef USE_NONOPTION_FLAGS
118
# ifdef USE_NONOPTION_FLAGS
256
119
/* Defined in getopt_init.c */
257
120
extern char *__getopt_nonoption_flags;
259
static int nonoption_flags_max_len;
260
static int nonoption_flags_len;
263
static int original_argc;
264
static char *const *original_argv;
266
/* Make sure the environment variable bash 2.0 puts in the environment
267
is valid for the getopt call we must make sure that the ARGV passed
268
to getopt is that one passed to the process. */
270
__attribute__ ((unused))
271
store_args_and_env (int argc, char *const *argv)
273
/* XXX This is no good solution. We should rather copy the args so
274
that we can compare them later. But we must not use malloc(3). */
275
original_argc = argc;
276
original_argv = argv;
278
# ifdef text_set_element
279
text_set_element (__libc_subinit, store_args_and_env);
280
# endif /* text_set_element */
282
123
# ifdef USE_NONOPTION_FLAGS
283
124
# define SWAP_FLAGS(ch1, ch2) \
284
if (nonoption_flags_len > 0) \
286
char __tmp = __getopt_nonoption_flags[ch1]; \
287
__getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
288
__getopt_nonoption_flags[ch2] = __tmp; \
125
if (d->__nonoption_flags_len > 0) \
127
char __tmp = __getopt_nonoption_flags[ch1]; \
128
__getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
129
__getopt_nonoption_flags[ch2] = __tmp; \
291
132
# define SWAP_FLAGS(ch1, ch2)
294
135
# define SWAP_FLAGS(ch1, ch2)
297
138
/* Exchange two adjacent subsequences of ARGV.
298
139
One subsequence is elements [first_nonopt,last_nonopt)
325
161
/* First make sure the handling of the `__getopt_nonoption_flags'
326
162
string can work normally. Our top argument must be in the range
327
163
of the string. */
328
if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
164
if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
330
166
/* We must extend the array. The user plays games with us and
331
presents new arguments. */
167
presents new arguments. */
332
168
char *new_str = malloc (top + 1);
333
169
if (new_str == NULL)
334
nonoption_flags_len = nonoption_flags_max_len = 0;
170
d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
337
memset (__mempcpy (new_str, __getopt_nonoption_flags,
338
nonoption_flags_max_len),
339
'\0', top + 1 - nonoption_flags_max_len);
340
nonoption_flags_max_len = top + 1;
341
__getopt_nonoption_flags = new_str;
173
memset (__mempcpy (new_str, __getopt_nonoption_flags,
174
d->__nonoption_flags_max_len),
175
'\0', top + 1 - d->__nonoption_flags_max_len);
176
d->__nonoption_flags_max_len = top + 1;
177
__getopt_nonoption_flags = new_str;
346
182
while (top > middle && middle > bottom)
348
184
if (top - middle > middle - bottom)
350
/* Bottom segment is the short one. */
351
int len = middle - bottom;
186
/* Bottom segment is the short one. */
187
int len = middle - bottom;
354
/* Swap it with the top part of the top segment. */
355
for (i = 0; i < len; i++)
357
tem = argv[bottom + i];
358
argv[bottom + i] = argv[top - (middle - bottom) + i];
359
argv[top - (middle - bottom) + i] = tem;
360
SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
362
/* Exclude the moved bottom segment from further swapping. */
190
/* Swap it with the top part of the top segment. */
191
for (i = 0; i < len; i++)
193
tem = argv[bottom + i];
194
argv[bottom + i] = argv[top - (middle - bottom) + i];
195
argv[top - (middle - bottom) + i] = tem;
196
SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
198
/* Exclude the moved bottom segment from further swapping. */
367
/* Top segment is the short one. */
368
int len = top - middle;
203
/* Top segment is the short one. */
204
int len = top - middle;
371
/* Swap it with the bottom part of the bottom segment. */
372
for (i = 0; i < len; i++)
374
tem = argv[bottom + i];
375
argv[bottom + i] = argv[middle + i];
376
argv[middle + i] = tem;
377
SWAP_FLAGS (bottom + i, middle + i);
379
/* Exclude the moved top segment from further swapping. */
207
/* Swap it with the bottom part of the bottom segment. */
208
for (i = 0; i < len; i++)
210
tem = argv[bottom + i];
211
argv[bottom + i] = argv[middle + i];
212
argv[middle + i] = tem;
213
SWAP_FLAGS (bottom + i, middle + i);
215
/* Exclude the moved top segment from further swapping. */
384
220
/* Update records for the slots the non-options now occupy. */
386
first_nonopt += (optind - last_nonopt);
387
last_nonopt = optind;
222
d->__first_nonopt += (d->optind - d->__last_nonopt);
223
d->__last_nonopt = d->optind;
390
226
/* Initialize the internal data when the first call is made. */
392
#if defined __STDC__ && __STDC__
393
static const char *_getopt_initialize (int, char *const *, const char *);
395
228
static const char *
396
_getopt_initialize (argc, argv, optstring)
399
const char *optstring;
229
_getopt_initialize (int argc _GL_UNUSED,
230
char **argv _GL_UNUSED, const char *optstring,
231
struct _getopt_data *d, int posixly_correct)
401
233
/* Start processing options with ARGV-element 1 (since ARGV-element 0
402
234
is the program name); the sequence of previously skipped
403
235
non-option ARGV-elements is empty. */
405
first_nonopt = last_nonopt = optind;
409
posixly_correct = getenv ("POSIXLY_CORRECT");
237
d->__first_nonopt = d->__last_nonopt = d->optind;
239
d->__nextchar = NULL;
241
d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
411
243
/* Determine how to handle the ordering of options and nonoptions. */
413
245
if (optstring[0] == '-')
415
ordering = RETURN_IN_ORDER;
247
d->__ordering = RETURN_IN_ORDER;
418
250
else if (optstring[0] == '+')
420
ordering = REQUIRE_ORDER;
252
d->__ordering = REQUIRE_ORDER;
423
else if (posixly_correct != NULL)
424
ordering = REQUIRE_ORDER;
255
else if (d->__posixly_correct)
256
d->__ordering = REQUIRE_ORDER;
258
d->__ordering = PERMUTE;
428
260
#if defined _LIBC && defined USE_NONOPTION_FLAGS
429
if (posixly_correct == NULL
430
&& argc == original_argc && argv == original_argv)
261
if (!d->__posixly_correct
262
&& argc == __libc_argc && argv == __libc_argv)
432
if (nonoption_flags_max_len == 0)
434
if (__getopt_nonoption_flags == NULL
435
|| __getopt_nonoption_flags[0] == '\0')
436
nonoption_flags_max_len = -1;
439
const char *orig_str = __getopt_nonoption_flags;
440
int len = nonoption_flags_max_len = strlen (orig_str);
441
if (nonoption_flags_max_len < argc)
442
nonoption_flags_max_len = argc;
443
__getopt_nonoption_flags =
444
(char *) malloc (nonoption_flags_max_len);
445
if (__getopt_nonoption_flags == NULL)
446
nonoption_flags_max_len = -1;
448
memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
449
'\0', nonoption_flags_max_len - len);
452
nonoption_flags_len = nonoption_flags_max_len;
264
if (d->__nonoption_flags_max_len == 0)
266
if (__getopt_nonoption_flags == NULL
267
|| __getopt_nonoption_flags[0] == '\0')
268
d->__nonoption_flags_max_len = -1;
271
const char *orig_str = __getopt_nonoption_flags;
272
int len = d->__nonoption_flags_max_len = strlen (orig_str);
273
if (d->__nonoption_flags_max_len < argc)
274
d->__nonoption_flags_max_len = argc;
275
__getopt_nonoption_flags =
276
(char *) malloc (d->__nonoption_flags_max_len);
277
if (__getopt_nonoption_flags == NULL)
278
d->__nonoption_flags_max_len = -1;
280
memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
281
'\0', d->__nonoption_flags_max_len - len);
284
d->__nonoption_flags_len = d->__nonoption_flags_max_len;
455
nonoption_flags_len = 0;
287
d->__nonoption_flags_len = 0;
458
290
return optstring;
545
374
from the shell indicating it is not an option. The later information
546
375
is only used when the used in the GNU libc. */
547
376
#if defined _LIBC && defined USE_NONOPTION_FLAGS
548
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
549
|| (optind < nonoption_flags_len \
550
&& __getopt_nonoption_flags[optind] == '1'))
377
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
378
|| (d->optind < d->__nonoption_flags_len \
379
&& __getopt_nonoption_flags[d->optind] == '1'))
552
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
381
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
555
if (nextchar == NULL || *nextchar == '\0')
384
if (d->__nextchar == NULL || *d->__nextchar == '\0')
557
386
/* Advance to the next ARGV-element. */
559
388
/* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
560
moved back by the user (who may also have changed the arguments). */
561
if (last_nonopt > optind)
562
last_nonopt = optind;
563
if (first_nonopt > optind)
564
first_nonopt = optind;
566
if (ordering == PERMUTE)
568
/* If we have just processed some options following some non-options,
569
exchange them so that the options come first. */
571
if (first_nonopt != last_nonopt && last_nonopt != optind)
572
exchange ((char **) argv);
573
else if (last_nonopt != optind)
574
first_nonopt = optind;
576
/* Skip any additional non-options
577
and extend the range of non-options previously skipped. */
579
while (optind < argc && NONOPTION_P)
581
last_nonopt = optind;
389
moved back by the user (who may also have changed the arguments). */
390
if (d->__last_nonopt > d->optind)
391
d->__last_nonopt = d->optind;
392
if (d->__first_nonopt > d->optind)
393
d->__first_nonopt = d->optind;
395
if (d->__ordering == PERMUTE)
397
/* If we have just processed some options following some non-options,
398
exchange them so that the options come first. */
400
if (d->__first_nonopt != d->__last_nonopt
401
&& d->__last_nonopt != d->optind)
402
exchange ((char **) argv, d);
403
else if (d->__last_nonopt != d->optind)
404
d->__first_nonopt = d->optind;
406
/* Skip any additional non-options
407
and extend the range of non-options previously skipped. */
409
while (d->optind < argc && NONOPTION_P)
411
d->__last_nonopt = d->optind;
584
414
/* The special ARGV-element `--' means premature end of options.
585
Skip it like a null option,
586
then exchange with previous non-options as if it were an option,
587
then skip everything else like a non-option. */
589
if (optind != argc && !strcmp (argv[optind], "--"))
593
if (first_nonopt != last_nonopt && last_nonopt != optind)
594
exchange ((char **) argv);
595
else if (first_nonopt == last_nonopt)
596
first_nonopt = optind;
415
Skip it like a null option,
416
then exchange with previous non-options as if it were an option,
417
then skip everything else like a non-option. */
419
if (d->optind != argc && !strcmp (argv[d->optind], "--"))
423
if (d->__first_nonopt != d->__last_nonopt
424
&& d->__last_nonopt != d->optind)
425
exchange ((char **) argv, d);
426
else if (d->__first_nonopt == d->__last_nonopt)
427
d->__first_nonopt = d->optind;
428
d->__last_nonopt = argc;
602
433
/* If we have done all the ARGV-elements, stop the scan
603
and back over any non-options that we skipped and permuted. */
434
and back over any non-options that we skipped and permuted. */
607
/* Set the next-arg-index to point at the non-options
608
that we previously skipped, so the caller will digest them. */
609
if (first_nonopt != last_nonopt)
610
optind = first_nonopt;
436
if (d->optind == argc)
438
/* Set the next-arg-index to point at the non-options
439
that we previously skipped, so the caller will digest them. */
440
if (d->__first_nonopt != d->__last_nonopt)
441
d->optind = d->__first_nonopt;
614
445
/* If we have come to a non-option and did not permute it,
615
either stop the scan or describe it to the caller and pass it by. */
446
either stop the scan or describe it to the caller and pass it by. */
619
if (ordering == REQUIRE_ORDER)
621
optarg = argv[optind++];
450
if (d->__ordering == REQUIRE_ORDER)
452
d->optarg = argv[d->optind++];
625
456
/* We have found another option-ARGV-element.
626
Skip the initial punctuation. */
457
Skip the initial punctuation. */
628
nextchar = (argv[optind] + 1
629
+ (longopts != NULL && argv[optind][1] == '-'));
459
d->__nextchar = (argv[d->optind] + 1
460
+ (longopts != NULL && argv[d->optind][1] == '-'));
632
463
/* Decode the current option-ARGV-element. */
656
488
int indfound = -1;
657
489
int option_index;
659
for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
491
for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
662
494
/* Test all long options for either exact match
663
or abbreviated matches. */
495
or abbreviated matches. */
664
496
for (p = longopts, option_index = 0; p->name; p++, option_index++)
665
if (!strncmp (p->name, nextchar, nameend - nextchar))
667
if ((unsigned int) (nameend - nextchar)
668
== (unsigned int) strlen (p->name))
670
/* Exact match found. */
672
indfound = option_index;
676
else if (pfound == NULL)
678
/* First nonexact match found. */
680
indfound = option_index;
683
|| pfound->has_arg != p->has_arg
684
|| pfound->flag != p->flag
685
|| pfound->val != p->val)
686
/* Second or later nonexact match found. */
497
if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
499
if ((unsigned int) (nameend - d->__nextchar)
500
== (unsigned int) strlen (p->name))
502
/* Exact match found. */
504
indfound = option_index;
508
else if (pfound == NULL)
510
/* First nonexact match found. */
512
indfound = option_index;
515
|| pfound->has_arg != p->has_arg
516
|| pfound->flag != p->flag
517
|| pfound->val != p->val)
518
/* Second or later nonexact match found. */
690
522
if (ambig && !exact)
693
fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
694
argv[0], argv[optind]);
695
nextchar += strlen (nextchar);
526
#if defined _LIBC && defined USE_IN_LIBIO
529
if (__asprintf (&buf, _("%s: option '%s' is ambiguous\n"),
530
argv[0], argv[d->optind]) >= 0)
532
_IO_flockfile (stderr);
534
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
535
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
537
__fxprintf (NULL, "%s", buf);
539
((_IO_FILE *) stderr)->_flags2 = old_flags2;
540
_IO_funlockfile (stderr);
545
fprintf (stderr, _("%s: option '%s' is ambiguous\n"),
546
argv[0], argv[d->optind]);
549
d->__nextchar += strlen (d->__nextchar);
701
555
if (pfound != NULL)
703
option_index = indfound;
707
/* Don't test has_arg with >, because some C compilers don't
708
allow it to be used on enums. */
710
optarg = nameend + 1;
715
if (argv[optind - 1][1] == '-')
718
_("%s: option `--%s' doesn't allow an argument\n"),
719
argv[0], pfound->name);
721
/* +option or -option */
723
_("%s: option `%c%s' doesn't allow an argument\n"),
724
argv[0], argv[optind - 1][0], pfound->name);
727
nextchar += strlen (nextchar);
729
optopt = pfound->val;
733
else if (pfound->has_arg == 1)
736
optarg = argv[optind++];
741
_("%s: option `%s' requires an argument\n"),
742
argv[0], argv[optind - 1]);
743
nextchar += strlen (nextchar);
744
optopt = pfound->val;
745
return optstring[0] == ':' ? ':' : '?';
748
nextchar += strlen (nextchar);
750
*longind = option_index;
753
*(pfound->flag) = pfound->val;
557
option_index = indfound;
561
/* Don't test has_arg with >, because some C compilers don't
562
allow it to be used on enums. */
564
d->optarg = nameend + 1;
569
#if defined _LIBC && defined USE_IN_LIBIO
574
if (argv[d->optind - 1][1] == '-')
577
#if defined _LIBC && defined USE_IN_LIBIO
578
n = __asprintf (&buf, _("\
579
%s: option '--%s' doesn't allow an argument\n"),
580
argv[0], pfound->name);
582
fprintf (stderr, _("\
583
%s: option '--%s' doesn't allow an argument\n"),
584
argv[0], pfound->name);
589
/* +option or -option */
590
#if defined _LIBC && defined USE_IN_LIBIO
591
n = __asprintf (&buf, _("\
592
%s: option '%c%s' doesn't allow an argument\n"),
593
argv[0], argv[d->optind - 1][0],
596
fprintf (stderr, _("\
597
%s: option '%c%s' doesn't allow an argument\n"),
598
argv[0], argv[d->optind - 1][0],
603
#if defined _LIBC && defined USE_IN_LIBIO
606
_IO_flockfile (stderr);
608
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
609
((_IO_FILE *) stderr)->_flags2
610
|= _IO_FLAGS2_NOTCANCEL;
612
__fxprintf (NULL, "%s", buf);
614
((_IO_FILE *) stderr)->_flags2 = old_flags2;
615
_IO_funlockfile (stderr);
622
d->__nextchar += strlen (d->__nextchar);
624
d->optopt = pfound->val;
628
else if (pfound->has_arg == 1)
630
if (d->optind < argc)
631
d->optarg = argv[d->optind++];
636
#if defined _LIBC && defined USE_IN_LIBIO
639
if (__asprintf (&buf, _("\
640
%s: option '%s' requires an argument\n"),
641
argv[0], argv[d->optind - 1]) >= 0)
643
_IO_flockfile (stderr);
645
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
646
((_IO_FILE *) stderr)->_flags2
647
|= _IO_FLAGS2_NOTCANCEL;
649
__fxprintf (NULL, "%s", buf);
651
((_IO_FILE *) stderr)->_flags2 = old_flags2;
652
_IO_funlockfile (stderr);
658
_("%s: option '%s' requires an argument\n"),
659
argv[0], argv[d->optind - 1]);
662
d->__nextchar += strlen (d->__nextchar);
663
d->optopt = pfound->val;
664
return optstring[0] == ':' ? ':' : '?';
667
d->__nextchar += strlen (d->__nextchar);
669
*longind = option_index;
672
*(pfound->flag) = pfound->val;
759
678
/* Can't find it as a long option. If this is not getopt_long_only,
760
or the option starts with '--' or is not a valid short
761
option, then it's an error.
762
Otherwise interpret it as a short option. */
763
if (!long_only || argv[optind][1] == '-'
764
|| my_index (optstring, *nextchar) == NULL)
768
if (argv[optind][1] == '-')
770
fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
773
/* +option or -option */
774
fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
775
argv[0], argv[optind][0], nextchar);
777
nextchar = (char *) "";
679
or the option starts with '--' or is not a valid short
680
option, then it's an error.
681
Otherwise interpret it as a short option. */
682
if (!long_only || argv[d->optind][1] == '-'
683
|| strchr (optstring, *d->__nextchar) == NULL)
687
#if defined _LIBC && defined USE_IN_LIBIO
692
if (argv[d->optind][1] == '-')
695
#if defined _LIBC && defined USE_IN_LIBIO
696
n = __asprintf (&buf, _("%s: unrecognized option '--%s'\n"),
697
argv[0], d->__nextchar);
699
fprintf (stderr, _("%s: unrecognized option '--%s'\n"),
700
argv[0], d->__nextchar);
705
/* +option or -option */
706
#if defined _LIBC && defined USE_IN_LIBIO
707
n = __asprintf (&buf, _("%s: unrecognized option '%c%s'\n"),
708
argv[0], argv[d->optind][0], d->__nextchar);
710
fprintf (stderr, _("%s: unrecognized option '%c%s'\n"),
711
argv[0], argv[d->optind][0], d->__nextchar);
715
#if defined _LIBC && defined USE_IN_LIBIO
718
_IO_flockfile (stderr);
720
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
721
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
723
__fxprintf (NULL, "%s", buf);
725
((_IO_FILE *) stderr)->_flags2 = old_flags2;
726
_IO_funlockfile (stderr);
732
d->__nextchar = (char *) "";
784
739
/* Look at and handle the next short option-character. */
787
char c = *nextchar++;
788
char *temp = my_index (optstring, c);
742
char c = *d->__nextchar++;
743
char *temp = strchr (optstring, c);
790
745
/* Increment `optind' when we start to process its last character. */
791
if (*nextchar == '\0')
746
if (*d->__nextchar == '\0')
794
749
if (temp == NULL || c == ':')
799
/* 1003.2 specifies the format of this message. */
800
fprintf (stderr, _("%s: illegal option -- %c\n"),
803
fprintf (stderr, _("%s: invalid option -- %c\n"),
753
#if defined _LIBC && defined USE_IN_LIBIO
758
#if defined _LIBC && defined USE_IN_LIBIO
759
n = __asprintf (&buf, _("%s: invalid option -- '%c'\n"),
762
fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
765
#if defined _LIBC && defined USE_IN_LIBIO
768
_IO_flockfile (stderr);
770
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
771
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
773
__fxprintf (NULL, "%s", buf);
775
((_IO_FILE *) stderr)->_flags2 = old_flags2;
776
_IO_funlockfile (stderr);
809
785
/* Convenience. Treat POSIX -W foo same as long option --foo */
810
786
if (temp[0] == 'W' && temp[1] == ';')
813
const struct option *p;
814
const struct option *pfound = NULL;
820
/* This is an option that requires an argument. */
821
if (*nextchar != '\0')
824
/* If we end this ARGV-element by taking the rest as an arg,
825
we must advance to the next element now. */
828
else if (optind == argc)
832
/* 1003.2 specifies the format of this message. */
833
fprintf (stderr, _("%s: option requires an argument -- %c\n"),
837
if (optstring[0] == ':')
844
/* We already incremented `optind' once;
845
increment it again when taking next ARGV-elt as argument. */
846
optarg = argv[optind++];
848
/* optarg is now the argument, see if it's in the
849
table of longopts. */
851
for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
854
/* Test all long options for either exact match
855
or abbreviated matches. */
856
for (p = longopts, option_index = 0; p->name; p++, option_index++)
857
if (!strncmp (p->name, nextchar, nameend - nextchar))
859
if ((unsigned int) (nameend - nextchar) == strlen (p->name))
861
/* Exact match found. */
863
indfound = option_index;
867
else if (pfound == NULL)
869
/* First nonexact match found. */
871
indfound = option_index;
874
/* Second or later nonexact match found. */
880
fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
881
argv[0], argv[optind]);
882
nextchar += strlen (nextchar);
888
option_index = indfound;
891
/* Don't test has_arg with >, because some C compilers don't
892
allow it to be used on enums. */
894
optarg = nameend + 1;
898
fprintf (stderr, _("\
899
%s: option `-W %s' doesn't allow an argument\n"),
900
argv[0], pfound->name);
902
nextchar += strlen (nextchar);
906
else if (pfound->has_arg == 1)
909
optarg = argv[optind++];
914
_("%s: option `%s' requires an argument\n"),
915
argv[0], argv[optind - 1]);
916
nextchar += strlen (nextchar);
917
return optstring[0] == ':' ? ':' : '?';
920
nextchar += strlen (nextchar);
922
*longind = option_index;
925
*(pfound->flag) = pfound->val;
931
return 'W'; /* Let the application handle it. */
789
const struct option *p;
790
const struct option *pfound = NULL;
796
/* This is an option that requires an argument. */
797
if (*d->__nextchar != '\0')
799
d->optarg = d->__nextchar;
800
/* If we end this ARGV-element by taking the rest as an arg,
801
we must advance to the next element now. */
804
else if (d->optind == argc)
808
#if defined _LIBC && defined USE_IN_LIBIO
811
if (__asprintf (&buf,
812
_("%s: option requires an argument -- '%c'\n"),
815
_IO_flockfile (stderr);
817
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
818
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
820
__fxprintf (NULL, "%s", buf);
822
((_IO_FILE *) stderr)->_flags2 = old_flags2;
823
_IO_funlockfile (stderr);
829
_("%s: option requires an argument -- '%c'\n"),
834
if (optstring[0] == ':')
841
/* We already incremented `d->optind' once;
842
increment it again when taking next ARGV-elt as argument. */
843
d->optarg = argv[d->optind++];
845
/* optarg is now the argument, see if it's in the
846
table of longopts. */
848
for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
852
/* Test all long options for either exact match
853
or abbreviated matches. */
854
for (p = longopts, option_index = 0; p->name; p++, option_index++)
855
if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
857
if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
859
/* Exact match found. */
861
indfound = option_index;
865
else if (pfound == NULL)
867
/* First nonexact match found. */
869
indfound = option_index;
872
/* Second or later nonexact match found. */
879
#if defined _LIBC && defined USE_IN_LIBIO
882
if (__asprintf (&buf, _("%s: option '-W %s' is ambiguous\n"),
883
argv[0], argv[d->optind]) >= 0)
885
_IO_flockfile (stderr);
887
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
888
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
890
__fxprintf (NULL, "%s", buf);
892
((_IO_FILE *) stderr)->_flags2 = old_flags2;
893
_IO_funlockfile (stderr);
898
fprintf (stderr, _("%s: option '-W %s' is ambiguous\n"),
899
argv[0], argv[d->optind]);
902
d->__nextchar += strlen (d->__nextchar);
908
option_index = indfound;
911
/* Don't test has_arg with >, because some C compilers don't
912
allow it to be used on enums. */
914
d->optarg = nameend + 1;
919
#if defined _LIBC && defined USE_IN_LIBIO
922
if (__asprintf (&buf, _("\
923
%s: option '-W %s' doesn't allow an argument\n"),
924
argv[0], pfound->name) >= 0)
926
_IO_flockfile (stderr);
928
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
929
((_IO_FILE *) stderr)->_flags2
930
|= _IO_FLAGS2_NOTCANCEL;
932
__fxprintf (NULL, "%s", buf);
934
((_IO_FILE *) stderr)->_flags2 = old_flags2;
935
_IO_funlockfile (stderr);
940
fprintf (stderr, _("\
941
%s: option '-W %s' doesn't allow an argument\n"),
942
argv[0], pfound->name);
946
d->__nextchar += strlen (d->__nextchar);
950
else if (pfound->has_arg == 1)
952
if (d->optind < argc)
953
d->optarg = argv[d->optind++];
958
#if defined _LIBC && defined USE_IN_LIBIO
961
if (__asprintf (&buf, _("\
962
%s: option '%s' requires an argument\n"),
963
argv[0], argv[d->optind - 1]) >= 0)
965
_IO_flockfile (stderr);
967
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
968
((_IO_FILE *) stderr)->_flags2
969
|= _IO_FLAGS2_NOTCANCEL;
971
__fxprintf (NULL, "%s", buf);
973
((_IO_FILE *) stderr)->_flags2 = old_flags2;
974
_IO_funlockfile (stderr);
980
_("%s: option '%s' requires an argument\n"),
981
argv[0], argv[d->optind - 1]);
984
d->__nextchar += strlen (d->__nextchar);
985
return optstring[0] == ':' ? ':' : '?';
988
d->__nextchar += strlen (d->__nextchar);
990
*longind = option_index;
993
*(pfound->flag) = pfound->val;
998
d->__nextchar = NULL;
999
return 'W'; /* Let the application handle it. */
933
1001
if (temp[1] == ':')
937
/* This is an option that accepts an argument optionally. */
938
if (*nextchar != '\0')
949
/* This is an option that requires an argument. */
950
if (*nextchar != '\0')
953
/* If we end this ARGV-element by taking the rest as an arg,
954
we must advance to the next element now. */
957
else if (optind == argc)
961
/* 1003.2 specifies the format of this message. */
963
_("%s: option requires an argument -- %c\n"),
967
if (optstring[0] == ':')
973
/* We already incremented `optind' once;
974
increment it again when taking next ARGV-elt as argument. */
975
optarg = argv[optind++];
1005
/* This is an option that accepts an argument optionally. */
1006
if (*d->__nextchar != '\0')
1008
d->optarg = d->__nextchar;
1013
d->__nextchar = NULL;
1017
/* This is an option that requires an argument. */
1018
if (*d->__nextchar != '\0')
1020
d->optarg = d->__nextchar;
1021
/* If we end this ARGV-element by taking the rest as an arg,
1022
we must advance to the next element now. */
1025
else if (d->optind == argc)
1029
#if defined _LIBC && defined USE_IN_LIBIO
1032
if (__asprintf (&buf, _("\
1033
%s: option requires an argument -- '%c'\n"),
1036
_IO_flockfile (stderr);
1038
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1039
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1041
__fxprintf (NULL, "%s", buf);
1043
((_IO_FILE *) stderr)->_flags2 = old_flags2;
1044
_IO_funlockfile (stderr);
1050
_("%s: option requires an argument -- '%c'\n"),
1055
if (optstring[0] == ':')
1061
/* We already incremented `optind' once;
1062
increment it again when taking next ARGV-elt as argument. */
1063
d->optarg = argv[d->optind++];
1064
d->__nextchar = NULL;
984
getopt (argc, argv, optstring)
987
const char *optstring;
1072
_getopt_internal (int argc, char **argv, const char *optstring,
1073
const struct option *longopts, int *longind, int long_only,
1074
int posixly_correct)
1078
getopt_data.optind = optind;
1079
getopt_data.opterr = opterr;
1081
result = _getopt_internal_r (argc, argv, optstring, longopts,
1082
longind, long_only, &getopt_data,
1085
optind = getopt_data.optind;
1086
optarg = getopt_data.optarg;
1087
optopt = getopt_data.optopt;
1092
/* glibc gets a LSB-compliant getopt.
1093
Standalone applications get a POSIX-compliant getopt. */
1095
enum { POSIXLY_CORRECT = 0 };
1097
enum { POSIXLY_CORRECT = 1 };
1101
getopt (int argc, char *const *argv, const char *optstring)
1103
return _getopt_internal (argc, (char **) argv, optstring,
1104
(const struct option *) 0,
1106
0, POSIXLY_CORRECT);
1111
__posix_getopt (int argc, char *const *argv, const char *optstring)
989
1113
return _getopt_internal (argc, argv, optstring,
990
(const struct option *) 0,
1114
(const struct option *) 0,
995
#endif /* Not ELIDE_CODE. */