~ubuntu-branches/ubuntu/maverick/texinfo/maverick

« back to all changes in this revision

Viewing changes to lib/getopt.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2005-10-28 15:10:30 UTC
  • mto: (2.1.1 dapper) (3.1.4 hardy)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20051028151030-9nsf2s2k2z3fktjt
Tags: upstream-4.8
ImportĀ upstreamĀ versionĀ 4.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Getopt for GNU.
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.
4
 
 
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
 
4
   before changing it!
 
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.
7
8
 
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
11
 
   later version.
 
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.
12
13
 
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.
17
18
 
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.  */
21
22
 
22
23
/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
27
28
 
28
29
#ifdef HAVE_CONFIG_H
29
30
# include <config.h>
30
 
#else
31
 
# if !defined __STDC__ || !__STDC__
32
 
/* This is a separate conditional since some stdc systems
33
 
   reject `defined (const)'.  */
34
 
#  ifndef const
35
 
#   define const
36
 
#  endif
37
 
# endif
38
31
#endif
39
32
 
40
33
#include <stdio.h>
41
34
 
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.  */
49
 
 
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
54
 
#  define ELIDE_CODE
55
 
# endif
56
 
#endif
57
 
 
58
 
#ifndef ELIDE_CODE
59
 
 
60
 
 
61
35
/* This needs to come after some library #include
62
36
   to get __GNU_LIBRARY__ defined.  */
63
37
#ifdef  __GNU_LIBRARY__
67
41
# include <unistd.h>
68
42
#endif  /* GNU C library.  */
69
43
 
 
44
#include <string.h>
 
45
 
70
46
#ifdef VMS
71
47
# include <unixlib.h>
72
 
# if HAVE_STRING_H - 0
73
 
#  include <string.h>
74
 
# endif
75
 
#endif
76
 
 
77
 
#ifndef _
78
 
/* This is for other GNU distributions with internationalized messages.
79
 
   When compiling libc, the _ macro is predefined.  */
80
 
# ifdef HAVE_LIBINTL_H
81
 
#  include <libintl.h>
82
 
#  define _(msgid)      gettext (msgid)
83
 
# else
84
 
#  define _(msgid)      (msgid)
85
 
# endif
 
48
#endif
 
49
 
 
50
#ifdef _LIBC
 
51
# include <libintl.h>
 
52
#else
 
53
# include "gettext.h"
 
54
# define _(msgid) gettext (msgid)
 
55
#endif
 
56
 
 
57
#if defined _LIBC && defined USE_IN_LIBIO
 
58
# include <wchar.h>
 
59
#endif
 
60
 
 
61
#ifndef attribute_hidden
 
62
# define attribute_hidden
86
63
#endif
87
64
 
88
65
/* This version of `getopt' appears to the caller like standard Unix `getopt'
100
77
   they can distinguish the relative order of options and other arguments.  */
101
78
 
102
79
#include "getopt.h"
 
80
#include "getopt_int.h"
103
81
 
104
82
/* For communication from `getopt' to the caller.
105
83
   When `getopt' finds an option that takes an argument,
124
102
/* 1003.2 says this must be 1 before any call.  */
125
103
int optind = 1;
126
104
 
127
 
/* Formerly, initialization of getopt depended on optind==0, which
128
 
   causes problems with re-calling getopt as programs generally don't
129
 
   know that. */
130
 
 
131
 
int __getopt_initialized;
132
 
 
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.
136
 
 
137
 
   If this is zero, or a null string, it means resume the scan
138
 
   by advancing to the next ARGV-element.  */
139
 
 
140
 
static char *nextchar;
141
 
 
142
105
/* Callers store zero here to inhibit the error message
143
106
   for unrecognized options.  */
144
107
 
150
113
 
151
114
int optopt = '?';
152
115
 
153
 
/* Describe how to deal with options that follow non-option ARGV-elements.
154
 
 
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.
158
 
 
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.
165
 
 
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
169
 
   expect this.
170
 
 
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.
177
 
 
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.  */
181
 
 
182
 
static enum
183
 
{
184
 
  REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
185
 
} ordering;
186
 
 
187
 
/* Value of POSIXLY_CORRECT environment variable.  */
188
 
static char *posixly_correct;
 
116
/* Keep a global copy of all internal members of getopt_data.  */
 
117
 
 
118
static struct _getopt_data getopt_data;
 
119
 
189
120
 
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
194
 
   in GCC.  */
195
 
# include <string.h>
196
 
# define my_index       strchr
197
 
#else
198
 
 
199
 
# if HAVE_STRING_H
200
 
#  include <string.h>
201
 
# else
202
 
#  include <strings.h>
203
 
# endif
 
121
#ifndef __GNU_LIBRARY__
204
122
 
205
123
/* Avoid depending on library functions or files
206
124
   whose names are inconsistent.  */
209
127
extern char *getenv ();
210
128
#endif
211
129
 
212
 
static char *
213
 
my_index (str, chr)
214
 
     const char *str;
215
 
     int chr;
216
 
{
217
 
  while (*str)
218
 
    {
219
 
      if (*str == chr)
220
 
        return (char *) str;
221
 
      str++;
222
 
    }
223
 
  return 0;
224
 
}
225
 
 
226
 
/* If using GCC, we can safely declare strlen this way.
227
 
   If not using GCC, it is ok not to declare it.  */
228
 
#ifdef __GNUC__
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__ */
237
 
 
238
130
#endif /* not __GNU_LIBRARY__ */
239
131
 
240
 
/* Handle permutation of arguments.  */
241
 
 
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.  */
245
 
 
246
 
static int first_nonopt;
247
 
static int last_nonopt;
248
 
 
249
132
#ifdef _LIBC
 
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;
 
138
 
250
139
/* Bash 2.0 gives us an environment variable containing flags
251
140
   indicating ARGV elements that should not be considered arguments.  */
252
141
 
 
142
# ifdef USE_NONOPTION_FLAGS
253
143
/* Defined in getopt_init.c  */
254
144
extern char *__getopt_nonoption_flags;
255
 
 
256
 
static int nonoption_flags_max_len;
257
 
static int nonoption_flags_len;
258
 
 
259
 
static int original_argc;
260
 
static char *const *original_argv;
261
 
 
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.  */
265
 
static void
266
 
__attribute__ ((unused))
267
 
store_args_and_env (int argc, char *const *argv)
268
 
{
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;
273
 
}
274
 
# ifdef text_set_element
275
 
text_set_element (__libc_subinit, store_args_and_env);
276
 
# endif /* text_set_element */
277
 
 
278
 
# define SWAP_FLAGS(ch1, ch2) \
279
 
  if (nonoption_flags_len > 0)                                                \
 
145
# endif
 
146
 
 
147
# ifdef USE_NONOPTION_FLAGS
 
148
#  define SWAP_FLAGS(ch1, ch2) \
 
149
  if (d->__nonoption_flags_len > 0)                                           \
280
150
    {                                                                         \
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;                                  \
284
154
    }
 
155
# else
 
156
#  define SWAP_FLAGS(ch1, ch2)
 
157
# endif
285
158
#else   /* !_LIBC */
286
159
# define SWAP_FLAGS(ch1, ch2)
287
160
#endif  /* _LIBC */
295
168
   `first_nonopt' and `last_nonopt' are relocated so that they describe
296
169
   the new indices of the non-options in ARGV after they are moved.  */
297
170
 
298
 
#if defined __STDC__ && __STDC__
299
 
static void exchange (char **);
300
 
#endif
301
 
 
302
171
static void
303
 
exchange (argv)
304
 
     char **argv;
 
172
exchange (char **argv, struct _getopt_data *d)
305
173
{
306
 
  int bottom = first_nonopt;
307
 
  int middle = last_nonopt;
308
 
  int top = optind;
 
174
  int bottom = d->__first_nonopt;
 
175
  int middle = d->__last_nonopt;
 
176
  int top = d->optind;
309
177
  char *tem;
310
178
 
311
179
  /* Exchange the shorter segment with the far end of the longer segment.
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.  */
315
183
 
316
 
#ifdef _LIBC
 
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)
321
189
    {
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;
327
195
      else
328
196
        {
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;
334
202
        }
335
203
    }
375
243
 
376
244
  /* Update records for the slots the non-options now occupy.  */
377
245
 
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;
380
248
}
381
249
 
382
250
/* Initialize the internal data when the first call is made.  */
383
251
 
384
 
#if defined __STDC__ && __STDC__
385
 
static const char *_getopt_initialize (int, char *const *, const char *);
386
 
#endif
387
252
static const char *
388
 
_getopt_initialize (argc, argv, optstring)
389
 
     int argc;
390
 
     char *const *argv;
391
 
     const char *optstring;
 
253
_getopt_initialize (int argc, char *const *argv, const char *optstring,
 
254
                    struct _getopt_data *d)
392
255
{
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.  */
396
259
 
397
 
  first_nonopt = last_nonopt = optind;
398
 
 
399
 
  nextchar = NULL;
400
 
 
401
 
  posixly_correct = getenv ("POSIXLY_CORRECT");
 
260
  d->__first_nonopt = d->__last_nonopt = d->optind;
 
261
 
 
262
  d->__nextchar = NULL;
 
263
 
 
264
  d->__posixly_correct = !!getenv ("POSIXLY_CORRECT");
402
265
 
403
266
  /* Determine how to handle the ordering of options and nonoptions.  */
404
267
 
405
268
  if (optstring[0] == '-')
406
269
    {
407
 
      ordering = RETURN_IN_ORDER;
 
270
      d->__ordering = RETURN_IN_ORDER;
408
271
      ++optstring;
409
272
    }
410
273
  else if (optstring[0] == '+')
411
274
    {
412
 
      ordering = REQUIRE_ORDER;
 
275
      d->__ordering = REQUIRE_ORDER;
413
276
      ++optstring;
414
277
    }
415
 
  else if (posixly_correct != NULL)
416
 
    ordering = REQUIRE_ORDER;
 
278
  else if (d->__posixly_correct)
 
279
    d->__ordering = REQUIRE_ORDER;
417
280
  else
418
 
    ordering = PERMUTE;
 
281
    d->__ordering = PERMUTE;
419
282
 
420
 
#ifdef _LIBC
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)
423
286
    {
424
 
      if (nonoption_flags_max_len == 0)
 
287
      if (d->__nonoption_flags_max_len == 0)
425
288
        {
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;
429
292
          else
430
293
            {
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;
439
302
              else
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);
442
305
            }
443
306
        }
444
 
      nonoption_flags_len = nonoption_flags_max_len;
 
307
      d->__nonoption_flags_len = d->__nonoption_flags_max_len;
445
308
    }
446
309
  else
447
 
    nonoption_flags_len = 0;
 
310
    d->__nonoption_flags_len = 0;
448
311
#endif
449
312
 
450
313
  return optstring;
507
370
   long-named options.  */
508
371
 
509
372
int
510
 
_getopt_internal (argc, argv, optstring, longopts, longind, long_only)
511
 
     int argc;
512
 
     char *const *argv;
513
 
     const char *optstring;
514
 
     const struct option *longopts;
515
 
     int *longind;
516
 
     int long_only;
 
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)
517
376
{
518
 
  optarg = NULL;
519
 
 
520
 
  if (optind == 0 || !__getopt_initialized)
 
377
  int print_errors = d->opterr;
 
378
  if (optstring[0] == ':')
 
379
    print_errors = 0;
 
380
 
 
381
  if (argc < 1)
 
382
    return -1;
 
383
 
 
384
  d->optarg = NULL;
 
385
 
 
386
  if (d->optind == 0 || !d->__initialized)
521
387
    {
522
 
      if (optind == 0)
523
 
        optind = 1;     /* Don't scan ARGV[0], the program name.  */
524
 
      optstring = _getopt_initialize (argc, argv, optstring);
525
 
      __getopt_initialized = 1;
 
388
      if (d->optind == 0)
 
389
        d->optind = 1;  /* Don't scan ARGV[0], the program name.  */
 
390
      optstring = _getopt_initialize (argc, argv, optstring, d);
 
391
      d->__initialized = 1;
526
392
    }
527
393
 
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.  */
532
 
#ifdef _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'))
536
402
#else
537
 
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
 
403
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
538
404
#endif
539
405
 
540
 
  if (nextchar == NULL || *nextchar == '\0')
 
406
  if (d->__nextchar == NULL || *d->__nextchar == '\0')
541
407
    {
542
408
      /* Advance to the next ARGV-element.  */
543
409
 
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;
550
416
 
551
 
      if (ordering == PERMUTE)
 
417
      if (d->__ordering == PERMUTE)
552
418
        {
553
419
          /* If we have just processed some options following some non-options,
554
420
             exchange them so that the options come first.  */
555
421
 
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;
560
427
 
561
428
          /* Skip any additional non-options
562
429
             and extend the range of non-options previously skipped.  */
563
430
 
564
 
          while (optind < argc && NONOPTION_P)
565
 
            optind++;
566
 
          last_nonopt = optind;
 
431
          while (d->optind < argc && NONOPTION_P)
 
432
            d->optind++;
 
433
          d->__last_nonopt = d->optind;
567
434
        }
568
435
 
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.  */
573
440
 
574
 
      if (optind != argc && !strcmp (argv[optind], "--"))
 
441
      if (d->optind != argc && !strcmp (argv[d->optind], "--"))
575
442
        {
576
 
          optind++;
577
 
 
578
 
          if (first_nonopt != last_nonopt && last_nonopt != optind)
579
 
            exchange ((char **) argv);
580
 
          else if (first_nonopt == last_nonopt)
581
 
            first_nonopt = optind;
582
 
          last_nonopt = argc;
583
 
 
584
 
          optind = argc;
 
443
          d->optind++;
 
444
 
 
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;
 
451
 
 
452
          d->optind = argc;
585
453
        }
586
454
 
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.  */
589
457
 
590
 
      if (optind == argc)
 
458
      if (d->optind == argc)
591
459
        {
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;
596
464
          return -1;
597
465
        }
598
466
 
601
469
 
602
470
      if (NONOPTION_P)
603
471
        {
604
 
          if (ordering == REQUIRE_ORDER)
 
472
          if (d->__ordering == REQUIRE_ORDER)
605
473
            return -1;
606
 
          optarg = argv[optind++];
 
474
          d->optarg = argv[d->optind++];
607
475
          return 1;
608
476
        }
609
477
 
610
478
      /* We have found another option-ARGV-element.
611
479
         Skip the initial punctuation.  */
612
480
 
613
 
      nextchar = (argv[optind] + 1
614
 
                  + (longopts != NULL && argv[optind][1] == '-'));
 
481
      d->__nextchar = (argv[d->optind] + 1
 
482
                  + (longopts != NULL && argv[d->optind][1] == '-'));
615
483
    }
616
484
 
617
485
  /* Decode the current option-ARGV-element.  */
630
498
     This distinction seems to be the most useful approach.  */
631
499
 
632
500
  if (longopts != NULL
633
 
      && (argv[optind][1] == '-'
634
 
          || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
 
501
      && (argv[d->optind][1] == '-'
 
502
          || (long_only && (argv[d->optind][2]
 
503
                            || !strchr (optstring, argv[d->optind][1])))))
635
504
    {
636
505
      char *nameend;
637
506
      const struct option *p;
641
510
      int indfound = -1;
642
511
      int option_index;
643
512
 
644
 
      for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
 
513
      for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
645
514
        /* Do nothing.  */ ;
646
515
 
647
516
      /* Test all long options for either exact match
648
517
         or abbreviated matches.  */
649
518
      for (p = longopts, option_index = 0; p->name; p++, option_index++)
650
 
        if (!strncmp (p->name, nextchar, nameend - nextchar))
 
519
        if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
651
520
          {
652
 
            if ((unsigned int) (nameend - nextchar)
 
521
            if ((unsigned int) (nameend - d->__nextchar)
653
522
                == (unsigned int) strlen (p->name))
654
523
              {
655
524
                /* Exact match found.  */
664
533
                pfound = p;
665
534
                indfound = option_index;
666
535
              }
667
 
            else
 
536
            else if (long_only
 
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.  */
669
541
              ambig = 1;
670
542
          }
671
543
 
672
544
      if (ambig && !exact)
673
545
        {
674
 
          if (opterr)
675
 
            fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
676
 
                     argv[0], argv[optind]);
677
 
          nextchar += strlen (nextchar);
678
 
          optind++;
679
 
          optopt = 0;
 
546
          if (print_errors)
 
547
            {
 
548
#if defined _LIBC && defined USE_IN_LIBIO
 
549
              char *buf;
 
550
 
 
551
              if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
 
552
                              argv[0], argv[d->optind]) >= 0)
 
553
                {
 
554
                  _IO_flockfile (stderr);
 
555
 
 
556
                  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
557
                  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
558
 
 
559
                  if (_IO_fwide (stderr, 0) > 0)
 
560
                    __fwprintf (stderr, L"%s", buf);
 
561
                  else
 
562
                    fputs (buf, stderr);
 
563
 
 
564
                  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
565
                  _IO_funlockfile (stderr);
 
566
 
 
567
                  free (buf);
 
568
                }
 
569
#else
 
570
              fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
 
571
                       argv[0], argv[d->optind]);
 
572
#endif
 
573
            }
 
574
          d->__nextchar += strlen (d->__nextchar);
 
575
          d->optind++;
 
576
          d->optopt = 0;
680
577
          return '?';
681
578
        }
682
579
 
683
580
      if (pfound != NULL)
684
581
        {
685
582
          option_index = indfound;
686
 
          optind++;
 
583
          d->optind++;
687
584
          if (*nameend)
688
585
            {
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;
693
590
              else
694
591
                {
695
 
                  if (opterr)
 
592
                  if (print_errors)
696
593
                    {
697
 
                      if (argv[optind - 1][1] == '-')
698
 
                        /* --option */
699
 
                        fprintf (stderr,
700
 
                                 _("%s: option `--%s' doesn't allow an argument\n"),
701
 
                                 argv[0], pfound->name);
 
594
#if defined _LIBC && defined USE_IN_LIBIO
 
595
                      char *buf;
 
596
                      int n;
 
597
#endif
 
598
 
 
599
                      if (argv[d->optind - 1][1] == '-')
 
600
                        {
 
601
                          /* --option */
 
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);
 
606
#else
 
607
                          fprintf (stderr, _("\
 
608
%s: option `--%s' doesn't allow an argument\n"),
 
609
                                   argv[0], pfound->name);
 
610
#endif
 
611
                        }
702
612
                      else
703
 
                        /* +option or -option */
704
 
                        fprintf (stderr,
705
 
                                 _("%s: option `%c%s' doesn't allow an argument\n"),
706
 
                                 argv[0], argv[optind - 1][0], pfound->name);
 
613
                        {
 
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],
 
619
                                          pfound->name);
 
620
#else
 
621
                          fprintf (stderr, _("\
 
622
%s: option `%c%s' doesn't allow an argument\n"),
 
623
                                   argv[0], argv[d->optind - 1][0],
 
624
                                   pfound->name);
 
625
#endif
 
626
                        }
 
627
 
 
628
#if defined _LIBC && defined USE_IN_LIBIO
 
629
                      if (n >= 0)
 
630
                        {
 
631
                          _IO_flockfile (stderr);
 
632
 
 
633
                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
634
                          ((_IO_FILE *) stderr)->_flags2
 
635
                            |= _IO_FLAGS2_NOTCANCEL;
 
636
 
 
637
                          if (_IO_fwide (stderr, 0) > 0)
 
638
                            __fwprintf (stderr, L"%s", buf);
 
639
                          else
 
640
                            fputs (buf, stderr);
 
641
 
 
642
                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
643
                          _IO_funlockfile (stderr);
 
644
 
 
645
                          free (buf);
 
646
                        }
 
647
#endif
707
648
                    }
708
649
 
709
 
                  nextchar += strlen (nextchar);
 
650
                  d->__nextchar += strlen (d->__nextchar);
710
651
 
711
 
                  optopt = pfound->val;
 
652
                  d->optopt = pfound->val;
712
653
                  return '?';
713
654
                }
714
655
            }
715
656
          else if (pfound->has_arg == 1)
716
657
            {
717
 
              if (optind < argc)
718
 
                optarg = argv[optind++];
 
658
              if (d->optind < argc)
 
659
                d->optarg = argv[d->optind++];
719
660
              else
720
661
                {
721
 
                  if (opterr)
722
 
                    fprintf (stderr,
723
 
                           _("%s: option `%s' requires an argument\n"),
724
 
                           argv[0], argv[optind - 1]);
725
 
                  nextchar += strlen (nextchar);
726
 
                  optopt = pfound->val;
 
662
                  if (print_errors)
 
663
                    {
 
664
#if defined _LIBC && defined USE_IN_LIBIO
 
665
                      char *buf;
 
666
 
 
667
                      if (__asprintf (&buf, _("\
 
668
%s: option `%s' requires an argument\n"),
 
669
                                      argv[0], argv[d->optind - 1]) >= 0)
 
670
                        {
 
671
                          _IO_flockfile (stderr);
 
672
 
 
673
                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
674
                          ((_IO_FILE *) stderr)->_flags2
 
675
                            |= _IO_FLAGS2_NOTCANCEL;
 
676
 
 
677
                          if (_IO_fwide (stderr, 0) > 0)
 
678
                            __fwprintf (stderr, L"%s", buf);
 
679
                          else
 
680
                            fputs (buf, stderr);
 
681
 
 
682
                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
683
                          _IO_funlockfile (stderr);
 
684
 
 
685
                          free (buf);
 
686
                        }
 
687
#else
 
688
                      fprintf (stderr,
 
689
                               _("%s: option `%s' requires an argument\n"),
 
690
                               argv[0], argv[d->optind - 1]);
 
691
#endif
 
692
                    }
 
693
                  d->__nextchar += strlen (d->__nextchar);
 
694
                  d->optopt = pfound->val;
727
695
                  return optstring[0] == ':' ? ':' : '?';
728
696
                }
729
697
            }
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)
747
715
        {
748
 
          if (opterr)
 
716
          if (print_errors)
749
717
            {
750
 
              if (argv[optind][1] == '-')
751
 
                /* --option */
752
 
                fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
753
 
                         argv[0], nextchar);
 
718
#if defined _LIBC && defined USE_IN_LIBIO
 
719
              char *buf;
 
720
              int n;
 
721
#endif
 
722
 
 
723
              if (argv[d->optind][1] == '-')
 
724
                {
 
725
                  /* --option */
 
726
#if defined _LIBC && defined USE_IN_LIBIO
 
727
                  n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
 
728
                                  argv[0], d->__nextchar);
 
729
#else
 
730
                  fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
 
731
                           argv[0], d->__nextchar);
 
732
#endif
 
733
                }
754
734
              else
755
 
                /* +option or -option */
756
 
                fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
757
 
                         argv[0], argv[optind][0], nextchar);
 
735
                {
 
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);
 
740
#else
 
741
                  fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
 
742
                           argv[0], argv[d->optind][0], d->__nextchar);
 
743
#endif
 
744
                }
 
745
 
 
746
#if defined _LIBC && defined USE_IN_LIBIO
 
747
              if (n >= 0)
 
748
                {
 
749
                  _IO_flockfile (stderr);
 
750
 
 
751
                  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
752
                  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
753
 
 
754
                  if (_IO_fwide (stderr, 0) > 0)
 
755
                    __fwprintf (stderr, L"%s", buf);
 
756
                  else
 
757
                    fputs (buf, stderr);
 
758
 
 
759
                  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
760
                  _IO_funlockfile (stderr);
 
761
 
 
762
                  free (buf);
 
763
                }
 
764
#endif
758
765
            }
759
 
          nextchar = (char *) "";
760
 
          optind++;
761
 
          optopt = 0;
 
766
          d->__nextchar = (char *) "";
 
767
          d->optind++;
 
768
          d->optopt = 0;
762
769
          return '?';
763
770
        }
764
771
    }
766
773
  /* Look at and handle the next short option-character.  */
767
774
 
768
775
  {
769
 
    char c = *nextchar++;
770
 
    char *temp = my_index (optstring, c);
 
776
    char c = *d->__nextchar++;
 
777
    char *temp = strchr (optstring, c);
771
778
 
772
779
    /* Increment `optind' when we start to process its last character.  */
773
 
    if (*nextchar == '\0')
774
 
      ++optind;
 
780
    if (*d->__nextchar == '\0')
 
781
      ++d->optind;
775
782
 
776
783
    if (temp == NULL || c == ':')
777
784
      {
778
 
        if (opterr)
 
785
        if (print_errors)
779
786
          {
780
 
            if (posixly_correct)
781
 
              /* 1003.2 specifies the format of this message.  */
782
 
              fprintf (stderr, _("%s: illegal option -- %c\n"),
783
 
                       argv[0], c);
 
787
#if defined _LIBC && defined USE_IN_LIBIO
 
788
              char *buf;
 
789
              int n;
 
790
#endif
 
791
 
 
792
            if (d->__posixly_correct)
 
793
              {
 
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"),
 
797
                                argv[0], c);
 
798
#else
 
799
                fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
 
800
#endif
 
801
              }
784
802
            else
785
 
              fprintf (stderr, _("%s: invalid option -- %c\n"),
786
 
                       argv[0], c);
 
803
              {
 
804
#if defined _LIBC && defined USE_IN_LIBIO
 
805
                n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
 
806
                                argv[0], c);
 
807
#else
 
808
                fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
 
809
#endif
 
810
              }
 
811
 
 
812
#if defined _LIBC && defined USE_IN_LIBIO
 
813
            if (n >= 0)
 
814
              {
 
815
                _IO_flockfile (stderr);
 
816
 
 
817
                int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
818
                ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
819
 
 
820
                if (_IO_fwide (stderr, 0) > 0)
 
821
                  __fwprintf (stderr, L"%s", buf);
 
822
                else
 
823
                  fputs (buf, stderr);
 
824
 
 
825
                ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
826
                _IO_funlockfile (stderr);
 
827
 
 
828
                free (buf);
 
829
              }
 
830
#endif
787
831
          }
788
 
        optopt = c;
 
832
        d->optopt = c;
789
833
        return '?';
790
834
      }
791
835
    /* Convenience. Treat POSIX -W foo same as long option --foo */
800
844
        int option_index;
801
845
 
802
846
        /* This is an option that requires an argument.  */
803
 
        if (*nextchar != '\0')
 
847
        if (*d->__nextchar != '\0')
804
848
          {
805
 
            optarg = nextchar;
 
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.  */
808
 
            optind++;
 
852
            d->optind++;
809
853
          }
810
 
        else if (optind == argc)
 
854
        else if (d->optind == argc)
811
855
          {
812
 
            if (opterr)
 
856
            if (print_errors)
813
857
              {
814
858
                /* 1003.2 specifies the format of this message.  */
 
859
#if defined _LIBC && defined USE_IN_LIBIO
 
860
                char *buf;
 
861
 
 
862
                if (__asprintf (&buf,
 
863
                                _("%s: option requires an argument -- %c\n"),
 
864
                                argv[0], c) >= 0)
 
865
                  {
 
866
                    _IO_flockfile (stderr);
 
867
 
 
868
                    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
869
                    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
870
 
 
871
                    if (_IO_fwide (stderr, 0) > 0)
 
872
                      __fwprintf (stderr, L"%s", buf);
 
873
                    else
 
874
                      fputs (buf, stderr);
 
875
 
 
876
                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
877
                    _IO_funlockfile (stderr);
 
878
 
 
879
                    free (buf);
 
880
                  }
 
881
#else
815
882
                fprintf (stderr, _("%s: option requires an argument -- %c\n"),
816
883
                         argv[0], c);
 
884
#endif
817
885
              }
818
 
            optopt = c;
 
886
            d->optopt = c;
819
887
            if (optstring[0] == ':')
820
888
              c = ':';
821
889
            else
823
891
            return c;
824
892
          }
825
893
        else
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++];
829
897
 
830
898
        /* optarg is now the argument, see if it's in the
831
899
           table of longopts.  */
832
900
 
833
 
        for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
 
901
        for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
 
902
             nameend++)
834
903
          /* Do nothing.  */ ;
835
904
 
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))
840
909
            {
841
 
              if ((unsigned int) (nameend - nextchar) == strlen (p->name))
 
910
              if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
842
911
                {
843
912
                  /* Exact match found.  */
844
913
                  pfound = p;
858
927
            }
859
928
        if (ambig && !exact)
860
929
          {
861
 
            if (opterr)
862
 
              fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
863
 
                       argv[0], argv[optind]);
864
 
            nextchar += strlen (nextchar);
865
 
            optind++;
 
930
            if (print_errors)
 
931
              {
 
932
#if defined _LIBC && defined USE_IN_LIBIO
 
933
                char *buf;
 
934
 
 
935
                if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
 
936
                                argv[0], argv[d->optind]) >= 0)
 
937
                  {
 
938
                    _IO_flockfile (stderr);
 
939
 
 
940
                    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
941
                    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
942
 
 
943
                    if (_IO_fwide (stderr, 0) > 0)
 
944
                      __fwprintf (stderr, L"%s", buf);
 
945
                    else
 
946
                      fputs (buf, stderr);
 
947
 
 
948
                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
949
                    _IO_funlockfile (stderr);
 
950
 
 
951
                    free (buf);
 
952
                  }
 
953
#else
 
954
                fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
 
955
                         argv[0], argv[d->optind]);
 
956
#endif
 
957
              }
 
958
            d->__nextchar += strlen (d->__nextchar);
 
959
            d->optind++;
866
960
            return '?';
867
961
          }
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;
877
971
                else
878
972
                  {
879
 
                    if (opterr)
880
 
                      fprintf (stderr, _("\
881
 
%s: option `-W %s' doesn't allow an argument\n"),
882
 
                               argv[0], pfound->name);
883
 
 
884
 
                    nextchar += strlen (nextchar);
 
973
                    if (print_errors)
 
974
                      {
 
975
#if defined _LIBC && defined USE_IN_LIBIO
 
976
                        char *buf;
 
977
 
 
978
                        if (__asprintf (&buf, _("\
 
979
%s: option `-W %s' doesn't allow an argument\n"),
 
980
                                        argv[0], pfound->name) >= 0)
 
981
                          {
 
982
                            _IO_flockfile (stderr);
 
983
 
 
984
                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
985
                            ((_IO_FILE *) stderr)->_flags2
 
986
                              |= _IO_FLAGS2_NOTCANCEL;
 
987
 
 
988
                            if (_IO_fwide (stderr, 0) > 0)
 
989
                              __fwprintf (stderr, L"%s", buf);
 
990
                            else
 
991
                              fputs (buf, stderr);
 
992
 
 
993
                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
994
                            _IO_funlockfile (stderr);
 
995
 
 
996
                            free (buf);
 
997
                          }
 
998
#else
 
999
                        fprintf (stderr, _("\
 
1000
%s: option `-W %s' doesn't allow an argument\n"),
 
1001
                                 argv[0], pfound->name);
 
1002
#endif
 
1003
                      }
 
1004
 
 
1005
                    d->__nextchar += strlen (d->__nextchar);
885
1006
                    return '?';
886
1007
                  }
887
1008
              }
888
1009
            else if (pfound->has_arg == 1)
889
1010
              {
890
 
                if (optind < argc)
891
 
                  optarg = argv[optind++];
 
1011
                if (d->optind < argc)
 
1012
                  d->optarg = argv[d->optind++];
892
1013
                else
893
1014
                  {
894
 
                    if (opterr)
895
 
                      fprintf (stderr,
896
 
                               _("%s: option `%s' requires an argument\n"),
897
 
                               argv[0], argv[optind - 1]);
898
 
                    nextchar += strlen (nextchar);
 
1015
                    if (print_errors)
 
1016
                      {
 
1017
#if defined _LIBC && defined USE_IN_LIBIO
 
1018
                        char *buf;
 
1019
 
 
1020
                        if (__asprintf (&buf, _("\
 
1021
%s: option `%s' requires an argument\n"),
 
1022
                                        argv[0], argv[d->optind - 1]) >= 0)
 
1023
                          {
 
1024
                            _IO_flockfile (stderr);
 
1025
 
 
1026
                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
1027
                            ((_IO_FILE *) stderr)->_flags2
 
1028
                              |= _IO_FLAGS2_NOTCANCEL;
 
1029
 
 
1030
                            if (_IO_fwide (stderr, 0) > 0)
 
1031
                              __fwprintf (stderr, L"%s", buf);
 
1032
                            else
 
1033
                              fputs (buf, stderr);
 
1034
 
 
1035
                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
1036
                            _IO_funlockfile (stderr);
 
1037
 
 
1038
                            free (buf);
 
1039
                          }
 
1040
#else
 
1041
                        fprintf (stderr,
 
1042
                                 _("%s: option `%s' requires an argument\n"),
 
1043
                                 argv[0], argv[d->optind - 1]);
 
1044
#endif
 
1045
                      }
 
1046
                    d->__nextchar += strlen (d->__nextchar);
899
1047
                    return optstring[0] == ':' ? ':' : '?';
900
1048
                  }
901
1049
              }
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)
909
1057
              }
910
1058
            return pfound->val;
911
1059
          }
912
 
          nextchar = NULL;
 
1060
          d->__nextchar = NULL;
913
1061
          return 'W';   /* Let the application handle it.   */
914
1062
      }
915
1063
    if (temp[1] == ':')
917
1065
        if (temp[2] == ':')
918
1066
          {
919
1067
            /* This is an option that accepts an argument optionally.  */
920
 
            if (*nextchar != '\0')
 
1068
            if (*d->__nextchar != '\0')
921
1069
              {
922
 
                optarg = nextchar;
923
 
                optind++;
 
1070
                d->optarg = d->__nextchar;
 
1071
                d->optind++;
924
1072
              }
925
1073
            else
926
 
              optarg = NULL;
927
 
            nextchar = NULL;
 
1074
              d->optarg = NULL;
 
1075
            d->__nextchar = NULL;
928
1076
          }
929
1077
        else
930
1078
          {
931
1079
            /* This is an option that requires an argument.  */
932
 
            if (*nextchar != '\0')
 
1080
            if (*d->__nextchar != '\0')
933
1081
              {
934
 
                optarg = nextchar;
 
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.  */
937
 
                optind++;
 
1085
                d->optind++;
938
1086
              }
939
 
            else if (optind == argc)
 
1087
            else if (d->optind == argc)
940
1088
              {
941
 
                if (opterr)
 
1089
                if (print_errors)
942
1090
                  {
943
1091
                    /* 1003.2 specifies the format of this message.  */
 
1092
#if defined _LIBC && defined USE_IN_LIBIO
 
1093
                    char *buf;
 
1094
 
 
1095
                    if (__asprintf (&buf, _("\
 
1096
%s: option requires an argument -- %c\n"),
 
1097
                                    argv[0], c) >= 0)
 
1098
                      {
 
1099
                        _IO_flockfile (stderr);
 
1100
 
 
1101
                        int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
1102
                        ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
1103
 
 
1104
                        if (_IO_fwide (stderr, 0) > 0)
 
1105
                          __fwprintf (stderr, L"%s", buf);
 
1106
                        else
 
1107
                          fputs (buf, stderr);
 
1108
 
 
1109
                        ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
1110
                        _IO_funlockfile (stderr);
 
1111
 
 
1112
                        free (buf);
 
1113
                      }
 
1114
#else
944
1115
                    fprintf (stderr,
945
 
                           _("%s: option requires an argument -- %c\n"),
946
 
                           argv[0], c);
 
1116
                             _("%s: option requires an argument -- %c\n"),
 
1117
                             argv[0], c);
 
1118
#endif
947
1119
                  }
948
 
                optopt = c;
 
1120
                d->optopt = c;
949
1121
                if (optstring[0] == ':')
950
1122
                  c = ':';
951
1123
                else
954
1126
            else
955
1127
              /* We already incremented `optind' once;
956
1128
                 increment it again when taking next ARGV-elt as argument.  */
957
 
              optarg = argv[optind++];
958
 
            nextchar = NULL;
 
1129
              d->optarg = argv[d->optind++];
 
1130
            d->__nextchar = NULL;
959
1131
          }
960
1132
      }
961
1133
    return c;
963
1135
}
964
1136
 
965
1137
int
966
 
getopt (argc, argv, optstring)
967
 
     int argc;
968
 
     char *const *argv;
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)
 
1140
{
 
1141
  int result;
 
1142
 
 
1143
  getopt_data.optind = optind;
 
1144
  getopt_data.opterr = opterr;
 
1145
 
 
1146
  result = _getopt_internal_r (argc, argv, optstring, longopts,
 
1147
                               longind, long_only, &getopt_data);
 
1148
 
 
1149
  optind = getopt_data.optind;
 
1150
  optarg = getopt_data.optarg;
 
1151
  optopt = getopt_data.optopt;
 
1152
 
 
1153
  return result;
 
1154
}
 
1155
 
 
1156
int
 
1157
getopt (int argc, char *const *argv, const char *optstring)
970
1158
{
971
1159
  return _getopt_internal (argc, argv, optstring,
972
1160
                           (const struct option *) 0,
974
1162
                           0);
975
1163
}
976
1164
 
977
 
#endif  /* Not ELIDE_CODE.  */
978
1165
 
979
1166
#ifdef TEST
980
1167
 
982
1169
   the above definition of `getopt'.  */
983
1170
 
984
1171
int
985
 
main (argc, argv)
986
 
     int argc;
987
 
     char **argv;
 
1172
main (int argc, char **argv)
988
1173
{
989
1174
  int c;
990
1175
  int digit_optind = 0;