~ubuntu-branches/ubuntu/raring/findutils/raring

« back to all changes in this revision

Viewing changes to gnulib/lib/getopt.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2005-07-04 11:37:37 UTC
  • mto: (11.1.1 lenny) (1.1.10 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050704113737-oxfumqxsqgfz5gay
Tags: upstream-4.2.22
ImportĀ upstreamĀ versionĀ 4.2.22

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
4
4
   before changing it!
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.
8
8
 
9
9
   This program is free software; you can redistribute it and/or modify
18
18
 
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.  */
22
22
 
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>
31
31
#endif
32
32
 
33
 
#if !defined __STDC__ || !__STDC__
34
 
/* This is a separate conditional since some stdc systems
35
 
   reject `defined (const)'.  */
36
 
# ifndef const
37
 
#  define const
38
 
# endif
39
 
#endif
40
 
 
41
33
#include <stdio.h>
42
34
 
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.  */
50
 
 
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
55
 
#  define ELIDE_CODE
56
 
# endif
57
 
#endif
58
 
 
59
 
#ifndef ELIDE_CODE
60
 
 
61
 
 
62
35
/* This needs to come after some library #include
63
36
   to get __GNU_LIBRARY__ defined.  */
64
37
#ifdef  __GNU_LIBRARY__
68
41
# include <unistd.h>
69
42
#endif  /* GNU C library.  */
70
43
 
 
44
#include <string.h>
 
45
 
71
46
#ifdef VMS
72
47
# include <unixlib.h>
73
 
# if HAVE_STRING_H - 0
74
 
#  include <string.h>
75
 
# endif
76
48
#endif
77
49
 
78
50
#ifdef _LIBC
79
51
# include <libintl.h>
80
52
#else
81
 
/* This is for other GNU distributions with internationalized messages.  */
82
53
# include "gettext.h"
 
54
# define _(msgid) gettext (msgid)
83
55
#endif
84
 
#define _(msgid) gettext (msgid)
85
56
 
86
57
#if defined _LIBC && defined USE_IN_LIBIO
87
58
# include <wchar.h>
91
62
# define attribute_hidden
92
63
#endif
93
64
 
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.
97
67
 
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.
101
71
 
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
 
73
   disables permutation.
 
74
   Then the application's behavior is completely standard.
104
75
 
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.  */
107
78
 
108
79
#include "getopt.h"
 
80
#include "getopt_int.h"
109
81
 
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.  */
131
103
int optind = 1;
132
104
 
133
 
/* Formerly, initialization of getopt depended on optind==0, which
134
 
   causes problems with re-calling getopt as programs generally don't
135
 
   know that. */
136
 
 
137
 
int __getopt_initialized attribute_hidden;
138
 
 
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.
142
 
 
143
 
   If this is zero, or a null string, it means resume the scan
144
 
   by advancing to the next ARGV-element.  */
145
 
 
146
 
static char *nextchar;
147
 
 
148
105
/* Callers store zero here to inhibit the error message
149
106
   for unrecognized options.  */
150
107
 
156
113
 
157
114
int optopt = '?';
158
115
 
159
 
/* Describe how to deal with options that follow non-option ARGV-elements.
160
 
 
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.
164
 
 
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.
171
 
 
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
175
 
   expect this.
176
 
 
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.
183
 
 
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.  */
187
 
 
188
 
static enum
189
 
{
190
 
  REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
191
 
} ordering;
192
 
 
193
 
/* Value of POSIXLY_CORRECT environment variable.  */
194
 
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
 
195
120
 
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
200
 
   in GCC.  */
201
 
# include <string.h>
202
 
# define my_index       strchr
203
 
#else
204
 
 
205
 
# if HAVE_STRING_H
206
 
#  include <string.h>
207
 
# else
208
 
#  include <strings.h>
209
 
# endif
 
121
#ifndef __GNU_LIBRARY__
210
122
 
211
123
/* Avoid depending on library functions or files
212
124
   whose names are inconsistent.  */
215
127
extern char *getenv ();
216
128
#endif
217
129
 
218
 
static char *
219
 
my_index (str, chr)
220
 
     const char *str;
221
 
     int chr;
222
 
{
223
 
  while (*str)
224
 
    {
225
 
      if (*str == chr)
226
 
        return (char *) str;
227
 
      str++;
228
 
    }
229
 
  return 0;
230
 
}
231
 
 
232
 
/* If using GCC, we can safely declare strlen this way.
233
 
   If not using GCC, it is ok not to declare it.  */
234
 
#ifdef __GNUC__
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__ */
243
 
 
244
130
#endif /* not __GNU_LIBRARY__ */
245
131
 
246
 
/* Handle permutation of arguments.  */
247
 
 
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.  */
251
 
 
252
 
static int first_nonopt;
253
 
static int last_nonopt;
254
 
 
255
132
#ifdef _LIBC
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;
268
 
 
269
 
static int nonoption_flags_max_len;
270
 
static int nonoption_flags_len;
271
145
# endif
272
146
 
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)                                           \
276
150
    {                                                                         \
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.  */
296
170
 
297
 
#if defined __STDC__ && __STDC__
298
 
static void exchange (char **);
299
 
#endif
300
 
 
301
171
static void
302
 
exchange (argv)
303
 
     char **argv;
 
172
exchange (char **argv, struct _getopt_data *d)
304
173
{
305
 
  int bottom = first_nonopt;
306
 
  int middle = last_nonopt;
307
 
  int top = optind;
 
174
  int bottom = d->__first_nonopt;
 
175
  int middle = d->__last_nonopt;
 
176
  int top = d->optind;
308
177
  char *tem;
309
178
 
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)
320
189
    {
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;
326
195
      else
327
196
        {
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;
333
202
        }
334
203
    }
374
243
 
375
244
  /* Update records for the slots the non-options now occupy.  */
376
245
 
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;
379
248
}
380
249
 
381
250
/* Initialize the internal data when the first call is made.  */
382
251
 
383
 
#if defined __STDC__ && __STDC__
384
 
static const char *_getopt_initialize (int, char *const *, const char *);
385
 
#endif
386
252
static const char *
387
 
_getopt_initialize (argc, argv, optstring)
388
 
     int argc;
389
 
     char *const *argv;
390
 
     const char *optstring;
 
253
_getopt_initialize (int argc, char **argv, const char *optstring,
 
254
                    int posixly_correct, struct _getopt_data *d)
391
255
{
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.  */
395
259
 
396
 
  first_nonopt = last_nonopt = optind;
397
 
 
398
 
  nextchar = NULL;
399
 
 
400
 
  posixly_correct = getenv ("POSIXLY_CORRECT");
 
260
  d->__first_nonopt = d->__last_nonopt = d->optind;
 
261
 
 
262
  d->__nextchar = NULL;
 
263
 
 
264
  d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
401
265
 
402
266
  /* Determine how to handle the ordering of options and nonoptions.  */
403
267
 
404
268
  if (optstring[0] == '-')
405
269
    {
406
 
      ordering = RETURN_IN_ORDER;
 
270
      d->__ordering = RETURN_IN_ORDER;
407
271
      ++optstring;
408
272
    }
409
273
  else if (optstring[0] == '+')
410
274
    {
411
 
      ordering = REQUIRE_ORDER;
 
275
      d->__ordering = REQUIRE_ORDER;
412
276
      ++optstring;
413
277
    }
414
 
  else if (posixly_correct != NULL)
415
 
    ordering = REQUIRE_ORDER;
 
278
  else if (d->__posixly_correct)
 
279
    d->__ordering = REQUIRE_ORDER;
416
280
  else
417
 
    ordering = PERMUTE;
 
281
    d->__ordering = PERMUTE;
418
282
 
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)
422
286
    {
423
 
      if (nonoption_flags_max_len == 0)
 
287
      if (d->__nonoption_flags_max_len == 0)
424
288
        {
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;
428
292
          else
429
293
            {
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;
438
302
              else
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);
441
305
            }
442
306
        }
443
 
      nonoption_flags_len = nonoption_flags_max_len;
 
307
      d->__nonoption_flags_len = d->__nonoption_flags_max_len;
444
308
    }
445
309
  else
446
 
    nonoption_flags_len = 0;
 
310
    d->__nonoption_flags_len = 0;
447
311
#endif
448
312
 
449
313
  return optstring;
491
355
   `flag' field is nonzero, the value of the option's `val' field
492
356
   if the `flag' field is zero.
493
357
 
494
 
   The elements of ARGV aren't really const, because we permute them.
495
 
   But we pretend they're const in the prototype to be compatible
496
 
   with other systems.
497
 
 
498
358
   LONGOPTS is a vector of `struct option' terminated by an
499
359
   element containing a name which is zero.
500
360
 
503
363
   recent call.
504
364
 
505
365
   If LONG_ONLY is nonzero, '-' as well as '--' can introduce
506
 
   long-named options.  */
 
366
   long-named options.
 
367
 
 
368
   If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
 
369
   environment variable were set.  */
507
370
 
508
371
int
509
 
_getopt_internal (argc, argv, optstring, longopts, longind, long_only)
510
 
     int argc;
511
 
     char *const *argv;
512
 
     const char *optstring;
513
 
     const struct option *longopts;
514
 
     int *longind;
515
 
     int long_only;
 
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)
516
375
{
517
 
  int print_errors = opterr;
 
376
  int print_errors = d->opterr;
518
377
  if (optstring[0] == ':')
519
378
    print_errors = 0;
520
379
 
521
380
  if (argc < 1)
522
381
    return -1;
523
382
 
524
 
  optarg = NULL;
 
383
  d->optarg = NULL;
525
384
 
526
 
  if (optind == 0 || !__getopt_initialized)
 
385
  if (d->optind == 0 || !d->__initialized)
527
386
    {
528
 
      if (optind == 0)
529
 
        optind = 1;     /* Don't scan ARGV[0], the program name.  */
530
 
      optstring = _getopt_initialize (argc, argv, optstring);
531
 
      __getopt_initialized = 1;
 
387
      if (d->optind == 0)
 
388
        d->optind = 1;  /* Don't scan ARGV[0], the program name.  */
 
389
      optstring = _getopt_initialize (argc, argv, optstring,
 
390
                                      posixly_correct, d);
 
391
      d->__initialized = 1;
532
392
    }
533
393
 
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'))
542
402
#else
543
 
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
 
403
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
544
404
#endif
545
405
 
546
 
  if (nextchar == NULL || *nextchar == '\0')
 
406
  if (d->__nextchar == NULL || *d->__nextchar == '\0')
547
407
    {
548
408
      /* Advance to the next ARGV-element.  */
549
409
 
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;
556
416
 
557
 
      if (ordering == PERMUTE)
 
417
      if (d->__ordering == PERMUTE)
558
418
        {
559
419
          /* If we have just processed some options following some non-options,
560
420
             exchange them so that the options come first.  */
561
421
 
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;
566
427
 
567
428
          /* Skip any additional non-options
568
429
             and extend the range of non-options previously skipped.  */
569
430
 
570
 
          while (optind < argc && NONOPTION_P)
571
 
            optind++;
572
 
          last_nonopt = optind;
 
431
          while (d->optind < argc && NONOPTION_P)
 
432
            d->optind++;
 
433
          d->__last_nonopt = d->optind;
573
434
        }
574
435
 
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.  */
579
440
 
580
 
      if (optind != argc && !strcmp (argv[optind], "--"))
 
441
      if (d->optind != argc && !strcmp (argv[d->optind], "--"))
581
442
        {
582
 
          optind++;
583
 
 
584
 
          if (first_nonopt != last_nonopt && last_nonopt != optind)
585
 
            exchange ((char **) argv);
586
 
          else if (first_nonopt == last_nonopt)
587
 
            first_nonopt = optind;
588
 
          last_nonopt = argc;
589
 
 
590
 
          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;
591
453
        }
592
454
 
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.  */
595
457
 
596
 
      if (optind == argc)
 
458
      if (d->optind == argc)
597
459
        {
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;
602
464
          return -1;
603
465
        }
604
466
 
607
469
 
608
470
      if (NONOPTION_P)
609
471
        {
610
 
          if (ordering == REQUIRE_ORDER)
 
472
          if (d->__ordering == REQUIRE_ORDER)
611
473
            return -1;
612
 
          optarg = argv[optind++];
 
474
          d->optarg = argv[d->optind++];
613
475
          return 1;
614
476
        }
615
477
 
616
478
      /* We have found another option-ARGV-element.
617
479
         Skip the initial punctuation.  */
618
480
 
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] == '-'));
621
483
    }
622
484
 
623
485
  /* Decode the current option-ARGV-element.  */
636
498
     This distinction seems to be the most useful approach.  */
637
499
 
638
500
  if (longopts != NULL
639
 
      && (argv[optind][1] == '-'
640
 
          || (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])))))
641
504
    {
642
505
      char *nameend;
643
506
      const struct option *p;
647
510
      int indfound = -1;
648
511
      int option_index;
649
512
 
650
 
      for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
 
513
      for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
651
514
        /* Do nothing.  */ ;
652
515
 
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))
657
520
          {
658
 
            if ((unsigned int) (nameend - nextchar)
 
521
            if ((unsigned int) (nameend - d->__nextchar)
659
522
                == (unsigned int) strlen (p->name))
660
523
              {
661
524
                /* Exact match found.  */
686
549
              char *buf;
687
550
 
688
551
              if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
689
 
                              argv[0], argv[optind]) >= 0)
 
552
                              argv[0], argv[d->optind]) >= 0)
690
553
                {
 
554
                  _IO_flockfile (stderr);
 
555
 
 
556
                  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
557
                  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
691
558
 
692
559
                  if (_IO_fwide (stderr, 0) > 0)
693
560
                    __fwprintf (stderr, L"%s", buf);
694
561
                  else
695
562
                    fputs (buf, stderr);
696
563
 
 
564
                  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
565
                  _IO_funlockfile (stderr);
 
566
 
697
567
                  free (buf);
698
568
                }
699
569
#else
700
570
              fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
701
 
                       argv[0], argv[optind]);
 
571
                       argv[0], argv[d->optind]);
702
572
#endif
703
573
            }
704
 
          nextchar += strlen (nextchar);
705
 
          optind++;
706
 
          optopt = 0;
 
574
          d->__nextchar += strlen (d->__nextchar);
 
575
          d->optind++;
 
576
          d->optopt = 0;
707
577
          return '?';
708
578
        }
709
579
 
710
580
      if (pfound != NULL)
711
581
        {
712
582
          option_index = indfound;
713
 
          optind++;
 
583
          d->optind++;
714
584
          if (*nameend)
715
585
            {
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;
720
590
              else
721
591
                {
722
592
                  if (print_errors)
726
596
                      int n;
727
597
#endif
728
598
 
729
 
                      if (argv[optind - 1][1] == '-')
 
599
                      if (argv[d->optind - 1][1] == '-')
730
600
                        {
731
601
                          /* --option */
732
602
#if defined _LIBC && defined USE_IN_LIBIO
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],
749
619
                                          pfound->name);
750
620
#else
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],
 
624
                                   pfound->name);
754
625
#endif
755
626
                        }
756
627
 
757
628
#if defined _LIBC && defined USE_IN_LIBIO
758
629
                      if (n >= 0)
759
630
                        {
 
631
                          _IO_flockfile (stderr);
 
632
 
 
633
                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
634
                          ((_IO_FILE *) stderr)->_flags2
 
635
                            |= _IO_FLAGS2_NOTCANCEL;
 
636
 
760
637
                          if (_IO_fwide (stderr, 0) > 0)
761
638
                            __fwprintf (stderr, L"%s", buf);
762
639
                          else
763
640
                            fputs (buf, stderr);
764
641
 
 
642
                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
643
                          _IO_funlockfile (stderr);
 
644
 
765
645
                          free (buf);
766
646
                        }
767
647
#endif
768
648
                    }
769
649
 
770
 
                  nextchar += strlen (nextchar);
 
650
                  d->__nextchar += strlen (d->__nextchar);
771
651
 
772
 
                  optopt = pfound->val;
 
652
                  d->optopt = pfound->val;
773
653
                  return '?';
774
654
                }
775
655
            }
776
656
          else if (pfound->has_arg == 1)
777
657
            {
778
 
              if (optind < argc)
779
 
                optarg = argv[optind++];
 
658
              if (d->optind < argc)
 
659
                d->optarg = argv[d->optind++];
780
660
              else
781
661
                {
782
662
                  if (print_errors)
786
666
 
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)
790
670
                        {
 
671
                          _IO_flockfile (stderr);
 
672
 
 
673
                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
674
                          ((_IO_FILE *) stderr)->_flags2
 
675
                            |= _IO_FLAGS2_NOTCANCEL;
 
676
 
791
677
                          if (_IO_fwide (stderr, 0) > 0)
792
678
                            __fwprintf (stderr, L"%s", buf);
793
679
                          else
794
680
                            fputs (buf, stderr);
795
681
 
 
682
                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
683
                          _IO_funlockfile (stderr);
 
684
 
796
685
                          free (buf);
797
686
                        }
798
687
#else
799
688
                      fprintf (stderr,
800
689
                               _("%s: option `%s' requires an argument\n"),
801
 
                               argv[0], argv[optind - 1]);
 
690
                               argv[0], argv[d->optind - 1]);
802
691
#endif
803
692
                    }
804
 
                  nextchar += strlen (nextchar);
805
 
                  optopt = pfound->val;
 
693
                  d->__nextchar += strlen (d->__nextchar);
 
694
                  d->optopt = pfound->val;
806
695
                  return optstring[0] == ':' ? ':' : '?';
807
696
                }
808
697
            }
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)
821
710
         or the option starts with '--' or is not a valid short
822
711
         option, then it's an error.
823
712
         Otherwise interpret it as a short option.  */
824
 
      if (!long_only || argv[optind][1] == '-'
825
 
          || my_index (optstring, *nextchar) == NULL)
 
713
      if (!long_only || argv[d->optind][1] == '-'
 
714
          || strchr (optstring, *d->__nextchar) == NULL)
826
715
        {
827
716
          if (print_errors)
828
717
            {
831
720
              int n;
832
721
#endif
833
722
 
834
 
              if (argv[optind][1] == '-')
 
723
              if (argv[d->optind][1] == '-')
835
724
                {
836
725
                  /* --option */
837
726
#if defined _LIBC && defined USE_IN_LIBIO
838
727
                  n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
839
 
                                  argv[0], nextchar);
 
728
                                  argv[0], d->__nextchar);
840
729
#else
841
730
                  fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
842
 
                           argv[0], nextchar);
 
731
                           argv[0], d->__nextchar);
843
732
#endif
844
733
                }
845
734
              else
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);
851
740
#else
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);
854
743
#endif
855
744
                }
856
745
 
857
746
#if defined _LIBC && defined USE_IN_LIBIO
858
747
              if (n >= 0)
859
748
                {
 
749
                  _IO_flockfile (stderr);
 
750
 
 
751
                  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
752
                  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
753
 
860
754
                  if (_IO_fwide (stderr, 0) > 0)
861
755
                    __fwprintf (stderr, L"%s", buf);
862
756
                  else
863
757
                    fputs (buf, stderr);
864
758
 
 
759
                  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
760
                  _IO_funlockfile (stderr);
 
761
 
865
762
                  free (buf);
866
763
                }
867
764
#endif
868
765
            }
869
 
          nextchar = (char *) "";
870
 
          optind++;
871
 
          optopt = 0;
 
766
          d->__nextchar = (char *) "";
 
767
          d->optind++;
 
768
          d->optopt = 0;
872
769
          return '?';
873
770
        }
874
771
    }
876
773
  /* Look at and handle the next short option-character.  */
877
774
 
878
775
  {
879
 
    char c = *nextchar++;
880
 
    char *temp = my_index (optstring, c);
 
776
    char c = *d->__nextchar++;
 
777
    char *temp = strchr (optstring, c);
881
778
 
882
779
    /* Increment `optind' when we start to process its last character.  */
883
 
    if (*nextchar == '\0')
884
 
      ++optind;
 
780
    if (*d->__nextchar == '\0')
 
781
      ++d->optind;
885
782
 
886
783
    if (temp == NULL || c == ':')
887
784
      {
892
789
              int n;
893
790
#endif
894
791
 
895
 
            if (posixly_correct)
 
792
            if (d->__posixly_correct)
896
793
              {
897
794
                /* 1003.2 specifies the format of this message.  */
898
795
#if defined _LIBC && defined USE_IN_LIBIO
915
812
#if defined _LIBC && defined USE_IN_LIBIO
916
813
            if (n >= 0)
917
814
              {
 
815
                _IO_flockfile (stderr);
 
816
 
 
817
                int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
818
                ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
819
 
918
820
                if (_IO_fwide (stderr, 0) > 0)
919
821
                  __fwprintf (stderr, L"%s", buf);
920
822
                else
921
823
                  fputs (buf, stderr);
922
824
 
 
825
                ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
826
                _IO_funlockfile (stderr);
 
827
 
923
828
                free (buf);
924
829
              }
925
830
#endif
926
831
          }
927
 
        optopt = c;
 
832
        d->optopt = c;
928
833
        return '?';
929
834
      }
930
835
    /* Convenience. Treat POSIX -W foo same as long option --foo */
939
844
        int option_index;
940
845
 
941
846
        /* This is an option that requires an argument.  */
942
 
        if (*nextchar != '\0')
 
847
        if (*d->__nextchar != '\0')
943
848
          {
944
 
            optarg = nextchar;
 
849
            d->optarg = d->__nextchar;
945
850
            /* If we end this ARGV-element by taking the rest as an arg,
946
851
               we must advance to the next element now.  */
947
 
            optind++;
 
852
            d->optind++;
948
853
          }
949
 
        else if (optind == argc)
 
854
        else if (d->optind == argc)
950
855
          {
951
856
            if (print_errors)
952
857
              {
958
863
                                _("%s: option requires an argument -- %c\n"),
959
864
                                argv[0], c) >= 0)
960
865
                  {
 
866
                    _IO_flockfile (stderr);
 
867
 
 
868
                    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
869
                    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
870
 
961
871
                    if (_IO_fwide (stderr, 0) > 0)
962
872
                      __fwprintf (stderr, L"%s", buf);
963
873
                    else
964
874
                      fputs (buf, stderr);
965
875
 
 
876
                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
877
                    _IO_funlockfile (stderr);
 
878
 
966
879
                    free (buf);
967
880
                  }
968
881
#else
970
883
                         argv[0], c);
971
884
#endif
972
885
              }
973
 
            optopt = c;
 
886
            d->optopt = c;
974
887
            if (optstring[0] == ':')
975
888
              c = ':';
976
889
            else
978
891
            return c;
979
892
          }
980
893
        else
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++];
984
897
 
985
898
        /* optarg is now the argument, see if it's in the
986
899
           table of longopts.  */
987
900
 
988
 
        for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
 
901
        for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
 
902
             nameend++)
989
903
          /* Do nothing.  */ ;
990
904
 
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))
995
909
            {
996
 
              if ((unsigned int) (nameend - nextchar) == strlen (p->name))
 
910
              if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
997
911
                {
998
912
                  /* Exact match found.  */
999
913
                  pfound = p;
1019
933
                char *buf;
1020
934
 
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)
1023
937
                  {
 
938
                    _IO_flockfile (stderr);
 
939
 
 
940
                    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
941
                    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
942
 
1024
943
                    if (_IO_fwide (stderr, 0) > 0)
1025
944
                      __fwprintf (stderr, L"%s", buf);
1026
945
                    else
1027
946
                      fputs (buf, stderr);
1028
947
 
 
948
                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
949
                    _IO_funlockfile (stderr);
 
950
 
1029
951
                    free (buf);
1030
952
                  }
1031
953
#else
1032
954
                fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
1033
 
                         argv[0], argv[optind]);
 
955
                         argv[0], argv[d->optind]);
1034
956
#endif
1035
957
              }
1036
 
            nextchar += strlen (nextchar);
1037
 
            optind++;
 
958
            d->__nextchar += strlen (d->__nextchar);
 
959
            d->optind++;
1038
960
            return '?';
1039
961
          }
1040
962
        if (pfound != NULL)
1045
967
                /* Don't test has_arg with >, because some C compilers don't
1046
968
                   allow it to be used on enums.  */
1047
969
                if (pfound->has_arg)
1048
 
                  optarg = nameend + 1;
 
970
                  d->optarg = nameend + 1;
1049
971
                else
1050
972
                  {
1051
973
                    if (print_errors)
1057
979
%s: option `-W %s' doesn't allow an argument\n"),
1058
980
                                        argv[0], pfound->name) >= 0)
1059
981
                          {
 
982
                            _IO_flockfile (stderr);
 
983
 
 
984
                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
985
                            ((_IO_FILE *) stderr)->_flags2
 
986
                              |= _IO_FLAGS2_NOTCANCEL;
 
987
 
1060
988
                            if (_IO_fwide (stderr, 0) > 0)
1061
989
                              __fwprintf (stderr, L"%s", buf);
1062
990
                            else
1063
991
                              fputs (buf, stderr);
1064
992
 
 
993
                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
994
                            _IO_funlockfile (stderr);
 
995
 
1065
996
                            free (buf);
1066
997
                          }
1067
998
#else
1071
1002
#endif
1072
1003
                      }
1073
1004
 
1074
 
                    nextchar += strlen (nextchar);
 
1005
                    d->__nextchar += strlen (d->__nextchar);
1075
1006
                    return '?';
1076
1007
                  }
1077
1008
              }
1078
1009
            else if (pfound->has_arg == 1)
1079
1010
              {
1080
 
                if (optind < argc)
1081
 
                  optarg = argv[optind++];
 
1011
                if (d->optind < argc)
 
1012
                  d->optarg = argv[d->optind++];
1082
1013
                else
1083
1014
                  {
1084
1015
                    if (print_errors)
1088
1019
 
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)
1092
1023
                          {
 
1024
                            _IO_flockfile (stderr);
 
1025
 
 
1026
                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
1027
                            ((_IO_FILE *) stderr)->_flags2
 
1028
                              |= _IO_FLAGS2_NOTCANCEL;
 
1029
 
1093
1030
                            if (_IO_fwide (stderr, 0) > 0)
1094
1031
                              __fwprintf (stderr, L"%s", buf);
1095
1032
                            else
1096
1033
                              fputs (buf, stderr);
1097
1034
 
 
1035
                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
1036
                            _IO_funlockfile (stderr);
 
1037
 
1098
1038
                            free (buf);
1099
1039
                          }
1100
1040
#else
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]);
1104
1044
#endif
1105
1045
                      }
1106
 
                    nextchar += strlen (nextchar);
 
1046
                    d->__nextchar += strlen (d->__nextchar);
1107
1047
                    return optstring[0] == ':' ? ':' : '?';
1108
1048
                  }
1109
1049
              }
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)
1117
1057
              }
1118
1058
            return pfound->val;
1119
1059
          }
1120
 
          nextchar = NULL;
 
1060
          d->__nextchar = NULL;
1121
1061
          return 'W';   /* Let the application handle it.   */
1122
1062
      }
1123
1063
    if (temp[1] == ':')
1125
1065
        if (temp[2] == ':')
1126
1066
          {
1127
1067
            /* This is an option that accepts an argument optionally.  */
1128
 
            if (*nextchar != '\0')
 
1068
            if (*d->__nextchar != '\0')
1129
1069
              {
1130
 
                optarg = nextchar;
1131
 
                optind++;
 
1070
                d->optarg = d->__nextchar;
 
1071
                d->optind++;
1132
1072
              }
1133
1073
            else
1134
 
              optarg = NULL;
1135
 
            nextchar = NULL;
 
1074
              d->optarg = NULL;
 
1075
            d->__nextchar = NULL;
1136
1076
          }
1137
1077
        else
1138
1078
          {
1139
1079
            /* This is an option that requires an argument.  */
1140
 
            if (*nextchar != '\0')
 
1080
            if (*d->__nextchar != '\0')
1141
1081
              {
1142
 
                optarg = nextchar;
 
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.  */
1145
 
                optind++;
 
1085
                d->optind++;
1146
1086
              }
1147
 
            else if (optind == argc)
 
1087
            else if (d->optind == argc)
1148
1088
              {
1149
1089
                if (print_errors)
1150
1090
                  {
1156
1096
%s: option requires an argument -- %c\n"),
1157
1097
                                    argv[0], c) >= 0)
1158
1098
                      {
 
1099
                        _IO_flockfile (stderr);
 
1100
 
 
1101
                        int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
1102
                        ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
1103
 
1159
1104
                        if (_IO_fwide (stderr, 0) > 0)
1160
1105
                          __fwprintf (stderr, L"%s", buf);
1161
1106
                        else
1162
1107
                          fputs (buf, stderr);
1163
1108
 
 
1109
                        ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
1110
                        _IO_funlockfile (stderr);
 
1111
 
1164
1112
                        free (buf);
1165
1113
                      }
1166
1114
#else
1169
1117
                             argv[0], c);
1170
1118
#endif
1171
1119
                  }
1172
 
                optopt = c;
 
1120
                d->optopt = c;
1173
1121
                if (optstring[0] == ':')
1174
1122
                  c = ':';
1175
1123
                else
1178
1126
            else
1179
1127
              /* We already incremented `optind' once;
1180
1128
                 increment it again when taking next ARGV-elt as argument.  */
1181
 
              optarg = argv[optind++];
1182
 
            nextchar = NULL;
 
1129
              d->optarg = argv[d->optind++];
 
1130
            d->__nextchar = NULL;
1183
1131
          }
1184
1132
      }
1185
1133
    return c;
1187
1135
}
1188
1136
 
1189
1137
int
1190
 
getopt (argc, argv, optstring)
1191
 
     int argc;
1192
 
     char *const *argv;
1193
 
     const char *optstring;
1194
 
{
1195
 
  return _getopt_internal (argc, argv, optstring,
1196
 
                           (const struct option *) 0,
1197
 
                           (int *) 0,
1198
 
                           0);
1199
 
}
1200
 
 
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)
 
1141
{
 
1142
  int result;
 
1143
 
 
1144
  getopt_data.optind = optind;
 
1145
  getopt_data.opterr = opterr;
 
1146
 
 
1147
  result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
 
1148
                               long_only, posixly_correct, &getopt_data);
 
1149
 
 
1150
  optind = getopt_data.optind;
 
1151
  optarg = getopt_data.optarg;
 
1152
  optopt = getopt_data.optopt;
 
1153
 
 
1154
  return result;
 
1155
}
 
1156
 
 
1157
/* glibc gets a LSB-compliant getopt.
 
1158
   Standalone applications get a POSIX-compliant getopt.  */
 
1159
#if _LIBC
 
1160
enum { POSIXLY_CORRECT = 0 };
 
1161
#else
 
1162
enum { POSIXLY_CORRECT = 1 };
 
1163
#endif
 
1164
 
 
1165
int
 
1166
getopt (int argc, char *const *argv, const char *optstring)
 
1167
{
 
1168
  return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
 
1169
                           POSIXLY_CORRECT);
 
1170
}
 
1171
 
1202
1172
 
1203
1173
#ifdef TEST
1204
1174
 
1206
1176
   the above definition of `getopt'.  */
1207
1177
 
1208
1178
int
1209
 
main (argc, argv)
1210
 
     int argc;
1211
 
     char **argv;
 
1179
main (int argc, char **argv)
1212
1180
{
1213
1181
  int c;
1214
1182
  int digit_optind = 0;