2
2
NOTE: getopt is now part of the C library, so if you don't know what
3
3
"Keep this file name-space clean" means, talk to drepper@gnu.org
5
Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002
6
Free Software Foundation, Inc.
5
Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004
6
Free Software Foundation, Inc.
7
7
This file is part of the GNU C Library.
9
9
This program is free software; you can redistribute it and/or modify
19
19
You should have received a copy of the GNU General Public License along
20
20
with this program; if not, write to the Free Software Foundation,
21
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23
23
/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
24
24
Ditto for AIX 3.2 and <stdlib.h>. */
30
30
# include <config.h>
33
#if !defined __STDC__ || !__STDC__
34
/* This is a separate conditional since some stdc systems
35
reject `defined (const)'. */
43
/* Comment out all this code if we are using the GNU C Library, and are not
44
actually compiling the library itself. This code is part of the GNU C
45
Library, but also included in many other GNU distributions. Compiling
46
and linking in this code is a waste when using the GNU C library
47
(especially if it is a shared library). Rather than having every GNU
48
program understand `configure --with-gnu-libc' and omit the object files,
49
it is simpler to just do this in the source for each such file. */
51
#define GETOPT_INTERFACE_VERSION 2
52
#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
53
# include <gnu-versions.h>
54
# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
62
35
/* This needs to come after some library #include
63
36
to get __GNU_LIBRARY__ defined. */
64
37
#ifdef __GNU_LIBRARY__
91
62
# define attribute_hidden
94
/* This version of `getopt' appears to the caller like standard Unix `getopt'
95
but it behaves differently for the user, since it allows the user
96
to intersperse the options with the other arguments.
65
/* Unlike standard Unix `getopt', functions like `getopt_long'
66
let the user intersperse the options with the other arguments.
98
As `getopt' works, it permutes the elements of ARGV so that,
68
As `getopt_long' works, it permutes the elements of ARGV so that,
99
69
when it is done, all the options precede everything else. Thus
100
70
all application programs are extended to handle flexible argument order.
102
Setting the environment variable POSIXLY_CORRECT disables permutation.
103
Then the behavior is completely standard.
72
Using `getopt' or setting the environment variable POSIXLY_CORRECT
74
Then the application's behavior is completely standard.
105
76
GNU application programs can use a third alternative mode in which
106
77
they can distinguish the relative order of options and other arguments. */
108
79
#include "getopt.h"
80
#include "getopt_int.h"
110
82
/* For communication from `getopt' to the caller.
111
83
When `getopt' finds an option that takes an argument,
130
102
/* 1003.2 says this must be 1 before any call. */
133
/* Formerly, initialization of getopt depended on optind==0, which
134
causes problems with re-calling getopt as programs generally don't
137
int __getopt_initialized attribute_hidden;
139
/* The next char to be scanned in the option-element
140
in which the last option character we returned was found.
141
This allows us to pick up the scan where we left off.
143
If this is zero, or a null string, it means resume the scan
144
by advancing to the next ARGV-element. */
146
static char *nextchar;
148
105
/* Callers store zero here to inhibit the error message
149
106
for unrecognized options. */
157
114
int optopt = '?';
159
/* Describe how to deal with options that follow non-option ARGV-elements.
161
If the caller did not specify anything,
162
the default is REQUIRE_ORDER if the environment variable
163
POSIXLY_CORRECT is defined, PERMUTE otherwise.
165
REQUIRE_ORDER means don't recognize them as options;
166
stop option processing when the first non-option is seen.
167
This is what Unix does.
168
This mode of operation is selected by either setting the environment
169
variable POSIXLY_CORRECT, or using `+' as the first character
170
of the list of option characters.
172
PERMUTE is the default. We permute the contents of ARGV as we scan,
173
so that eventually all the non-options are at the end. This allows options
174
to be given in any order, even with programs that were not written to
177
RETURN_IN_ORDER is an option available to programs that were written
178
to expect options and other ARGV-elements in any order and that care about
179
the ordering of the two. We describe each non-option ARGV-element
180
as if it were the argument of an option with character code 1.
181
Using `-' as the first character of the list of option characters
182
selects this mode of operation.
184
The special argument `--' forces an end of option-scanning regardless
185
of the value of `ordering'. In the case of RETURN_IN_ORDER, only
186
`--' can cause `getopt' to return -1 with `optind' != ARGC. */
190
REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
193
/* Value of POSIXLY_CORRECT environment variable. */
194
static char *posixly_correct;
116
/* Keep a global copy of all internal members of getopt_data. */
118
static struct _getopt_data getopt_data;
196
#ifdef __GNU_LIBRARY__
197
/* We want to avoid inclusion of string.h with non-GNU libraries
198
because there are many ways it can cause trouble.
199
On some systems, it contains special magic macros that don't work
202
# define my_index strchr
208
# include <strings.h>
121
#ifndef __GNU_LIBRARY__
211
123
/* Avoid depending on library functions or files
212
124
whose names are inconsistent. */
215
127
extern char *getenv ();
232
/* If using GCC, we can safely declare strlen this way.
233
If not using GCC, it is ok not to declare it. */
235
/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
236
That was relevant to code that was here before. */
237
# if (!defined __STDC__ || !__STDC__) && !defined strlen
238
/* gcc with -traditional declares the built-in strlen to return int,
239
and has done so at least since version 2.4.5. -- rms. */
240
extern int strlen (const char *);
241
# endif /* not __STDC__ */
242
#endif /* __GNUC__ */
244
130
#endif /* not __GNU_LIBRARY__ */
246
/* Handle permutation of arguments. */
248
/* Describe the part of ARGV that contains non-options that have
249
been skipped. `first_nonopt' is the index in ARGV of the first of them;
250
`last_nonopt' is the index after the last of them. */
252
static int first_nonopt;
253
static int last_nonopt;
256
133
/* Stored original parameters.
257
134
XXX This is no good solution. We should rather copy the args so
265
142
# ifdef USE_NONOPTION_FLAGS
266
143
/* Defined in getopt_init.c */
267
144
extern char *__getopt_nonoption_flags;
269
static int nonoption_flags_max_len;
270
static int nonoption_flags_len;
273
147
# ifdef USE_NONOPTION_FLAGS
274
148
# define SWAP_FLAGS(ch1, ch2) \
275
if (nonoption_flags_len > 0) \
149
if (d->__nonoption_flags_len > 0) \
277
151
char __tmp = __getopt_nonoption_flags[ch1]; \
278
152
__getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
294
168
`first_nonopt' and `last_nonopt' are relocated so that they describe
295
169
the new indices of the non-options in ARGV after they are moved. */
297
#if defined __STDC__ && __STDC__
298
static void exchange (char **);
172
exchange (char **argv, struct _getopt_data *d)
305
int bottom = first_nonopt;
306
int middle = last_nonopt;
174
int bottom = d->__first_nonopt;
175
int middle = d->__last_nonopt;
310
179
/* Exchange the shorter segment with the far end of the longer segment.
316
185
/* First make sure the handling of the `__getopt_nonoption_flags'
317
186
string can work normally. Our top argument must be in the range
318
187
of the string. */
319
if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
188
if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
321
190
/* We must extend the array. The user plays games with us and
322
191
presents new arguments. */
323
192
char *new_str = malloc (top + 1);
324
193
if (new_str == NULL)
325
nonoption_flags_len = nonoption_flags_max_len = 0;
194
d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
328
197
memset (__mempcpy (new_str, __getopt_nonoption_flags,
329
nonoption_flags_max_len),
330
'\0', top + 1 - nonoption_flags_max_len);
331
nonoption_flags_max_len = top + 1;
198
d->__nonoption_flags_max_len),
199
'\0', top + 1 - d->__nonoption_flags_max_len);
200
d->__nonoption_flags_max_len = top + 1;
332
201
__getopt_nonoption_flags = new_str;
375
244
/* Update records for the slots the non-options now occupy. */
377
first_nonopt += (optind - last_nonopt);
378
last_nonopt = optind;
246
d->__first_nonopt += (d->optind - d->__last_nonopt);
247
d->__last_nonopt = d->optind;
381
250
/* Initialize the internal data when the first call is made. */
383
#if defined __STDC__ && __STDC__
384
static const char *_getopt_initialize (int, char *const *, const char *);
386
252
static const char *
387
_getopt_initialize (argc, argv, optstring)
390
const char *optstring;
253
_getopt_initialize (int argc, char **argv, const char *optstring,
254
int posixly_correct, struct _getopt_data *d)
392
256
/* Start processing options with ARGV-element 1 (since ARGV-element 0
393
257
is the program name); the sequence of previously skipped
394
258
non-option ARGV-elements is empty. */
396
first_nonopt = last_nonopt = optind;
400
posixly_correct = getenv ("POSIXLY_CORRECT");
260
d->__first_nonopt = d->__last_nonopt = d->optind;
262
d->__nextchar = NULL;
264
d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
402
266
/* Determine how to handle the ordering of options and nonoptions. */
404
268
if (optstring[0] == '-')
406
ordering = RETURN_IN_ORDER;
270
d->__ordering = RETURN_IN_ORDER;
409
273
else if (optstring[0] == '+')
411
ordering = REQUIRE_ORDER;
275
d->__ordering = REQUIRE_ORDER;
414
else if (posixly_correct != NULL)
415
ordering = REQUIRE_ORDER;
278
else if (d->__posixly_correct)
279
d->__ordering = REQUIRE_ORDER;
281
d->__ordering = PERMUTE;
419
283
#if defined _LIBC && defined USE_NONOPTION_FLAGS
420
if (posixly_correct == NULL
284
if (!d->__posixly_correct
421
285
&& argc == __libc_argc && argv == __libc_argv)
423
if (nonoption_flags_max_len == 0)
287
if (d->__nonoption_flags_max_len == 0)
425
289
if (__getopt_nonoption_flags == NULL
426
290
|| __getopt_nonoption_flags[0] == '\0')
427
nonoption_flags_max_len = -1;
291
d->__nonoption_flags_max_len = -1;
430
294
const char *orig_str = __getopt_nonoption_flags;
431
int len = nonoption_flags_max_len = strlen (orig_str);
432
if (nonoption_flags_max_len < argc)
433
nonoption_flags_max_len = argc;
295
int len = d->__nonoption_flags_max_len = strlen (orig_str);
296
if (d->__nonoption_flags_max_len < argc)
297
d->__nonoption_flags_max_len = argc;
434
298
__getopt_nonoption_flags =
435
(char *) malloc (nonoption_flags_max_len);
299
(char *) malloc (d->__nonoption_flags_max_len);
436
300
if (__getopt_nonoption_flags == NULL)
437
nonoption_flags_max_len = -1;
301
d->__nonoption_flags_max_len = -1;
439
303
memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
440
'\0', nonoption_flags_max_len - len);
304
'\0', d->__nonoption_flags_max_len - len);
443
nonoption_flags_len = nonoption_flags_max_len;
307
d->__nonoption_flags_len = d->__nonoption_flags_max_len;
446
nonoption_flags_len = 0;
310
d->__nonoption_flags_len = 0;
449
313
return optstring;
505
365
If LONG_ONLY is nonzero, '-' as well as '--' can introduce
506
long-named options. */
368
If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
369
environment variable were set. */
509
_getopt_internal (argc, argv, optstring, longopts, longind, long_only)
512
const char *optstring;
513
const struct option *longopts;
372
_getopt_internal_r (int argc, char **argv, const char *optstring,
373
const struct option *longopts, int *longind,
374
int long_only, int posixly_correct, struct _getopt_data *d)
517
int print_errors = opterr;
376
int print_errors = d->opterr;
518
377
if (optstring[0] == ':')
519
378
print_errors = 0;
526
if (optind == 0 || !__getopt_initialized)
385
if (d->optind == 0 || !d->__initialized)
529
optind = 1; /* Don't scan ARGV[0], the program name. */
530
optstring = _getopt_initialize (argc, argv, optstring);
531
__getopt_initialized = 1;
388
d->optind = 1; /* Don't scan ARGV[0], the program name. */
389
optstring = _getopt_initialize (argc, argv, optstring,
391
d->__initialized = 1;
534
394
/* Test whether ARGV[optind] points to a non-option argument.
536
396
from the shell indicating it is not an option. The later information
537
397
is only used when the used in the GNU libc. */
538
398
#if defined _LIBC && defined USE_NONOPTION_FLAGS
539
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
540
|| (optind < nonoption_flags_len \
541
&& __getopt_nonoption_flags[optind] == '1'))
399
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
400
|| (d->optind < d->__nonoption_flags_len \
401
&& __getopt_nonoption_flags[d->optind] == '1'))
543
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
403
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
546
if (nextchar == NULL || *nextchar == '\0')
406
if (d->__nextchar == NULL || *d->__nextchar == '\0')
548
408
/* Advance to the next ARGV-element. */
550
410
/* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
551
411
moved back by the user (who may also have changed the arguments). */
552
if (last_nonopt > optind)
553
last_nonopt = optind;
554
if (first_nonopt > optind)
555
first_nonopt = optind;
412
if (d->__last_nonopt > d->optind)
413
d->__last_nonopt = d->optind;
414
if (d->__first_nonopt > d->optind)
415
d->__first_nonopt = d->optind;
557
if (ordering == PERMUTE)
417
if (d->__ordering == PERMUTE)
559
419
/* If we have just processed some options following some non-options,
560
420
exchange them so that the options come first. */
562
if (first_nonopt != last_nonopt && last_nonopt != optind)
563
exchange ((char **) argv);
564
else if (last_nonopt != optind)
565
first_nonopt = optind;
422
if (d->__first_nonopt != d->__last_nonopt
423
&& d->__last_nonopt != d->optind)
424
exchange ((char **) argv, d);
425
else if (d->__last_nonopt != d->optind)
426
d->__first_nonopt = d->optind;
567
428
/* Skip any additional non-options
568
429
and extend the range of non-options previously skipped. */
570
while (optind < argc && NONOPTION_P)
572
last_nonopt = optind;
431
while (d->optind < argc && NONOPTION_P)
433
d->__last_nonopt = d->optind;
575
436
/* The special ARGV-element `--' means premature end of options.
577
438
then exchange with previous non-options as if it were an option,
578
439
then skip everything else like a non-option. */
580
if (optind != argc && !strcmp (argv[optind], "--"))
441
if (d->optind != argc && !strcmp (argv[d->optind], "--"))
584
if (first_nonopt != last_nonopt && last_nonopt != optind)
585
exchange ((char **) argv);
586
else if (first_nonopt == last_nonopt)
587
first_nonopt = optind;
445
if (d->__first_nonopt != d->__last_nonopt
446
&& d->__last_nonopt != d->optind)
447
exchange ((char **) argv, d);
448
else if (d->__first_nonopt == d->__last_nonopt)
449
d->__first_nonopt = d->optind;
450
d->__last_nonopt = argc;
593
455
/* If we have done all the ARGV-elements, stop the scan
594
456
and back over any non-options that we skipped and permuted. */
458
if (d->optind == argc)
598
460
/* Set the next-arg-index to point at the non-options
599
461
that we previously skipped, so the caller will digest them. */
600
if (first_nonopt != last_nonopt)
601
optind = first_nonopt;
462
if (d->__first_nonopt != d->__last_nonopt)
463
d->optind = d->__first_nonopt;
610
if (ordering == REQUIRE_ORDER)
472
if (d->__ordering == REQUIRE_ORDER)
612
optarg = argv[optind++];
474
d->optarg = argv[d->optind++];
616
478
/* We have found another option-ARGV-element.
617
479
Skip the initial punctuation. */
619
nextchar = (argv[optind] + 1
620
+ (longopts != NULL && argv[optind][1] == '-'));
481
d->__nextchar = (argv[d->optind] + 1
482
+ (longopts != NULL && argv[d->optind][1] == '-'));
623
485
/* Decode the current option-ARGV-element. */
647
510
int indfound = -1;
648
511
int option_index;
650
for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
513
for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
651
514
/* Do nothing. */ ;
653
516
/* Test all long options for either exact match
654
517
or abbreviated matches. */
655
518
for (p = longopts, option_index = 0; p->name; p++, option_index++)
656
if (!strncmp (p->name, nextchar, nameend - nextchar))
519
if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
658
if ((unsigned int) (nameend - nextchar)
521
if ((unsigned int) (nameend - d->__nextchar)
659
522
== (unsigned int) strlen (p->name))
661
524
/* Exact match found. */
688
551
if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
689
argv[0], argv[optind]) >= 0)
552
argv[0], argv[d->optind]) >= 0)
554
_IO_flockfile (stderr);
556
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
557
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
692
559
if (_IO_fwide (stderr, 0) > 0)
693
560
__fwprintf (stderr, L"%s", buf);
695
562
fputs (buf, stderr);
564
((_IO_FILE *) stderr)->_flags2 = old_flags2;
565
_IO_funlockfile (stderr);
700
570
fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
701
argv[0], argv[optind]);
571
argv[0], argv[d->optind]);
704
nextchar += strlen (nextchar);
574
d->__nextchar += strlen (d->__nextchar);
710
580
if (pfound != NULL)
712
582
option_index = indfound;
716
586
/* Don't test has_arg with >, because some C compilers don't
717
587
allow it to be used on enums. */
718
588
if (pfound->has_arg)
719
optarg = nameend + 1;
589
d->optarg = nameend + 1;
722
592
if (print_errors)
745
615
#if defined _LIBC && defined USE_IN_LIBIO
746
616
n = __asprintf (&buf, _("\
747
617
%s: option `%c%s' doesn't allow an argument\n"),
748
argv[0], argv[optind - 1][0],
618
argv[0], argv[d->optind - 1][0],
751
621
fprintf (stderr, _("\
752
622
%s: option `%c%s' doesn't allow an argument\n"),
753
argv[0], argv[optind - 1][0], pfound->name);
623
argv[0], argv[d->optind - 1][0],
757
628
#if defined _LIBC && defined USE_IN_LIBIO
631
_IO_flockfile (stderr);
633
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
634
((_IO_FILE *) stderr)->_flags2
635
|= _IO_FLAGS2_NOTCANCEL;
760
637
if (_IO_fwide (stderr, 0) > 0)
761
638
__fwprintf (stderr, L"%s", buf);
763
640
fputs (buf, stderr);
642
((_IO_FILE *) stderr)->_flags2 = old_flags2;
643
_IO_funlockfile (stderr);
770
nextchar += strlen (nextchar);
650
d->__nextchar += strlen (d->__nextchar);
772
optopt = pfound->val;
652
d->optopt = pfound->val;
776
656
else if (pfound->has_arg == 1)
779
optarg = argv[optind++];
658
if (d->optind < argc)
659
d->optarg = argv[d->optind++];
782
662
if (print_errors)
787
667
if (__asprintf (&buf, _("\
788
668
%s: option `%s' requires an argument\n"),
789
argv[0], argv[optind - 1]) >= 0)
669
argv[0], argv[d->optind - 1]) >= 0)
671
_IO_flockfile (stderr);
673
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
674
((_IO_FILE *) stderr)->_flags2
675
|= _IO_FLAGS2_NOTCANCEL;
791
677
if (_IO_fwide (stderr, 0) > 0)
792
678
__fwprintf (stderr, L"%s", buf);
794
680
fputs (buf, stderr);
682
((_IO_FILE *) stderr)->_flags2 = old_flags2;
683
_IO_funlockfile (stderr);
800
689
_("%s: option `%s' requires an argument\n"),
801
argv[0], argv[optind - 1]);
690
argv[0], argv[d->optind - 1]);
804
nextchar += strlen (nextchar);
805
optopt = pfound->val;
693
d->__nextchar += strlen (d->__nextchar);
694
d->optopt = pfound->val;
806
695
return optstring[0] == ':' ? ':' : '?';
809
nextchar += strlen (nextchar);
698
d->__nextchar += strlen (d->__nextchar);
810
699
if (longind != NULL)
811
700
*longind = option_index;
812
701
if (pfound->flag)
847
736
/* +option or -option */
848
737
#if defined _LIBC && defined USE_IN_LIBIO
849
738
n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
850
argv[0], argv[optind][0], nextchar);
739
argv[0], argv[d->optind][0], d->__nextchar);
852
741
fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
853
argv[0], argv[optind][0], nextchar);
742
argv[0], argv[d->optind][0], d->__nextchar);
857
746
#if defined _LIBC && defined USE_IN_LIBIO
749
_IO_flockfile (stderr);
751
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
752
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
860
754
if (_IO_fwide (stderr, 0) > 0)
861
755
__fwprintf (stderr, L"%s", buf);
863
757
fputs (buf, stderr);
759
((_IO_FILE *) stderr)->_flags2 = old_flags2;
760
_IO_funlockfile (stderr);
869
nextchar = (char *) "";
766
d->__nextchar = (char *) "";
915
812
#if defined _LIBC && defined USE_IN_LIBIO
815
_IO_flockfile (stderr);
817
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
818
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
918
820
if (_IO_fwide (stderr, 0) > 0)
919
821
__fwprintf (stderr, L"%s", buf);
921
823
fputs (buf, stderr);
825
((_IO_FILE *) stderr)->_flags2 = old_flags2;
826
_IO_funlockfile (stderr);
930
835
/* Convenience. Treat POSIX -W foo same as long option --foo */
981
/* We already incremented `optind' once;
894
/* We already incremented `d->optind' once;
982
895
increment it again when taking next ARGV-elt as argument. */
983
optarg = argv[optind++];
896
d->optarg = argv[d->optind++];
985
898
/* optarg is now the argument, see if it's in the
986
899
table of longopts. */
988
for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
901
for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
989
903
/* Do nothing. */ ;
991
905
/* Test all long options for either exact match
992
906
or abbreviated matches. */
993
907
for (p = longopts, option_index = 0; p->name; p++, option_index++)
994
if (!strncmp (p->name, nextchar, nameend - nextchar))
908
if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
996
if ((unsigned int) (nameend - nextchar) == strlen (p->name))
910
if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
998
912
/* Exact match found. */
1021
935
if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
1022
argv[0], argv[optind]) >= 0)
936
argv[0], argv[d->optind]) >= 0)
938
_IO_flockfile (stderr);
940
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
941
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1024
943
if (_IO_fwide (stderr, 0) > 0)
1025
944
__fwprintf (stderr, L"%s", buf);
1027
946
fputs (buf, stderr);
948
((_IO_FILE *) stderr)->_flags2 = old_flags2;
949
_IO_funlockfile (stderr);
1032
954
fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
1033
argv[0], argv[optind]);
955
argv[0], argv[d->optind]);
1036
nextchar += strlen (nextchar);
958
d->__nextchar += strlen (d->__nextchar);
1040
962
if (pfound != NULL)
1057
979
%s: option `-W %s' doesn't allow an argument\n"),
1058
980
argv[0], pfound->name) >= 0)
982
_IO_flockfile (stderr);
984
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
985
((_IO_FILE *) stderr)->_flags2
986
|= _IO_FLAGS2_NOTCANCEL;
1060
988
if (_IO_fwide (stderr, 0) > 0)
1061
989
__fwprintf (stderr, L"%s", buf);
1063
991
fputs (buf, stderr);
993
((_IO_FILE *) stderr)->_flags2 = old_flags2;
994
_IO_funlockfile (stderr);
1089
1020
if (__asprintf (&buf, _("\
1090
1021
%s: option `%s' requires an argument\n"),
1091
argv[0], argv[optind - 1]) >= 0)
1022
argv[0], argv[d->optind - 1]) >= 0)
1024
_IO_flockfile (stderr);
1026
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1027
((_IO_FILE *) stderr)->_flags2
1028
|= _IO_FLAGS2_NOTCANCEL;
1093
1030
if (_IO_fwide (stderr, 0) > 0)
1094
1031
__fwprintf (stderr, L"%s", buf);
1096
1033
fputs (buf, stderr);
1035
((_IO_FILE *) stderr)->_flags2 = old_flags2;
1036
_IO_funlockfile (stderr);
1101
1041
fprintf (stderr,
1102
1042
_("%s: option `%s' requires an argument\n"),
1103
argv[0], argv[optind - 1]);
1043
argv[0], argv[d->optind - 1]);
1106
nextchar += strlen (nextchar);
1046
d->__nextchar += strlen (d->__nextchar);
1107
1047
return optstring[0] == ':' ? ':' : '?';
1110
nextchar += strlen (nextchar);
1050
d->__nextchar += strlen (d->__nextchar);
1111
1051
if (longind != NULL)
1112
1052
*longind = option_index;
1113
1053
if (pfound->flag)
1125
1065
if (temp[2] == ':')
1127
1067
/* This is an option that accepts an argument optionally. */
1128
if (*nextchar != '\0')
1068
if (*d->__nextchar != '\0')
1070
d->optarg = d->__nextchar;
1075
d->__nextchar = NULL;
1139
1079
/* This is an option that requires an argument. */
1140
if (*nextchar != '\0')
1080
if (*d->__nextchar != '\0')
1082
d->optarg = d->__nextchar;
1143
1083
/* If we end this ARGV-element by taking the rest as an arg,
1144
1084
we must advance to the next element now. */
1147
else if (optind == argc)
1087
else if (d->optind == argc)
1149
1089
if (print_errors)
1190
getopt (argc, argv, optstring)
1193
const char *optstring;
1195
return _getopt_internal (argc, argv, optstring,
1196
(const struct option *) 0,
1201
#endif /* Not ELIDE_CODE. */
1138
_getopt_internal (int argc, char **argv, const char *optstring,
1139
const struct option *longopts, int *longind,
1140
int long_only, int posixly_correct)
1144
getopt_data.optind = optind;
1145
getopt_data.opterr = opterr;
1147
result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
1148
long_only, posixly_correct, &getopt_data);
1150
optind = getopt_data.optind;
1151
optarg = getopt_data.optarg;
1152
optopt = getopt_data.optopt;
1157
/* glibc gets a LSB-compliant getopt.
1158
Standalone applications get a POSIX-compliant getopt. */
1160
enum { POSIXLY_CORRECT = 0 };
1162
enum { POSIXLY_CORRECT = 1 };
1166
getopt (int argc, char *const *argv, const char *optstring)
1168
return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,