2
NOTE: The canonical source of this file is maintained with the GNU
3
C Library. Bugs can be reported to bug-glibc@gnu.org.
5
Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
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
5
Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004
6
6
Free Software Foundation, Inc.
7
This file is part of the GNU C Library.
8
This program is free software; you can redistribute it and/or modify it
9
under the terms of the GNU General Public License as published by the
10
Free Software Foundation; either version 2, or (at your option) any
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)
13
14
This program is distributed in the hope that it will be useful,
14
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
17
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software Foundation,
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,
20
21
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
23
/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
28
29
#ifdef HAVE_CONFIG_H
29
30
# include <config.h>
31
# if !defined __STDC__ || !__STDC__
32
/* This is a separate conditional since some stdc systems
33
reject `defined (const)'. */
42
/* Comment out all this code if we are using the GNU C Library, and are not
43
actually compiling the library itself. This code is part of the GNU C
44
Library, but also included in many other GNU distributions. Compiling
45
and linking in this code is a waste when using the GNU C library
46
(especially if it is a shared library). Rather than having every GNU
47
program understand `configure --with-gnu-libc' and omit the object files,
48
it is simpler to just do this in the source for each such file. */
50
#define GETOPT_INTERFACE_VERSION 2
51
#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
52
# include <gnu-versions.h>
53
# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
61
35
/* This needs to come after some library #include
62
36
to get __GNU_LIBRARY__ defined. */
63
37
#ifdef __GNU_LIBRARY__
124
102
/* 1003.2 says this must be 1 before any call. */
127
/* Formerly, initialization of getopt depended on optind==0, which
128
causes problems with re-calling getopt as programs generally don't
131
int __getopt_initialized;
133
/* The next char to be scanned in the option-element
134
in which the last option character we returned was found.
135
This allows us to pick up the scan where we left off.
137
If this is zero, or a null string, it means resume the scan
138
by advancing to the next ARGV-element. */
140
static char *nextchar;
142
105
/* Callers store zero here to inhibit the error message
143
106
for unrecognized options. */
151
114
int optopt = '?';
153
/* Describe how to deal with options that follow non-option ARGV-elements.
155
If the caller did not specify anything,
156
the default is REQUIRE_ORDER if the environment variable
157
POSIXLY_CORRECT is defined, PERMUTE otherwise.
159
REQUIRE_ORDER means don't recognize them as options;
160
stop option processing when the first non-option is seen.
161
This is what Unix does.
162
This mode of operation is selected by either setting the environment
163
variable POSIXLY_CORRECT, or using `+' as the first character
164
of the list of option characters.
166
PERMUTE is the default. We permute the contents of ARGV as we scan,
167
so that eventually all the non-options are at the end. This allows options
168
to be given in any order, even with programs that were not written to
171
RETURN_IN_ORDER is an option available to programs that were written
172
to expect options and other ARGV-elements in any order and that care about
173
the ordering of the two. We describe each non-option ARGV-element
174
as if it were the argument of an option with character code 1.
175
Using `-' as the first character of the list of option characters
176
selects this mode of operation.
178
The special argument `--' forces an end of option-scanning regardless
179
of the value of `ordering'. In the case of RETURN_IN_ORDER, only
180
`--' can cause `getopt' to return -1 with `optind' != ARGC. */
184
REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
187
/* Value of POSIXLY_CORRECT environment variable. */
188
static char *posixly_correct;
116
/* Keep a global copy of all internal members of getopt_data. */
118
static struct _getopt_data getopt_data;
190
#ifdef __GNU_LIBRARY__
191
/* We want to avoid inclusion of string.h with non-GNU libraries
192
because there are many ways it can cause trouble.
193
On some systems, it contains special magic macros that don't work
196
# define my_index strchr
202
# include <strings.h>
121
#ifndef __GNU_LIBRARY__
205
123
/* Avoid depending on library functions or files
206
124
whose names are inconsistent. */
209
127
extern char *getenv ();
226
/* If using GCC, we can safely declare strlen this way.
227
If not using GCC, it is ok not to declare it. */
229
/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
230
That was relevant to code that was here before. */
231
# if (!defined __STDC__ || !__STDC__) && !defined strlen
232
/* gcc with -traditional declares the built-in strlen to return int,
233
and has done so at least since version 2.4.5. -- rms. */
234
extern int strlen (const char *);
235
# endif /* not __STDC__ */
236
#endif /* __GNUC__ */
238
130
#endif /* not __GNU_LIBRARY__ */
240
/* Handle permutation of arguments. */
242
/* Describe the part of ARGV that contains non-options that have
243
been skipped. `first_nonopt' is the index in ARGV of the first of them;
244
`last_nonopt' is the index after the last of them. */
246
static int first_nonopt;
247
static int last_nonopt;
133
/* Stored original parameters.
134
XXX This is no good solution. We should rather copy the args so
135
that we can compare them later. But we must not use malloc(3). */
136
extern int __libc_argc;
137
extern char **__libc_argv;
250
139
/* Bash 2.0 gives us an environment variable containing flags
251
140
indicating ARGV elements that should not be considered arguments. */
142
# ifdef USE_NONOPTION_FLAGS
253
143
/* Defined in getopt_init.c */
254
144
extern char *__getopt_nonoption_flags;
256
static int nonoption_flags_max_len;
257
static int nonoption_flags_len;
259
static int original_argc;
260
static char *const *original_argv;
262
/* Make sure the environment variable bash 2.0 puts in the environment
263
is valid for the getopt call we must make sure that the ARGV passed
264
to getopt is that one passed to the process. */
266
__attribute__ ((unused))
267
store_args_and_env (int argc, char *const *argv)
269
/* XXX This is no good solution. We should rather copy the args so
270
that we can compare them later. But we must not use malloc(3). */
271
original_argc = argc;
272
original_argv = argv;
274
# ifdef text_set_element
275
text_set_element (__libc_subinit, store_args_and_env);
276
# endif /* text_set_element */
278
# define SWAP_FLAGS(ch1, ch2) \
279
if (nonoption_flags_len > 0) \
147
# ifdef USE_NONOPTION_FLAGS
148
# define SWAP_FLAGS(ch1, ch2) \
149
if (d->__nonoption_flags_len > 0) \
281
151
char __tmp = __getopt_nonoption_flags[ch1]; \
282
152
__getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
283
153
__getopt_nonoption_flags[ch2] = __tmp; \
156
# define SWAP_FLAGS(ch1, ch2)
285
158
#else /* !_LIBC */
286
159
# define SWAP_FLAGS(ch1, ch2)
287
160
#endif /* _LIBC */
313
181
It leaves the longer segment in the right place overall,
314
182
but it consists of two parts that need to be swapped next. */
184
#if defined _LIBC && defined USE_NONOPTION_FLAGS
317
185
/* First make sure the handling of the `__getopt_nonoption_flags'
318
186
string can work normally. Our top argument must be in the range
319
187
of the string. */
320
if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
188
if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
322
190
/* We must extend the array. The user plays games with us and
323
191
presents new arguments. */
324
192
char *new_str = malloc (top + 1);
325
193
if (new_str == NULL)
326
nonoption_flags_len = nonoption_flags_max_len = 0;
194
d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
329
197
memset (__mempcpy (new_str, __getopt_nonoption_flags,
330
nonoption_flags_max_len),
331
'\0', top + 1 - nonoption_flags_max_len);
332
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;
333
201
__getopt_nonoption_flags = new_str;
376
244
/* Update records for the slots the non-options now occupy. */
378
first_nonopt += (optind - last_nonopt);
379
last_nonopt = optind;
246
d->__first_nonopt += (d->optind - d->__last_nonopt);
247
d->__last_nonopt = d->optind;
382
250
/* Initialize the internal data when the first call is made. */
384
#if defined __STDC__ && __STDC__
385
static const char *_getopt_initialize (int, char *const *, const char *);
387
252
static const char *
388
_getopt_initialize (argc, argv, optstring)
391
const char *optstring;
253
_getopt_initialize (int argc, char *const *argv, const char *optstring,
254
struct _getopt_data *d)
393
256
/* Start processing options with ARGV-element 1 (since ARGV-element 0
394
257
is the program name); the sequence of previously skipped
395
258
non-option ARGV-elements is empty. */
397
first_nonopt = last_nonopt = optind;
401
posixly_correct = getenv ("POSIXLY_CORRECT");
260
d->__first_nonopt = d->__last_nonopt = d->optind;
262
d->__nextchar = NULL;
264
d->__posixly_correct = !!getenv ("POSIXLY_CORRECT");
403
266
/* Determine how to handle the ordering of options and nonoptions. */
405
268
if (optstring[0] == '-')
407
ordering = RETURN_IN_ORDER;
270
d->__ordering = RETURN_IN_ORDER;
410
273
else if (optstring[0] == '+')
412
ordering = REQUIRE_ORDER;
275
d->__ordering = REQUIRE_ORDER;
415
else if (posixly_correct != NULL)
416
ordering = REQUIRE_ORDER;
278
else if (d->__posixly_correct)
279
d->__ordering = REQUIRE_ORDER;
281
d->__ordering = PERMUTE;
421
if (posixly_correct == NULL
422
&& argc == original_argc && argv == original_argv)
283
#if defined _LIBC && defined USE_NONOPTION_FLAGS
284
if (!d->__posixly_correct
285
&& argc == __libc_argc && argv == __libc_argv)
424
if (nonoption_flags_max_len == 0)
287
if (d->__nonoption_flags_max_len == 0)
426
289
if (__getopt_nonoption_flags == NULL
427
290
|| __getopt_nonoption_flags[0] == '\0')
428
nonoption_flags_max_len = -1;
291
d->__nonoption_flags_max_len = -1;
431
294
const char *orig_str = __getopt_nonoption_flags;
432
int len = nonoption_flags_max_len = strlen (orig_str);
433
if (nonoption_flags_max_len < argc)
434
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;
435
298
__getopt_nonoption_flags =
436
(char *) malloc (nonoption_flags_max_len);
299
(char *) malloc (d->__nonoption_flags_max_len);
437
300
if (__getopt_nonoption_flags == NULL)
438
nonoption_flags_max_len = -1;
301
d->__nonoption_flags_max_len = -1;
440
303
memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
441
'\0', nonoption_flags_max_len - len);
304
'\0', d->__nonoption_flags_max_len - len);
444
nonoption_flags_len = nonoption_flags_max_len;
307
d->__nonoption_flags_len = d->__nonoption_flags_max_len;
447
nonoption_flags_len = 0;
310
d->__nonoption_flags_len = 0;
450
313
return optstring;
507
370
long-named options. */
510
_getopt_internal (argc, argv, optstring, longopts, longind, long_only)
513
const char *optstring;
514
const struct option *longopts;
373
_getopt_internal_r (int argc, char *const *argv, const char *optstring,
374
const struct option *longopts, int *longind,
375
int long_only, struct _getopt_data *d)
520
if (optind == 0 || !__getopt_initialized)
377
int print_errors = d->opterr;
378
if (optstring[0] == ':')
386
if (d->optind == 0 || !d->__initialized)
523
optind = 1; /* Don't scan ARGV[0], the program name. */
524
optstring = _getopt_initialize (argc, argv, optstring);
525
__getopt_initialized = 1;
389
d->optind = 1; /* Don't scan ARGV[0], the program name. */
390
optstring = _getopt_initialize (argc, argv, optstring, d);
391
d->__initialized = 1;
528
394
/* Test whether ARGV[optind] points to a non-option argument.
529
395
Either it does not have option syntax, or there is an environment flag
530
396
from the shell indicating it is not an option. The later information
531
397
is only used when the used in the GNU libc. */
533
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
534
|| (optind < nonoption_flags_len \
535
&& __getopt_nonoption_flags[optind] == '1'))
398
#if defined _LIBC && defined USE_NONOPTION_FLAGS
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'))
537
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
403
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
540
if (nextchar == NULL || *nextchar == '\0')
406
if (d->__nextchar == NULL || *d->__nextchar == '\0')
542
408
/* Advance to the next ARGV-element. */
544
410
/* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
545
411
moved back by the user (who may also have changed the arguments). */
546
if (last_nonopt > optind)
547
last_nonopt = optind;
548
if (first_nonopt > optind)
549
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;
551
if (ordering == PERMUTE)
417
if (d->__ordering == PERMUTE)
553
419
/* If we have just processed some options following some non-options,
554
420
exchange them so that the options come first. */
556
if (first_nonopt != last_nonopt && last_nonopt != optind)
557
exchange ((char **) argv);
558
else if (last_nonopt != optind)
559
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;
561
428
/* Skip any additional non-options
562
429
and extend the range of non-options previously skipped. */
564
while (optind < argc && NONOPTION_P)
566
last_nonopt = optind;
431
while (d->optind < argc && NONOPTION_P)
433
d->__last_nonopt = d->optind;
569
436
/* The special ARGV-element `--' means premature end of options.
571
438
then exchange with previous non-options as if it were an option,
572
439
then skip everything else like a non-option. */
574
if (optind != argc && !strcmp (argv[optind], "--"))
441
if (d->optind != argc && !strcmp (argv[d->optind], "--"))
578
if (first_nonopt != last_nonopt && last_nonopt != optind)
579
exchange ((char **) argv);
580
else if (first_nonopt == last_nonopt)
581
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;
587
455
/* If we have done all the ARGV-elements, stop the scan
588
456
and back over any non-options that we skipped and permuted. */
458
if (d->optind == argc)
592
460
/* Set the next-arg-index to point at the non-options
593
461
that we previously skipped, so the caller will digest them. */
594
if (first_nonopt != last_nonopt)
595
optind = first_nonopt;
462
if (d->__first_nonopt != d->__last_nonopt)
463
d->optind = d->__first_nonopt;
665
534
indfound = option_index;
537
|| pfound->has_arg != p->has_arg
538
|| pfound->flag != p->flag
539
|| pfound->val != p->val)
668
540
/* Second or later nonexact match found. */
672
544
if (ambig && !exact)
675
fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
676
argv[0], argv[optind]);
677
nextchar += strlen (nextchar);
548
#if defined _LIBC && defined USE_IN_LIBIO
551
if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
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;
559
if (_IO_fwide (stderr, 0) > 0)
560
__fwprintf (stderr, L"%s", buf);
564
((_IO_FILE *) stderr)->_flags2 = old_flags2;
565
_IO_funlockfile (stderr);
570
fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
571
argv[0], argv[d->optind]);
574
d->__nextchar += strlen (d->__nextchar);
683
580
if (pfound != NULL)
685
582
option_index = indfound;
689
586
/* Don't test has_arg with >, because some C compilers don't
690
587
allow it to be used on enums. */
691
588
if (pfound->has_arg)
692
optarg = nameend + 1;
589
d->optarg = nameend + 1;
697
if (argv[optind - 1][1] == '-')
700
_("%s: option `--%s' doesn't allow an argument\n"),
701
argv[0], pfound->name);
594
#if defined _LIBC && defined USE_IN_LIBIO
599
if (argv[d->optind - 1][1] == '-')
602
#if defined _LIBC && defined USE_IN_LIBIO
603
n = __asprintf (&buf, _("\
604
%s: option `--%s' doesn't allow an argument\n"),
605
argv[0], pfound->name);
607
fprintf (stderr, _("\
608
%s: option `--%s' doesn't allow an argument\n"),
609
argv[0], pfound->name);
703
/* +option or -option */
705
_("%s: option `%c%s' doesn't allow an argument\n"),
706
argv[0], argv[optind - 1][0], pfound->name);
614
/* +option or -option */
615
#if defined _LIBC && defined USE_IN_LIBIO
616
n = __asprintf (&buf, _("\
617
%s: option `%c%s' doesn't allow an argument\n"),
618
argv[0], argv[d->optind - 1][0],
621
fprintf (stderr, _("\
622
%s: option `%c%s' doesn't allow an argument\n"),
623
argv[0], argv[d->optind - 1][0],
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;
637
if (_IO_fwide (stderr, 0) > 0)
638
__fwprintf (stderr, L"%s", buf);
642
((_IO_FILE *) stderr)->_flags2 = old_flags2;
643
_IO_funlockfile (stderr);
709
nextchar += strlen (nextchar);
650
d->__nextchar += strlen (d->__nextchar);
711
optopt = pfound->val;
652
d->optopt = pfound->val;
715
656
else if (pfound->has_arg == 1)
718
optarg = argv[optind++];
658
if (d->optind < argc)
659
d->optarg = argv[d->optind++];
723
_("%s: option `%s' requires an argument\n"),
724
argv[0], argv[optind - 1]);
725
nextchar += strlen (nextchar);
726
optopt = pfound->val;
664
#if defined _LIBC && defined USE_IN_LIBIO
667
if (__asprintf (&buf, _("\
668
%s: option `%s' requires an argument\n"),
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;
677
if (_IO_fwide (stderr, 0) > 0)
678
__fwprintf (stderr, L"%s", buf);
682
((_IO_FILE *) stderr)->_flags2 = old_flags2;
683
_IO_funlockfile (stderr);
689
_("%s: option `%s' requires an argument\n"),
690
argv[0], argv[d->optind - 1]);
693
d->__nextchar += strlen (d->__nextchar);
694
d->optopt = pfound->val;
727
695
return optstring[0] == ':' ? ':' : '?';
730
nextchar += strlen (nextchar);
698
d->__nextchar += strlen (d->__nextchar);
731
699
if (longind != NULL)
732
700
*longind = option_index;
733
701
if (pfound->flag)
742
710
or the option starts with '--' or is not a valid short
743
711
option, then it's an error.
744
712
Otherwise interpret it as a short option. */
745
if (!long_only || argv[optind][1] == '-'
746
|| my_index (optstring, *nextchar) == NULL)
713
if (!long_only || argv[d->optind][1] == '-'
714
|| strchr (optstring, *d->__nextchar) == NULL)
750
if (argv[optind][1] == '-')
752
fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
718
#if defined _LIBC && defined USE_IN_LIBIO
723
if (argv[d->optind][1] == '-')
726
#if defined _LIBC && defined USE_IN_LIBIO
727
n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
728
argv[0], d->__nextchar);
730
fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
731
argv[0], d->__nextchar);
755
/* +option or -option */
756
fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
757
argv[0], argv[optind][0], nextchar);
736
/* +option or -option */
737
#if defined _LIBC && defined USE_IN_LIBIO
738
n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
739
argv[0], argv[d->optind][0], d->__nextchar);
741
fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
742
argv[0], argv[d->optind][0], d->__nextchar);
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;
754
if (_IO_fwide (stderr, 0) > 0)
755
__fwprintf (stderr, L"%s", buf);
759
((_IO_FILE *) stderr)->_flags2 = old_flags2;
760
_IO_funlockfile (stderr);
759
nextchar = (char *) "";
766
d->__nextchar = (char *) "";
766
773
/* Look at and handle the next short option-character. */
769
char c = *nextchar++;
770
char *temp = my_index (optstring, c);
776
char c = *d->__nextchar++;
777
char *temp = strchr (optstring, c);
772
779
/* Increment `optind' when we start to process its last character. */
773
if (*nextchar == '\0')
780
if (*d->__nextchar == '\0')
776
783
if (temp == NULL || c == ':')
781
/* 1003.2 specifies the format of this message. */
782
fprintf (stderr, _("%s: illegal option -- %c\n"),
787
#if defined _LIBC && defined USE_IN_LIBIO
792
if (d->__posixly_correct)
794
/* 1003.2 specifies the format of this message. */
795
#if defined _LIBC && defined USE_IN_LIBIO
796
n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
799
fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
785
fprintf (stderr, _("%s: invalid option -- %c\n"),
804
#if defined _LIBC && defined USE_IN_LIBIO
805
n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
808
fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
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;
820
if (_IO_fwide (stderr, 0) > 0)
821
__fwprintf (stderr, L"%s", buf);
825
((_IO_FILE *) stderr)->_flags2 = old_flags2;
826
_IO_funlockfile (stderr);
791
835
/* Convenience. Treat POSIX -W foo same as long option --foo */
800
844
int option_index;
802
846
/* This is an option that requires an argument. */
803
if (*nextchar != '\0')
847
if (*d->__nextchar != '\0')
849
d->optarg = d->__nextchar;
806
850
/* If we end this ARGV-element by taking the rest as an arg,
807
851
we must advance to the next element now. */
810
else if (optind == argc)
854
else if (d->optind == argc)
814
858
/* 1003.2 specifies the format of this message. */
859
#if defined _LIBC && defined USE_IN_LIBIO
862
if (__asprintf (&buf,
863
_("%s: option requires an argument -- %c\n"),
866
_IO_flockfile (stderr);
868
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
869
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
871
if (_IO_fwide (stderr, 0) > 0)
872
__fwprintf (stderr, L"%s", buf);
876
((_IO_FILE *) stderr)->_flags2 = old_flags2;
877
_IO_funlockfile (stderr);
815
882
fprintf (stderr, _("%s: option requires an argument -- %c\n"),
819
887
if (optstring[0] == ':')
826
/* We already incremented `optind' once;
894
/* We already incremented `d->optind' once;
827
895
increment it again when taking next ARGV-elt as argument. */
828
optarg = argv[optind++];
896
d->optarg = argv[d->optind++];
830
898
/* optarg is now the argument, see if it's in the
831
899
table of longopts. */
833
for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
901
for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
834
903
/* Do nothing. */ ;
836
905
/* Test all long options for either exact match
837
906
or abbreviated matches. */
838
907
for (p = longopts, option_index = 0; p->name; p++, option_index++)
839
if (!strncmp (p->name, nextchar, nameend - nextchar))
908
if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
841
if ((unsigned int) (nameend - nextchar) == strlen (p->name))
910
if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
843
912
/* Exact match found. */
859
928
if (ambig && !exact)
862
fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
863
argv[0], argv[optind]);
864
nextchar += strlen (nextchar);
932
#if defined _LIBC && defined USE_IN_LIBIO
935
if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
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;
943
if (_IO_fwide (stderr, 0) > 0)
944
__fwprintf (stderr, L"%s", buf);
948
((_IO_FILE *) stderr)->_flags2 = old_flags2;
949
_IO_funlockfile (stderr);
954
fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
955
argv[0], argv[d->optind]);
958
d->__nextchar += strlen (d->__nextchar);
868
962
if (pfound != NULL)
873
967
/* Don't test has_arg with >, because some C compilers don't
874
968
allow it to be used on enums. */
875
969
if (pfound->has_arg)
876
optarg = nameend + 1;
970
d->optarg = nameend + 1;
880
fprintf (stderr, _("\
881
%s: option `-W %s' doesn't allow an argument\n"),
882
argv[0], pfound->name);
884
nextchar += strlen (nextchar);
975
#if defined _LIBC && defined USE_IN_LIBIO
978
if (__asprintf (&buf, _("\
979
%s: option `-W %s' doesn't allow an argument\n"),
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;
988
if (_IO_fwide (stderr, 0) > 0)
989
__fwprintf (stderr, L"%s", buf);
993
((_IO_FILE *) stderr)->_flags2 = old_flags2;
994
_IO_funlockfile (stderr);
999
fprintf (stderr, _("\
1000
%s: option `-W %s' doesn't allow an argument\n"),
1001
argv[0], pfound->name);
1005
d->__nextchar += strlen (d->__nextchar);
888
1009
else if (pfound->has_arg == 1)
891
optarg = argv[optind++];
1011
if (d->optind < argc)
1012
d->optarg = argv[d->optind++];
896
_("%s: option `%s' requires an argument\n"),
897
argv[0], argv[optind - 1]);
898
nextchar += strlen (nextchar);
1017
#if defined _LIBC && defined USE_IN_LIBIO
1020
if (__asprintf (&buf, _("\
1021
%s: option `%s' requires an argument\n"),
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;
1030
if (_IO_fwide (stderr, 0) > 0)
1031
__fwprintf (stderr, L"%s", buf);
1033
fputs (buf, stderr);
1035
((_IO_FILE *) stderr)->_flags2 = old_flags2;
1036
_IO_funlockfile (stderr);
1042
_("%s: option `%s' requires an argument\n"),
1043
argv[0], argv[d->optind - 1]);
1046
d->__nextchar += strlen (d->__nextchar);
899
1047
return optstring[0] == ':' ? ':' : '?';
902
nextchar += strlen (nextchar);
1050
d->__nextchar += strlen (d->__nextchar);
903
1051
if (longind != NULL)
904
1052
*longind = option_index;
905
1053
if (pfound->flag)
917
1065
if (temp[2] == ':')
919
1067
/* This is an option that accepts an argument optionally. */
920
if (*nextchar != '\0')
1068
if (*d->__nextchar != '\0')
1070
d->optarg = d->__nextchar;
1075
d->__nextchar = NULL;
931
1079
/* This is an option that requires an argument. */
932
if (*nextchar != '\0')
1080
if (*d->__nextchar != '\0')
1082
d->optarg = d->__nextchar;
935
1083
/* If we end this ARGV-element by taking the rest as an arg,
936
1084
we must advance to the next element now. */
939
else if (optind == argc)
1087
else if (d->optind == argc)
943
1091
/* 1003.2 specifies the format of this message. */
1092
#if defined _LIBC && defined USE_IN_LIBIO
1095
if (__asprintf (&buf, _("\
1096
%s: option requires an argument -- %c\n"),
1099
_IO_flockfile (stderr);
1101
int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1102
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1104
if (_IO_fwide (stderr, 0) > 0)
1105
__fwprintf (stderr, L"%s", buf);
1107
fputs (buf, stderr);
1109
((_IO_FILE *) stderr)->_flags2 = old_flags2;
1110
_IO_funlockfile (stderr);
944
1115
fprintf (stderr,
945
_("%s: option requires an argument -- %c\n"),
1116
_("%s: option requires an argument -- %c\n"),
949
1121
if (optstring[0] == ':')
966
getopt (argc, argv, optstring)
969
const char *optstring;
1138
_getopt_internal (int argc, char *const *argv, const char *optstring,
1139
const struct option *longopts, int *longind, int long_only)
1143
getopt_data.optind = optind;
1144
getopt_data.opterr = opterr;
1146
result = _getopt_internal_r (argc, argv, optstring, longopts,
1147
longind, long_only, &getopt_data);
1149
optind = getopt_data.optind;
1150
optarg = getopt_data.optarg;
1151
optopt = getopt_data.optopt;
1157
getopt (int argc, char *const *argv, const char *optstring)
971
1159
return _getopt_internal (argc, argv, optstring,
972
1160
(const struct option *) 0,