~ubuntu-branches/ubuntu/vivid/gzip/vivid

« back to all changes in this revision

Viewing changes to lib/getopt.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-10-19 11:42:42 UTC
  • mfrom: (4.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20111019114242-d8wiiu8kbvdtgmgj
Tags: 1.4-1ubuntu1
* Merge with Debian testing.  Remaining Ubuntu changes:
  - debian/{control,rules}: Remove the Win32 build and mingw64
    build-dependency, since mingw is in universe, and will remain so for
    the forseeable future.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Getopt for GNU.
2
 
   NOTE: getopt is now part of the C library, so if you don't know what
 
2
   NOTE: getopt is 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,2003,2004,2006
6
 
        Free Software Foundation, Inc.
 
5
   Copyright (C) 1987-1996, 1998-2004, 2006, 2008-2010 Free Software
 
6
   Foundation, Inc.
7
7
   This file is part of the GNU C Library.
8
8
 
9
 
   This program is free software; you can redistribute it and/or modify
 
9
   This program is free software: you can redistribute it and/or modify
10
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.
 
11
   the Free Software Foundation; either version 3 of the License, or
 
12
   (at your option) any later version.
13
13
 
14
14
   This program is distributed in the hope that it will be useful,
15
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
17
   GNU General Public License for more details.
18
18
 
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,
21
 
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
19
   You should have received a copy of the GNU General Public License
 
20
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
21
 
23
22
#ifndef _LIBC
24
23
# include <config.h>
31
30
#include <string.h>
32
31
#include <unistd.h>
33
32
 
34
 
#ifdef __VMS
35
 
# include <unixlib.h>
36
 
#endif
37
 
 
38
33
#ifdef _LIBC
39
34
# include <libintl.h>
40
35
#else
50
45
# define attribute_hidden
51
46
#endif
52
47
 
53
 
/* Unlike standard Unix `getopt', functions like `getopt_long'
54
 
   let the user intersperse the options with the other arguments.
 
48
/* This version of `getopt' appears to the caller like standard Unix `getopt'
 
49
   but it behaves differently for the user, since it allows the user
 
50
   to intersperse the options with the other arguments.
55
51
 
56
52
   As `getopt_long' works, it permutes the elements of ARGV so that,
57
53
   when it is done, all the options precede everything else.  Thus
59
55
 
60
56
   Using `getopt' or setting the environment variable POSIXLY_CORRECT
61
57
   disables permutation.
62
 
   Then the application's behavior is completely standard.
 
58
   Then the behavior is completely standard.
63
59
 
64
60
   GNU application programs can use a third alternative mode in which
65
61
   they can distinguish the relative order of options and other arguments.  */
126
122
 
127
123
# ifdef USE_NONOPTION_FLAGS
128
124
#  define SWAP_FLAGS(ch1, ch2) \
129
 
  if (d->__nonoption_flags_len > 0)                                           \
130
 
    {                                                                         \
131
 
      char __tmp = __getopt_nonoption_flags[ch1];                             \
132
 
      __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];          \
133
 
      __getopt_nonoption_flags[ch2] = __tmp;                                  \
 
125
  if (d->__nonoption_flags_len > 0)                                           \
 
126
    {                                                                         \
 
127
      char __tmp = __getopt_nonoption_flags[ch1];                             \
 
128
      __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];          \
 
129
      __getopt_nonoption_flags[ch2] = __tmp;                                  \
134
130
    }
135
131
# else
136
132
#  define SWAP_FLAGS(ch1, ch2)
137
133
# endif
138
 
#else   /* !_LIBC */
 
134
#else   /* !_LIBC */
139
135
# define SWAP_FLAGS(ch1, ch2)
140
 
#endif  /* _LIBC */
 
136
#endif  /* _LIBC */
141
137
 
142
138
/* Exchange two adjacent subsequences of ARGV.
143
139
   One subsequence is elements [first_nonopt,last_nonopt)
168
164
  if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
169
165
    {
170
166
      /* We must extend the array.  The user plays games with us and
171
 
         presents new arguments.  */
 
167
         presents new arguments.  */
172
168
      char *new_str = malloc (top + 1);
173
169
      if (new_str == NULL)
174
 
        d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
 
170
        d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
175
171
      else
176
 
        {
177
 
          memset (__mempcpy (new_str, __getopt_nonoption_flags,
178
 
                             d->__nonoption_flags_max_len),
179
 
                  '\0', top + 1 - d->__nonoption_flags_max_len);
180
 
          d->__nonoption_flags_max_len = top + 1;
181
 
          __getopt_nonoption_flags = new_str;
182
 
        }
 
172
        {
 
173
          memset (__mempcpy (new_str, __getopt_nonoption_flags,
 
174
                             d->__nonoption_flags_max_len),
 
175
                  '\0', top + 1 - d->__nonoption_flags_max_len);
 
176
          d->__nonoption_flags_max_len = top + 1;
 
177
          __getopt_nonoption_flags = new_str;
 
178
        }
183
179
    }
184
180
#endif
185
181
 
186
182
  while (top > middle && middle > bottom)
187
183
    {
188
184
      if (top - middle > middle - bottom)
189
 
        {
190
 
          /* Bottom segment is the short one.  */
191
 
          int len = middle - bottom;
192
 
          register int i;
 
185
        {
 
186
          /* Bottom segment is the short one.  */
 
187
          int len = middle - bottom;
 
188
          register int i;
193
189
 
194
 
          /* Swap it with the top part of the top segment.  */
195
 
          for (i = 0; i < len; i++)
196
 
            {
197
 
              tem = argv[bottom + i];
198
 
              argv[bottom + i] = argv[top - (middle - bottom) + i];
199
 
              argv[top - (middle - bottom) + i] = tem;
200
 
              SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
201
 
            }
202
 
          /* Exclude the moved bottom segment from further swapping.  */
203
 
          top -= len;
204
 
        }
 
190
          /* Swap it with the top part of the top segment.  */
 
191
          for (i = 0; i < len; i++)
 
192
            {
 
193
              tem = argv[bottom + i];
 
194
              argv[bottom + i] = argv[top - (middle - bottom) + i];
 
195
              argv[top - (middle - bottom) + i] = tem;
 
196
              SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
 
197
            }
 
198
          /* Exclude the moved bottom segment from further swapping.  */
 
199
          top -= len;
 
200
        }
205
201
      else
206
 
        {
207
 
          /* Top segment is the short one.  */
208
 
          int len = top - middle;
209
 
          register int i;
 
202
        {
 
203
          /* Top segment is the short one.  */
 
204
          int len = top - middle;
 
205
          register int i;
210
206
 
211
 
          /* Swap it with the bottom part of the bottom segment.  */
212
 
          for (i = 0; i < len; i++)
213
 
            {
214
 
              tem = argv[bottom + i];
215
 
              argv[bottom + i] = argv[middle + i];
216
 
              argv[middle + i] = tem;
217
 
              SWAP_FLAGS (bottom + i, middle + i);
218
 
            }
219
 
          /* Exclude the moved top segment from further swapping.  */
220
 
          bottom += len;
221
 
        }
 
207
          /* Swap it with the bottom part of the bottom segment.  */
 
208
          for (i = 0; i < len; i++)
 
209
            {
 
210
              tem = argv[bottom + i];
 
211
              argv[bottom + i] = argv[middle + i];
 
212
              argv[middle + i] = tem;
 
213
              SWAP_FLAGS (bottom + i, middle + i);
 
214
            }
 
215
          /* Exclude the moved top segment from further swapping.  */
 
216
          bottom += len;
 
217
        }
222
218
    }
223
219
 
224
220
  /* Update records for the slots the non-options now occupy.  */
230
226
/* Initialize the internal data when the first call is made.  */
231
227
 
232
228
static const char *
233
 
_getopt_initialize (int argc, char **argv, const char *optstring,
234
 
                    int posixly_correct, struct _getopt_data *d)
 
229
_getopt_initialize (int argc _GL_UNUSED,
 
230
                    char **argv _GL_UNUSED, const char *optstring,
 
231
                    struct _getopt_data *d, int posixly_correct)
235
232
{
236
233
  /* Start processing options with ARGV-element 1 (since ARGV-element 0
237
234
     is the program name); the sequence of previously skipped
265
262
      && argc == __libc_argc && argv == __libc_argv)
266
263
    {
267
264
      if (d->__nonoption_flags_max_len == 0)
268
 
        {
269
 
          if (__getopt_nonoption_flags == NULL
270
 
              || __getopt_nonoption_flags[0] == '\0')
271
 
            d->__nonoption_flags_max_len = -1;
272
 
          else
273
 
            {
274
 
              const char *orig_str = __getopt_nonoption_flags;
275
 
              int len = d->__nonoption_flags_max_len = strlen (orig_str);
276
 
              if (d->__nonoption_flags_max_len < argc)
277
 
                d->__nonoption_flags_max_len = argc;
278
 
              __getopt_nonoption_flags =
279
 
                (char *) malloc (d->__nonoption_flags_max_len);
280
 
              if (__getopt_nonoption_flags == NULL)
281
 
                d->__nonoption_flags_max_len = -1;
282
 
              else
283
 
                memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
284
 
                        '\0', d->__nonoption_flags_max_len - len);
285
 
            }
286
 
        }
 
265
        {
 
266
          if (__getopt_nonoption_flags == NULL
 
267
              || __getopt_nonoption_flags[0] == '\0')
 
268
            d->__nonoption_flags_max_len = -1;
 
269
          else
 
270
            {
 
271
              const char *orig_str = __getopt_nonoption_flags;
 
272
              int len = d->__nonoption_flags_max_len = strlen (orig_str);
 
273
              if (d->__nonoption_flags_max_len < argc)
 
274
                d->__nonoption_flags_max_len = argc;
 
275
              __getopt_nonoption_flags =
 
276
                (char *) malloc (d->__nonoption_flags_max_len);
 
277
              if (__getopt_nonoption_flags == NULL)
 
278
                d->__nonoption_flags_max_len = -1;
 
279
              else
 
280
                memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
 
281
                        '\0', d->__nonoption_flags_max_len - len);
 
282
            }
 
283
        }
287
284
      d->__nonoption_flags_len = d->__nonoption_flags_max_len;
288
285
    }
289
286
  else
335
332
   `flag' field is nonzero, the value of the option's `val' field
336
333
   if the `flag' field is zero.
337
334
 
 
335
   The elements of ARGV aren't really const, because we permute them.
 
336
   But we pretend they're const in the prototype to be compatible
 
337
   with other systems.
 
338
 
338
339
   LONGOPTS is a vector of `struct option' terminated by an
339
340
   element containing a name which is zero.
340
341
 
343
344
   recent call.
344
345
 
345
346
   If LONG_ONLY is nonzero, '-' as well as '--' can introduce
346
 
   long-named options.
347
 
 
348
 
   If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
349
 
   environment variable were set.  */
 
347
   long-named options.  */
350
348
 
351
349
int
352
350
_getopt_internal_r (int argc, char **argv, const char *optstring,
353
 
                    const struct option *longopts, int *longind,
354
 
                    int long_only, int posixly_correct, struct _getopt_data *d)
 
351
                    const struct option *longopts, int *longind,
 
352
                    int long_only, struct _getopt_data *d, int posixly_correct)
355
353
{
356
354
  int print_errors = d->opterr;
357
355
  if (optstring[0] == ':')
365
363
  if (d->optind == 0 || !d->__initialized)
366
364
    {
367
365
      if (d->optind == 0)
368
 
        d->optind = 1;  /* Don't scan ARGV[0], the program name.  */
369
 
      optstring = _getopt_initialize (argc, argv, optstring,
370
 
                                      posixly_correct, d);
 
366
        d->optind = 1;  /* Don't scan ARGV[0], the program name.  */
 
367
      optstring = _getopt_initialize (argc, argv, optstring, d,
 
368
                                      posixly_correct);
371
369
      d->__initialized = 1;
372
370
    }
373
371
 
377
375
     is only used when the used in the GNU libc.  */
378
376
#if defined _LIBC && defined USE_NONOPTION_FLAGS
379
377
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
380
 
                      || (d->optind < d->__nonoption_flags_len                \
381
 
                          && __getopt_nonoption_flags[d->optind] == '1'))
 
378
                      || (d->optind < d->__nonoption_flags_len                \
 
379
                          && __getopt_nonoption_flags[d->optind] == '1'))
382
380
#else
383
381
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
384
382
#endif
388
386
      /* Advance to the next ARGV-element.  */
389
387
 
390
388
      /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
391
 
         moved back by the user (who may also have changed the arguments).  */
 
389
         moved back by the user (who may also have changed the arguments).  */
392
390
      if (d->__last_nonopt > d->optind)
393
 
        d->__last_nonopt = d->optind;
 
391
        d->__last_nonopt = d->optind;
394
392
      if (d->__first_nonopt > d->optind)
395
 
        d->__first_nonopt = d->optind;
 
393
        d->__first_nonopt = d->optind;
396
394
 
397
395
      if (d->__ordering == PERMUTE)
398
 
        {
399
 
          /* If we have just processed some options following some non-options,
400
 
             exchange them so that the options come first.  */
401
 
 
402
 
          if (d->__first_nonopt != d->__last_nonopt
403
 
              && d->__last_nonopt != d->optind)
404
 
            exchange ((char **) argv, d);
405
 
          else if (d->__last_nonopt != d->optind)
406
 
            d->__first_nonopt = d->optind;
407
 
 
408
 
          /* Skip any additional non-options
409
 
             and extend the range of non-options previously skipped.  */
410
 
 
411
 
          while (d->optind < argc && NONOPTION_P)
412
 
            d->optind++;
413
 
          d->__last_nonopt = d->optind;
414
 
        }
 
396
        {
 
397
          /* If we have just processed some options following some non-options,
 
398
             exchange them so that the options come first.  */
 
399
 
 
400
          if (d->__first_nonopt != d->__last_nonopt
 
401
              && d->__last_nonopt != d->optind)
 
402
            exchange ((char **) argv, d);
 
403
          else if (d->__last_nonopt != d->optind)
 
404
            d->__first_nonopt = d->optind;
 
405
 
 
406
          /* Skip any additional non-options
 
407
             and extend the range of non-options previously skipped.  */
 
408
 
 
409
          while (d->optind < argc && NONOPTION_P)
 
410
            d->optind++;
 
411
          d->__last_nonopt = d->optind;
 
412
        }
415
413
 
416
414
      /* The special ARGV-element `--' means premature end of options.
417
 
         Skip it like a null option,
418
 
         then exchange with previous non-options as if it were an option,
419
 
         then skip everything else like a non-option.  */
 
415
         Skip it like a null option,
 
416
         then exchange with previous non-options as if it were an option,
 
417
         then skip everything else like a non-option.  */
420
418
 
421
419
      if (d->optind != argc && !strcmp (argv[d->optind], "--"))
422
 
        {
423
 
          d->optind++;
424
 
 
425
 
          if (d->__first_nonopt != d->__last_nonopt
426
 
              && d->__last_nonopt != d->optind)
427
 
            exchange ((char **) argv, d);
428
 
          else if (d->__first_nonopt == d->__last_nonopt)
429
 
            d->__first_nonopt = d->optind;
430
 
          d->__last_nonopt = argc;
431
 
 
432
 
          d->optind = argc;
433
 
        }
 
420
        {
 
421
          d->optind++;
 
422
 
 
423
          if (d->__first_nonopt != d->__last_nonopt
 
424
              && d->__last_nonopt != d->optind)
 
425
            exchange ((char **) argv, d);
 
426
          else if (d->__first_nonopt == d->__last_nonopt)
 
427
            d->__first_nonopt = d->optind;
 
428
          d->__last_nonopt = argc;
 
429
 
 
430
          d->optind = argc;
 
431
        }
434
432
 
435
433
      /* If we have done all the ARGV-elements, stop the scan
436
 
         and back over any non-options that we skipped and permuted.  */
 
434
         and back over any non-options that we skipped and permuted.  */
437
435
 
438
436
      if (d->optind == argc)
439
 
        {
440
 
          /* Set the next-arg-index to point at the non-options
441
 
             that we previously skipped, so the caller will digest them.  */
442
 
          if (d->__first_nonopt != d->__last_nonopt)
443
 
            d->optind = d->__first_nonopt;
444
 
          return -1;
445
 
        }
 
437
        {
 
438
          /* Set the next-arg-index to point at the non-options
 
439
             that we previously skipped, so the caller will digest them.  */
 
440
          if (d->__first_nonopt != d->__last_nonopt)
 
441
            d->optind = d->__first_nonopt;
 
442
          return -1;
 
443
        }
446
444
 
447
445
      /* If we have come to a non-option and did not permute it,
448
 
         either stop the scan or describe it to the caller and pass it by.  */
 
446
         either stop the scan or describe it to the caller and pass it by.  */
449
447
 
450
448
      if (NONOPTION_P)
451
 
        {
452
 
          if (d->__ordering == REQUIRE_ORDER)
453
 
            return -1;
454
 
          d->optarg = argv[d->optind++];
455
 
          return 1;
456
 
        }
 
449
        {
 
450
          if (d->__ordering == REQUIRE_ORDER)
 
451
            return -1;
 
452
          d->optarg = argv[d->optind++];
 
453
          return 1;
 
454
        }
457
455
 
458
456
      /* We have found another option-ARGV-element.
459
 
         Skip the initial punctuation.  */
 
457
         Skip the initial punctuation.  */
460
458
 
461
459
      d->__nextchar = (argv[d->optind] + 1
462
 
                  + (longopts != NULL && argv[d->optind][1] == '-'));
 
460
                  + (longopts != NULL && argv[d->optind][1] == '-'));
463
461
    }
464
462
 
465
463
  /* Decode the current option-ARGV-element.  */
479
477
 
480
478
  if (longopts != NULL
481
479
      && (argv[d->optind][1] == '-'
482
 
          || (long_only && (argv[d->optind][2]
483
 
                            || !strchr (optstring, argv[d->optind][1])))))
 
480
          || (long_only && (argv[d->optind][2]
 
481
                            || !strchr (optstring, argv[d->optind][1])))))
484
482
    {
485
483
      char *nameend;
486
484
      const struct option *p;
491
489
      int option_index;
492
490
 
493
491
      for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
494
 
        /* Do nothing.  */ ;
 
492
        /* Do nothing.  */ ;
495
493
 
496
494
      /* Test all long options for either exact match
497
 
         or abbreviated matches.  */
 
495
         or abbreviated matches.  */
498
496
      for (p = longopts, option_index = 0; p->name; p++, option_index++)
499
 
        if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
500
 
          {
501
 
            if ((unsigned int) (nameend - d->__nextchar)
502
 
                == (unsigned int) strlen (p->name))
503
 
              {
504
 
                /* Exact match found.  */
505
 
                pfound = p;
506
 
                indfound = option_index;
507
 
                exact = 1;
508
 
                break;
509
 
              }
510
 
            else if (pfound == NULL)
511
 
              {
512
 
                /* First nonexact match found.  */
513
 
                pfound = p;
514
 
                indfound = option_index;
515
 
              }
516
 
            else if (long_only
517
 
                     || pfound->has_arg != p->has_arg
518
 
                     || pfound->flag != p->flag
519
 
                     || pfound->val != p->val)
520
 
              /* Second or later nonexact match found.  */
521
 
              ambig = 1;
522
 
          }
 
497
        if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
 
498
          {
 
499
            if ((unsigned int) (nameend - d->__nextchar)
 
500
                == (unsigned int) strlen (p->name))
 
501
              {
 
502
                /* Exact match found.  */
 
503
                pfound = p;
 
504
                indfound = option_index;
 
505
                exact = 1;
 
506
                break;
 
507
              }
 
508
            else if (pfound == NULL)
 
509
              {
 
510
                /* First nonexact match found.  */
 
511
                pfound = p;
 
512
                indfound = option_index;
 
513
              }
 
514
            else if (long_only
 
515
                     || pfound->has_arg != p->has_arg
 
516
                     || pfound->flag != p->flag
 
517
                     || pfound->val != p->val)
 
518
              /* Second or later nonexact match found.  */
 
519
              ambig = 1;
 
520
          }
523
521
 
524
522
      if (ambig && !exact)
525
 
        {
526
 
          if (print_errors)
527
 
            {
 
523
        {
 
524
          if (print_errors)
 
525
            {
528
526
#if defined _LIBC && defined USE_IN_LIBIO
529
 
              char *buf;
530
 
 
531
 
              if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
532
 
                              argv[0], argv[d->optind]) >= 0)
533
 
                {
534
 
                  _IO_flockfile (stderr);
535
 
 
536
 
                  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
537
 
                  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
538
 
 
539
 
                  __fxprintf (NULL, "%s", buf);
540
 
 
541
 
                  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
542
 
                  _IO_funlockfile (stderr);
543
 
 
544
 
                  free (buf);
545
 
                }
 
527
              char *buf;
 
528
 
 
529
              if (__asprintf (&buf, _("%s: option '%s' is ambiguous\n"),
 
530
                              argv[0], argv[d->optind]) >= 0)
 
531
                {
 
532
                  _IO_flockfile (stderr);
 
533
 
 
534
                  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
535
                  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
536
 
 
537
                  __fxprintf (NULL, "%s", buf);
 
538
 
 
539
                  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
540
                  _IO_funlockfile (stderr);
 
541
 
 
542
                  free (buf);
 
543
                }
546
544
#else
547
 
              fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
548
 
                       argv[0], argv[d->optind]);
 
545
              fprintf (stderr, _("%s: option '%s' is ambiguous\n"),
 
546
                       argv[0], argv[d->optind]);
549
547
#endif
550
 
            }
551
 
          d->__nextchar += strlen (d->__nextchar);
552
 
          d->optind++;
553
 
          d->optopt = 0;
554
 
          return '?';
555
 
        }
 
548
            }
 
549
          d->__nextchar += strlen (d->__nextchar);
 
550
          d->optind++;
 
551
          d->optopt = 0;
 
552
          return '?';
 
553
        }
556
554
 
557
555
      if (pfound != NULL)
558
 
        {
559
 
          option_index = indfound;
560
 
          d->optind++;
561
 
          if (*nameend)
562
 
            {
563
 
              /* Don't test has_arg with >, because some C compilers don't
564
 
                 allow it to be used on enums.  */
565
 
              if (pfound->has_arg)
566
 
                d->optarg = nameend + 1;
567
 
              else
568
 
                {
569
 
                  if (print_errors)
570
 
                    {
571
 
#if defined _LIBC && defined USE_IN_LIBIO
572
 
                      char *buf;
573
 
                      int n;
574
 
#endif
575
 
 
576
 
                      if (argv[d->optind - 1][1] == '-')
577
 
                        {
578
 
                          /* --option */
579
 
#if defined _LIBC && defined USE_IN_LIBIO
580
 
                          n = __asprintf (&buf, _("\
581
 
%s: option `--%s' doesn't allow an argument\n"),
582
 
                                          argv[0], pfound->name);
583
 
#else
584
 
                          fprintf (stderr, _("\
585
 
%s: option `--%s' doesn't allow an argument\n"),
586
 
                                   argv[0], pfound->name);
587
 
#endif
588
 
                        }
589
 
                      else
590
 
                        {
591
 
                          /* +option or -option */
592
 
#if defined _LIBC && defined USE_IN_LIBIO
593
 
                          n = __asprintf (&buf, _("\
594
 
%s: option `%c%s' doesn't allow an argument\n"),
595
 
                                          argv[0], argv[d->optind - 1][0],
596
 
                                          pfound->name);
597
 
#else
598
 
                          fprintf (stderr, _("\
599
 
%s: option `%c%s' doesn't allow an argument\n"),
600
 
                                   argv[0], argv[d->optind - 1][0],
601
 
                                   pfound->name);
602
 
#endif
603
 
                        }
604
 
 
605
 
#if defined _LIBC && defined USE_IN_LIBIO
606
 
                      if (n >= 0)
607
 
                        {
608
 
                          _IO_flockfile (stderr);
609
 
 
610
 
                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
611
 
                          ((_IO_FILE *) stderr)->_flags2
612
 
                            |= _IO_FLAGS2_NOTCANCEL;
613
 
 
614
 
                          __fxprintf (NULL, "%s", buf);
615
 
 
616
 
                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
617
 
                          _IO_funlockfile (stderr);
618
 
 
619
 
                          free (buf);
620
 
                        }
621
 
#endif
622
 
                    }
623
 
 
624
 
                  d->__nextchar += strlen (d->__nextchar);
625
 
 
626
 
                  d->optopt = pfound->val;
627
 
                  return '?';
628
 
                }
629
 
            }
630
 
          else if (pfound->has_arg == 1)
631
 
            {
632
 
              if (d->optind < argc)
633
 
                d->optarg = argv[d->optind++];
634
 
              else
635
 
                {
636
 
                  if (print_errors)
637
 
                    {
638
 
#if defined _LIBC && defined USE_IN_LIBIO
639
 
                      char *buf;
640
 
 
641
 
                      if (__asprintf (&buf, _("\
642
 
%s: option `%s' requires an argument\n"),
643
 
                                      argv[0], argv[d->optind - 1]) >= 0)
644
 
                        {
645
 
                          _IO_flockfile (stderr);
646
 
 
647
 
                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
648
 
                          ((_IO_FILE *) stderr)->_flags2
649
 
                            |= _IO_FLAGS2_NOTCANCEL;
650
 
 
651
 
                          __fxprintf (NULL, "%s", buf);
652
 
 
653
 
                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
654
 
                          _IO_funlockfile (stderr);
655
 
 
656
 
                          free (buf);
657
 
                        }
658
 
#else
659
 
                      fprintf (stderr,
660
 
                               _("%s: option `%s' requires an argument\n"),
661
 
                               argv[0], argv[d->optind - 1]);
662
 
#endif
663
 
                    }
664
 
                  d->__nextchar += strlen (d->__nextchar);
665
 
                  d->optopt = pfound->val;
666
 
                  return optstring[0] == ':' ? ':' : '?';
667
 
                }
668
 
            }
669
 
          d->__nextchar += strlen (d->__nextchar);
670
 
          if (longind != NULL)
671
 
            *longind = option_index;
672
 
          if (pfound->flag)
673
 
            {
674
 
              *(pfound->flag) = pfound->val;
675
 
              return 0;
676
 
            }
677
 
          return pfound->val;
678
 
        }
 
556
        {
 
557
          option_index = indfound;
 
558
          d->optind++;
 
559
          if (*nameend)
 
560
            {
 
561
              /* Don't test has_arg with >, because some C compilers don't
 
562
                 allow it to be used on enums.  */
 
563
              if (pfound->has_arg)
 
564
                d->optarg = nameend + 1;
 
565
              else
 
566
                {
 
567
                  if (print_errors)
 
568
                    {
 
569
#if defined _LIBC && defined USE_IN_LIBIO
 
570
                      char *buf;
 
571
                      int n;
 
572
#endif
 
573
 
 
574
                      if (argv[d->optind - 1][1] == '-')
 
575
                        {
 
576
                          /* --option */
 
577
#if defined _LIBC && defined USE_IN_LIBIO
 
578
                          n = __asprintf (&buf, _("\
 
579
%s: option '--%s' doesn't allow an argument\n"),
 
580
                                          argv[0], pfound->name);
 
581
#else
 
582
                          fprintf (stderr, _("\
 
583
%s: option '--%s' doesn't allow an argument\n"),
 
584
                                   argv[0], pfound->name);
 
585
#endif
 
586
                        }
 
587
                      else
 
588
                        {
 
589
                          /* +option or -option */
 
590
#if defined _LIBC && defined USE_IN_LIBIO
 
591
                          n = __asprintf (&buf, _("\
 
592
%s: option '%c%s' doesn't allow an argument\n"),
 
593
                                          argv[0], argv[d->optind - 1][0],
 
594
                                          pfound->name);
 
595
#else
 
596
                          fprintf (stderr, _("\
 
597
%s: option '%c%s' doesn't allow an argument\n"),
 
598
                                   argv[0], argv[d->optind - 1][0],
 
599
                                   pfound->name);
 
600
#endif
 
601
                        }
 
602
 
 
603
#if defined _LIBC && defined USE_IN_LIBIO
 
604
                      if (n >= 0)
 
605
                        {
 
606
                          _IO_flockfile (stderr);
 
607
 
 
608
                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
609
                          ((_IO_FILE *) stderr)->_flags2
 
610
                            |= _IO_FLAGS2_NOTCANCEL;
 
611
 
 
612
                          __fxprintf (NULL, "%s", buf);
 
613
 
 
614
                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
615
                          _IO_funlockfile (stderr);
 
616
 
 
617
                          free (buf);
 
618
                        }
 
619
#endif
 
620
                    }
 
621
 
 
622
                  d->__nextchar += strlen (d->__nextchar);
 
623
 
 
624
                  d->optopt = pfound->val;
 
625
                  return '?';
 
626
                }
 
627
            }
 
628
          else if (pfound->has_arg == 1)
 
629
            {
 
630
              if (d->optind < argc)
 
631
                d->optarg = argv[d->optind++];
 
632
              else
 
633
                {
 
634
                  if (print_errors)
 
635
                    {
 
636
#if defined _LIBC && defined USE_IN_LIBIO
 
637
                      char *buf;
 
638
 
 
639
                      if (__asprintf (&buf, _("\
 
640
%s: option '%s' requires an argument\n"),
 
641
                                      argv[0], argv[d->optind - 1]) >= 0)
 
642
                        {
 
643
                          _IO_flockfile (stderr);
 
644
 
 
645
                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
646
                          ((_IO_FILE *) stderr)->_flags2
 
647
                            |= _IO_FLAGS2_NOTCANCEL;
 
648
 
 
649
                          __fxprintf (NULL, "%s", buf);
 
650
 
 
651
                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
652
                          _IO_funlockfile (stderr);
 
653
 
 
654
                          free (buf);
 
655
                        }
 
656
#else
 
657
                      fprintf (stderr,
 
658
                               _("%s: option '%s' requires an argument\n"),
 
659
                               argv[0], argv[d->optind - 1]);
 
660
#endif
 
661
                    }
 
662
                  d->__nextchar += strlen (d->__nextchar);
 
663
                  d->optopt = pfound->val;
 
664
                  return optstring[0] == ':' ? ':' : '?';
 
665
                }
 
666
            }
 
667
          d->__nextchar += strlen (d->__nextchar);
 
668
          if (longind != NULL)
 
669
            *longind = option_index;
 
670
          if (pfound->flag)
 
671
            {
 
672
              *(pfound->flag) = pfound->val;
 
673
              return 0;
 
674
            }
 
675
          return pfound->val;
 
676
        }
679
677
 
680
678
      /* Can't find it as a long option.  If this is not getopt_long_only,
681
 
         or the option starts with '--' or is not a valid short
682
 
         option, then it's an error.
683
 
         Otherwise interpret it as a short option.  */
 
679
         or the option starts with '--' or is not a valid short
 
680
         option, then it's an error.
 
681
         Otherwise interpret it as a short option.  */
684
682
      if (!long_only || argv[d->optind][1] == '-'
685
 
          || strchr (optstring, *d->__nextchar) == NULL)
686
 
        {
687
 
          if (print_errors)
688
 
            {
689
 
#if defined _LIBC && defined USE_IN_LIBIO
690
 
              char *buf;
691
 
              int n;
692
 
#endif
693
 
 
694
 
              if (argv[d->optind][1] == '-')
695
 
                {
696
 
                  /* --option */
697
 
#if defined _LIBC && defined USE_IN_LIBIO
698
 
                  n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
699
 
                                  argv[0], d->__nextchar);
700
 
#else
701
 
                  fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
702
 
                           argv[0], d->__nextchar);
703
 
#endif
704
 
                }
705
 
              else
706
 
                {
707
 
                  /* +option or -option */
708
 
#if defined _LIBC && defined USE_IN_LIBIO
709
 
                  n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
710
 
                                  argv[0], argv[d->optind][0], d->__nextchar);
711
 
#else
712
 
                  fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
713
 
                           argv[0], argv[d->optind][0], d->__nextchar);
714
 
#endif
715
 
                }
716
 
 
717
 
#if defined _LIBC && defined USE_IN_LIBIO
718
 
              if (n >= 0)
719
 
                {
720
 
                  _IO_flockfile (stderr);
721
 
 
722
 
                  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
723
 
                  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
724
 
 
725
 
                  __fxprintf (NULL, "%s", buf);
726
 
 
727
 
                  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
728
 
                  _IO_funlockfile (stderr);
729
 
 
730
 
                  free (buf);
731
 
                }
732
 
#endif
733
 
            }
734
 
          d->__nextchar = (char *) "";
735
 
          d->optind++;
736
 
          d->optopt = 0;
737
 
          return '?';
738
 
        }
 
683
          || strchr (optstring, *d->__nextchar) == NULL)
 
684
        {
 
685
          if (print_errors)
 
686
            {
 
687
#if defined _LIBC && defined USE_IN_LIBIO
 
688
              char *buf;
 
689
              int n;
 
690
#endif
 
691
 
 
692
              if (argv[d->optind][1] == '-')
 
693
                {
 
694
                  /* --option */
 
695
#if defined _LIBC && defined USE_IN_LIBIO
 
696
                  n = __asprintf (&buf, _("%s: unrecognized option '--%s'\n"),
 
697
                                  argv[0], d->__nextchar);
 
698
#else
 
699
                  fprintf (stderr, _("%s: unrecognized option '--%s'\n"),
 
700
                           argv[0], d->__nextchar);
 
701
#endif
 
702
                }
 
703
              else
 
704
                {
 
705
                  /* +option or -option */
 
706
#if defined _LIBC && defined USE_IN_LIBIO
 
707
                  n = __asprintf (&buf, _("%s: unrecognized option '%c%s'\n"),
 
708
                                  argv[0], argv[d->optind][0], d->__nextchar);
 
709
#else
 
710
                  fprintf (stderr, _("%s: unrecognized option '%c%s'\n"),
 
711
                           argv[0], argv[d->optind][0], d->__nextchar);
 
712
#endif
 
713
                }
 
714
 
 
715
#if defined _LIBC && defined USE_IN_LIBIO
 
716
              if (n >= 0)
 
717
                {
 
718
                  _IO_flockfile (stderr);
 
719
 
 
720
                  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
721
                  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
722
 
 
723
                  __fxprintf (NULL, "%s", buf);
 
724
 
 
725
                  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
726
                  _IO_funlockfile (stderr);
 
727
 
 
728
                  free (buf);
 
729
                }
 
730
#endif
 
731
            }
 
732
          d->__nextchar = (char *) "";
 
733
          d->optind++;
 
734
          d->optopt = 0;
 
735
          return '?';
 
736
        }
739
737
    }
740
738
 
741
739
  /* Look at and handle the next short option-character.  */
750
748
 
751
749
    if (temp == NULL || c == ':')
752
750
      {
753
 
        if (print_errors)
754
 
          {
755
 
#if defined _LIBC && defined USE_IN_LIBIO
756
 
              char *buf;
757
 
              int n;
758
 
#endif
759
 
 
760
 
            if (d->__posixly_correct)
761
 
              {
762
 
                /* 1003.2 specifies the format of this message.  */
763
 
#if defined _LIBC && defined USE_IN_LIBIO
764
 
                n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
765
 
                                argv[0], c);
766
 
#else
767
 
                fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
768
 
#endif
769
 
              }
770
 
            else
771
 
              {
772
 
#if defined _LIBC && defined USE_IN_LIBIO
773
 
                n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
774
 
                                argv[0], c);
775
 
#else
776
 
                fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
777
 
#endif
778
 
              }
779
 
 
780
 
#if defined _LIBC && defined USE_IN_LIBIO
781
 
            if (n >= 0)
782
 
              {
783
 
                _IO_flockfile (stderr);
784
 
 
785
 
                int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
786
 
                ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
787
 
 
788
 
                __fxprintf (NULL, "%s", buf);
789
 
 
790
 
                ((_IO_FILE *) stderr)->_flags2 = old_flags2;
791
 
                _IO_funlockfile (stderr);
792
 
 
793
 
                free (buf);
794
 
              }
795
 
#endif
796
 
          }
797
 
        d->optopt = c;
798
 
        return '?';
 
751
        if (print_errors)
 
752
          {
 
753
#if defined _LIBC && defined USE_IN_LIBIO
 
754
              char *buf;
 
755
              int n;
 
756
#endif
 
757
 
 
758
#if defined _LIBC && defined USE_IN_LIBIO
 
759
              n = __asprintf (&buf, _("%s: invalid option -- '%c'\n"),
 
760
                              argv[0], c);
 
761
#else
 
762
              fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
 
763
#endif
 
764
 
 
765
#if defined _LIBC && defined USE_IN_LIBIO
 
766
            if (n >= 0)
 
767
              {
 
768
                _IO_flockfile (stderr);
 
769
 
 
770
                int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
771
                ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
772
 
 
773
                __fxprintf (NULL, "%s", buf);
 
774
 
 
775
                ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
776
                _IO_funlockfile (stderr);
 
777
 
 
778
                free (buf);
 
779
              }
 
780
#endif
 
781
          }
 
782
        d->optopt = c;
 
783
        return '?';
799
784
      }
800
785
    /* Convenience. Treat POSIX -W foo same as long option --foo */
801
786
    if (temp[0] == 'W' && temp[1] == ';')
802
787
      {
803
 
        char *nameend;
804
 
        const struct option *p;
805
 
        const struct option *pfound = NULL;
806
 
        int exact = 0;
807
 
        int ambig = 0;
808
 
        int indfound = 0;
809
 
        int option_index;
810
 
 
811
 
        /* This is an option that requires an argument.  */
812
 
        if (*d->__nextchar != '\0')
813
 
          {
814
 
            d->optarg = d->__nextchar;
815
 
            /* If we end this ARGV-element by taking the rest as an arg,
816
 
               we must advance to the next element now.  */
817
 
            d->optind++;
818
 
          }
819
 
        else if (d->optind == argc)
820
 
          {
821
 
            if (print_errors)
822
 
              {
823
 
                /* 1003.2 specifies the format of this message.  */
824
 
#if defined _LIBC && defined USE_IN_LIBIO
825
 
                char *buf;
826
 
 
827
 
                if (__asprintf (&buf,
828
 
                                _("%s: option requires an argument -- %c\n"),
829
 
                                argv[0], c) >= 0)
830
 
                  {
831
 
                    _IO_flockfile (stderr);
832
 
 
833
 
                    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
834
 
                    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
835
 
 
836
 
                    __fxprintf (NULL, "%s", buf);
837
 
 
838
 
                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
839
 
                    _IO_funlockfile (stderr);
840
 
 
841
 
                    free (buf);
842
 
                  }
843
 
#else
844
 
                fprintf (stderr, _("%s: option requires an argument -- %c\n"),
845
 
                         argv[0], c);
846
 
#endif
847
 
              }
848
 
            d->optopt = c;
849
 
            if (optstring[0] == ':')
850
 
              c = ':';
851
 
            else
852
 
              c = '?';
853
 
            return c;
854
 
          }
855
 
        else
856
 
          /* We already incremented `d->optind' once;
857
 
             increment it again when taking next ARGV-elt as argument.  */
858
 
          d->optarg = argv[d->optind++];
859
 
 
860
 
        /* optarg is now the argument, see if it's in the
861
 
           table of longopts.  */
862
 
 
863
 
        for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
864
 
             nameend++)
865
 
          /* Do nothing.  */ ;
866
 
 
867
 
        /* Test all long options for either exact match
868
 
           or abbreviated matches.  */
869
 
        for (p = longopts, option_index = 0; p->name; p++, option_index++)
870
 
          if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
871
 
            {
872
 
              if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
873
 
                {
874
 
                  /* Exact match found.  */
875
 
                  pfound = p;
876
 
                  indfound = option_index;
877
 
                  exact = 1;
878
 
                  break;
879
 
                }
880
 
              else if (pfound == NULL)
881
 
                {
882
 
                  /* First nonexact match found.  */
883
 
                  pfound = p;
884
 
                  indfound = option_index;
885
 
                }
886
 
              else
887
 
                /* Second or later nonexact match found.  */
888
 
                ambig = 1;
889
 
            }
890
 
        if (ambig && !exact)
891
 
          {
892
 
            if (print_errors)
893
 
              {
894
 
#if defined _LIBC && defined USE_IN_LIBIO
895
 
                char *buf;
896
 
 
897
 
                if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
898
 
                                argv[0], argv[d->optind]) >= 0)
899
 
                  {
900
 
                    _IO_flockfile (stderr);
901
 
 
902
 
                    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
903
 
                    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
904
 
 
905
 
                    __fxprintf (NULL, "%s", buf);
906
 
 
907
 
                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
908
 
                    _IO_funlockfile (stderr);
909
 
 
910
 
                    free (buf);
911
 
                  }
912
 
#else
913
 
                fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
914
 
                         argv[0], argv[d->optind]);
915
 
#endif
916
 
              }
917
 
            d->__nextchar += strlen (d->__nextchar);
918
 
            d->optind++;
919
 
            return '?';
920
 
          }
921
 
        if (pfound != NULL)
922
 
          {
923
 
            option_index = indfound;
924
 
            if (*nameend)
925
 
              {
926
 
                /* Don't test has_arg with >, because some C compilers don't
927
 
                   allow it to be used on enums.  */
928
 
                if (pfound->has_arg)
929
 
                  d->optarg = nameend + 1;
930
 
                else
931
 
                  {
932
 
                    if (print_errors)
933
 
                      {
934
 
#if defined _LIBC && defined USE_IN_LIBIO
935
 
                        char *buf;
936
 
 
937
 
                        if (__asprintf (&buf, _("\
938
 
%s: option `-W %s' doesn't allow an argument\n"),
939
 
                                        argv[0], pfound->name) >= 0)
940
 
                          {
941
 
                            _IO_flockfile (stderr);
942
 
 
943
 
                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
944
 
                            ((_IO_FILE *) stderr)->_flags2
945
 
                              |= _IO_FLAGS2_NOTCANCEL;
946
 
 
947
 
                            __fxprintf (NULL, "%s", buf);
948
 
 
949
 
                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
950
 
                            _IO_funlockfile (stderr);
951
 
 
952
 
                            free (buf);
953
 
                          }
954
 
#else
955
 
                        fprintf (stderr, _("\
956
 
%s: option `-W %s' doesn't allow an argument\n"),
957
 
                                 argv[0], pfound->name);
958
 
#endif
959
 
                      }
960
 
 
961
 
                    d->__nextchar += strlen (d->__nextchar);
962
 
                    return '?';
963
 
                  }
964
 
              }
965
 
            else if (pfound->has_arg == 1)
966
 
              {
967
 
                if (d->optind < argc)
968
 
                  d->optarg = argv[d->optind++];
969
 
                else
970
 
                  {
971
 
                    if (print_errors)
972
 
                      {
973
 
#if defined _LIBC && defined USE_IN_LIBIO
974
 
                        char *buf;
975
 
 
976
 
                        if (__asprintf (&buf, _("\
977
 
%s: option `%s' requires an argument\n"),
978
 
                                        argv[0], argv[d->optind - 1]) >= 0)
979
 
                          {
980
 
                            _IO_flockfile (stderr);
981
 
 
982
 
                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
983
 
                            ((_IO_FILE *) stderr)->_flags2
984
 
                              |= _IO_FLAGS2_NOTCANCEL;
985
 
 
986
 
                            __fxprintf (NULL, "%s", buf);
987
 
 
988
 
                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
989
 
                            _IO_funlockfile (stderr);
990
 
 
991
 
                            free (buf);
992
 
                          }
993
 
#else
994
 
                        fprintf (stderr,
995
 
                                 _("%s: option `%s' requires an argument\n"),
996
 
                                 argv[0], argv[d->optind - 1]);
997
 
#endif
998
 
                      }
999
 
                    d->__nextchar += strlen (d->__nextchar);
1000
 
                    return optstring[0] == ':' ? ':' : '?';
1001
 
                  }
1002
 
              }
1003
 
            d->__nextchar += strlen (d->__nextchar);
1004
 
            if (longind != NULL)
1005
 
              *longind = option_index;
1006
 
            if (pfound->flag)
1007
 
              {
1008
 
                *(pfound->flag) = pfound->val;
1009
 
                return 0;
1010
 
              }
1011
 
            return pfound->val;
1012
 
          }
1013
 
          d->__nextchar = NULL;
1014
 
          return 'W';   /* Let the application handle it.   */
 
788
        char *nameend;
 
789
        const struct option *p;
 
790
        const struct option *pfound = NULL;
 
791
        int exact = 0;
 
792
        int ambig = 0;
 
793
        int indfound = 0;
 
794
        int option_index;
 
795
 
 
796
        /* This is an option that requires an argument.  */
 
797
        if (*d->__nextchar != '\0')
 
798
          {
 
799
            d->optarg = d->__nextchar;
 
800
            /* If we end this ARGV-element by taking the rest as an arg,
 
801
               we must advance to the next element now.  */
 
802
            d->optind++;
 
803
          }
 
804
        else if (d->optind == argc)
 
805
          {
 
806
            if (print_errors)
 
807
              {
 
808
#if defined _LIBC && defined USE_IN_LIBIO
 
809
                char *buf;
 
810
 
 
811
                if (__asprintf (&buf,
 
812
                                _("%s: option requires an argument -- '%c'\n"),
 
813
                                argv[0], c) >= 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
                    __fxprintf (NULL, "%s", buf);
 
821
 
 
822
                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
823
                    _IO_funlockfile (stderr);
 
824
 
 
825
                    free (buf);
 
826
                  }
 
827
#else
 
828
                fprintf (stderr,
 
829
                         _("%s: option requires an argument -- '%c'\n"),
 
830
                         argv[0], c);
 
831
#endif
 
832
              }
 
833
            d->optopt = c;
 
834
            if (optstring[0] == ':')
 
835
              c = ':';
 
836
            else
 
837
              c = '?';
 
838
            return c;
 
839
          }
 
840
        else
 
841
          /* We already incremented `d->optind' once;
 
842
             increment it again when taking next ARGV-elt as argument.  */
 
843
          d->optarg = argv[d->optind++];
 
844
 
 
845
        /* optarg is now the argument, see if it's in the
 
846
           table of longopts.  */
 
847
 
 
848
        for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
 
849
             nameend++)
 
850
          /* Do nothing.  */ ;
 
851
 
 
852
        /* Test all long options for either exact match
 
853
           or abbreviated matches.  */
 
854
        for (p = longopts, option_index = 0; p->name; p++, option_index++)
 
855
          if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
 
856
            {
 
857
              if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
 
858
                {
 
859
                  /* Exact match found.  */
 
860
                  pfound = p;
 
861
                  indfound = option_index;
 
862
                  exact = 1;
 
863
                  break;
 
864
                }
 
865
              else if (pfound == NULL)
 
866
                {
 
867
                  /* First nonexact match found.  */
 
868
                  pfound = p;
 
869
                  indfound = option_index;
 
870
                }
 
871
              else
 
872
                /* Second or later nonexact match found.  */
 
873
                ambig = 1;
 
874
            }
 
875
        if (ambig && !exact)
 
876
          {
 
877
            if (print_errors)
 
878
              {
 
879
#if defined _LIBC && defined USE_IN_LIBIO
 
880
                char *buf;
 
881
 
 
882
                if (__asprintf (&buf, _("%s: option '-W %s' is ambiguous\n"),
 
883
                                argv[0], argv[d->optind]) >= 0)
 
884
                  {
 
885
                    _IO_flockfile (stderr);
 
886
 
 
887
                    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
888
                    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
889
 
 
890
                    __fxprintf (NULL, "%s", buf);
 
891
 
 
892
                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
893
                    _IO_funlockfile (stderr);
 
894
 
 
895
                    free (buf);
 
896
                  }
 
897
#else
 
898
                fprintf (stderr, _("%s: option '-W %s' is ambiguous\n"),
 
899
                         argv[0], argv[d->optind]);
 
900
#endif
 
901
              }
 
902
            d->__nextchar += strlen (d->__nextchar);
 
903
            d->optind++;
 
904
            return '?';
 
905
          }
 
906
        if (pfound != NULL)
 
907
          {
 
908
            option_index = indfound;
 
909
            if (*nameend)
 
910
              {
 
911
                /* Don't test has_arg with >, because some C compilers don't
 
912
                   allow it to be used on enums.  */
 
913
                if (pfound->has_arg)
 
914
                  d->optarg = nameend + 1;
 
915
                else
 
916
                  {
 
917
                    if (print_errors)
 
918
                      {
 
919
#if defined _LIBC && defined USE_IN_LIBIO
 
920
                        char *buf;
 
921
 
 
922
                        if (__asprintf (&buf, _("\
 
923
%s: option '-W %s' doesn't allow an argument\n"),
 
924
                                        argv[0], pfound->name) >= 0)
 
925
                          {
 
926
                            _IO_flockfile (stderr);
 
927
 
 
928
                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
929
                            ((_IO_FILE *) stderr)->_flags2
 
930
                              |= _IO_FLAGS2_NOTCANCEL;
 
931
 
 
932
                            __fxprintf (NULL, "%s", buf);
 
933
 
 
934
                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
935
                            _IO_funlockfile (stderr);
 
936
 
 
937
                            free (buf);
 
938
                          }
 
939
#else
 
940
                        fprintf (stderr, _("\
 
941
%s: option '-W %s' doesn't allow an argument\n"),
 
942
                                 argv[0], pfound->name);
 
943
#endif
 
944
                      }
 
945
 
 
946
                    d->__nextchar += strlen (d->__nextchar);
 
947
                    return '?';
 
948
                  }
 
949
              }
 
950
            else if (pfound->has_arg == 1)
 
951
              {
 
952
                if (d->optind < argc)
 
953
                  d->optarg = argv[d->optind++];
 
954
                else
 
955
                  {
 
956
                    if (print_errors)
 
957
                      {
 
958
#if defined _LIBC && defined USE_IN_LIBIO
 
959
                        char *buf;
 
960
 
 
961
                        if (__asprintf (&buf, _("\
 
962
%s: option '%s' requires an argument\n"),
 
963
                                        argv[0], argv[d->optind - 1]) >= 0)
 
964
                          {
 
965
                            _IO_flockfile (stderr);
 
966
 
 
967
                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
968
                            ((_IO_FILE *) stderr)->_flags2
 
969
                              |= _IO_FLAGS2_NOTCANCEL;
 
970
 
 
971
                            __fxprintf (NULL, "%s", buf);
 
972
 
 
973
                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
974
                            _IO_funlockfile (stderr);
 
975
 
 
976
                            free (buf);
 
977
                          }
 
978
#else
 
979
                        fprintf (stderr,
 
980
                                 _("%s: option '%s' requires an argument\n"),
 
981
                                 argv[0], argv[d->optind - 1]);
 
982
#endif
 
983
                      }
 
984
                    d->__nextchar += strlen (d->__nextchar);
 
985
                    return optstring[0] == ':' ? ':' : '?';
 
986
                  }
 
987
              }
 
988
            d->__nextchar += strlen (d->__nextchar);
 
989
            if (longind != NULL)
 
990
              *longind = option_index;
 
991
            if (pfound->flag)
 
992
              {
 
993
                *(pfound->flag) = pfound->val;
 
994
                return 0;
 
995
              }
 
996
            return pfound->val;
 
997
          }
 
998
          d->__nextchar = NULL;
 
999
          return 'W';   /* Let the application handle it.   */
1015
1000
      }
1016
1001
    if (temp[1] == ':')
1017
1002
      {
1018
 
        if (temp[2] == ':')
1019
 
          {
1020
 
            /* This is an option that accepts an argument optionally.  */
1021
 
            if (*d->__nextchar != '\0')
1022
 
              {
1023
 
                d->optarg = d->__nextchar;
1024
 
                d->optind++;
1025
 
              }
1026
 
            else
1027
 
              d->optarg = NULL;
1028
 
            d->__nextchar = NULL;
1029
 
          }
1030
 
        else
1031
 
          {
1032
 
            /* This is an option that requires an argument.  */
1033
 
            if (*d->__nextchar != '\0')
1034
 
              {
1035
 
                d->optarg = d->__nextchar;
1036
 
                /* If we end this ARGV-element by taking the rest as an arg,
1037
 
                   we must advance to the next element now.  */
1038
 
                d->optind++;
1039
 
              }
1040
 
            else if (d->optind == argc)
1041
 
              {
1042
 
                if (print_errors)
1043
 
                  {
1044
 
                    /* 1003.2 specifies the format of this message.  */
 
1003
        if (temp[2] == ':')
 
1004
          {
 
1005
            /* This is an option that accepts an argument optionally.  */
 
1006
            if (*d->__nextchar != '\0')
 
1007
              {
 
1008
                d->optarg = d->__nextchar;
 
1009
                d->optind++;
 
1010
              }
 
1011
            else
 
1012
              d->optarg = NULL;
 
1013
            d->__nextchar = NULL;
 
1014
          }
 
1015
        else
 
1016
          {
 
1017
            /* This is an option that requires an argument.  */
 
1018
            if (*d->__nextchar != '\0')
 
1019
              {
 
1020
                d->optarg = d->__nextchar;
 
1021
                /* If we end this ARGV-element by taking the rest as an arg,
 
1022
                   we must advance to the next element now.  */
 
1023
                d->optind++;
 
1024
              }
 
1025
            else if (d->optind == argc)
 
1026
              {
 
1027
                if (print_errors)
 
1028
                  {
1045
1029
#if defined _LIBC && defined USE_IN_LIBIO
1046
 
                    char *buf;
1047
 
 
1048
 
                    if (__asprintf (&buf, _("\
1049
 
%s: option requires an argument -- %c\n"),
1050
 
                                    argv[0], c) >= 0)
1051
 
                      {
1052
 
                        _IO_flockfile (stderr);
1053
 
 
1054
 
                        int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1055
 
                        ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1056
 
 
1057
 
                        __fxprintf (NULL, "%s", buf);
1058
 
 
1059
 
                        ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1060
 
                        _IO_funlockfile (stderr);
1061
 
 
1062
 
                        free (buf);
1063
 
                      }
 
1030
                    char *buf;
 
1031
 
 
1032
                    if (__asprintf (&buf, _("\
 
1033
%s: option requires an argument -- '%c'\n"),
 
1034
                                    argv[0], c) >= 0)
 
1035
                      {
 
1036
                        _IO_flockfile (stderr);
 
1037
 
 
1038
                        int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
1039
                        ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
1040
 
 
1041
                        __fxprintf (NULL, "%s", buf);
 
1042
 
 
1043
                        ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
1044
                        _IO_funlockfile (stderr);
 
1045
 
 
1046
                        free (buf);
 
1047
                      }
1064
1048
#else
1065
 
                    fprintf (stderr,
1066
 
                             _("%s: option requires an argument -- %c\n"),
1067
 
                             argv[0], c);
 
1049
                    fprintf (stderr,
 
1050
                             _("%s: option requires an argument -- '%c'\n"),
 
1051
                             argv[0], c);
1068
1052
#endif
1069
 
                  }
1070
 
                d->optopt = c;
1071
 
                if (optstring[0] == ':')
1072
 
                  c = ':';
1073
 
                else
1074
 
                  c = '?';
1075
 
              }
1076
 
            else
1077
 
              /* We already incremented `optind' once;
1078
 
                 increment it again when taking next ARGV-elt as argument.  */
1079
 
              d->optarg = argv[d->optind++];
1080
 
            d->__nextchar = NULL;
1081
 
          }
 
1053
                  }
 
1054
                d->optopt = c;
 
1055
                if (optstring[0] == ':')
 
1056
                  c = ':';
 
1057
                else
 
1058
                  c = '?';
 
1059
              }
 
1060
            else
 
1061
              /* We already incremented `optind' once;
 
1062
                 increment it again when taking next ARGV-elt as argument.  */
 
1063
              d->optarg = argv[d->optind++];
 
1064
            d->__nextchar = NULL;
 
1065
          }
1082
1066
      }
1083
1067
    return c;
1084
1068
  }
1086
1070
 
1087
1071
int
1088
1072
_getopt_internal (int argc, char **argv, const char *optstring,
1089
 
                  const struct option *longopts, int *longind,
1090
 
                  int long_only, int posixly_correct)
 
1073
                  const struct option *longopts, int *longind, int long_only,
 
1074
                  int posixly_correct)
1091
1075
{
1092
1076
  int result;
1093
1077
 
1094
1078
  getopt_data.optind = optind;
1095
1079
  getopt_data.opterr = opterr;
1096
1080
 
1097
 
  result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
1098
 
                               long_only, posixly_correct, &getopt_data);
 
1081
  result = _getopt_internal_r (argc, argv, optstring, longopts,
 
1082
                               longind, long_only, &getopt_data,
 
1083
                               posixly_correct);
1099
1084
 
1100
1085
  optind = getopt_data.optind;
1101
1086
  optarg = getopt_data.optarg;
1115
1100
int
1116
1101
getopt (int argc, char *const *argv, const char *optstring)
1117
1102
{
1118
 
  return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
1119
 
                           POSIXLY_CORRECT);
1120
 
}
 
1103
  return _getopt_internal (argc, (char **) argv, optstring,
 
1104
                           (const struct option *) 0,
 
1105
                           (int *) 0,
 
1106
                           0, POSIXLY_CORRECT);
 
1107
}
 
1108
 
 
1109
#ifdef _LIBC
 
1110
int
 
1111
__posix_getopt (int argc, char *const *argv, const char *optstring)
 
1112
{
 
1113
  return _getopt_internal (argc, argv, optstring,
 
1114
                           (const struct option *) 0,
 
1115
                           (int *) 0,
 
1116
                           0, 1);
 
1117
}
 
1118
#endif
1121
1119
 
1122
1120
 
1123
1121
#ifdef TEST
1137
1135
 
1138
1136
      c = getopt (argc, argv, "abc:d:0123456789");
1139
1137
      if (c == -1)
1140
 
        break;
 
1138
        break;
1141
1139
 
1142
1140
      switch (c)
1143
 
        {
1144
 
        case '0':
1145
 
        case '1':
1146
 
        case '2':
1147
 
        case '3':
1148
 
        case '4':
1149
 
        case '5':
1150
 
        case '6':
1151
 
        case '7':
1152
 
        case '8':
1153
 
        case '9':
1154
 
          if (digit_optind != 0 && digit_optind != this_option_optind)
1155
 
            printf ("digits occur in two different argv-elements.\n");
1156
 
          digit_optind = this_option_optind;
1157
 
          printf ("option %c\n", c);
1158
 
          break;
1159
 
 
1160
 
        case 'a':
1161
 
          printf ("option a\n");
1162
 
          break;
1163
 
 
1164
 
        case 'b':
1165
 
          printf ("option b\n");
1166
 
          break;
1167
 
 
1168
 
        case 'c':
1169
 
          printf ("option c with value `%s'\n", optarg);
1170
 
          break;
1171
 
 
1172
 
        case '?':
1173
 
          break;
1174
 
 
1175
 
        default:
1176
 
          printf ("?? getopt returned character code 0%o ??\n", c);
1177
 
        }
 
1141
        {
 
1142
        case '0':
 
1143
        case '1':
 
1144
        case '2':
 
1145
        case '3':
 
1146
        case '4':
 
1147
        case '5':
 
1148
        case '6':
 
1149
        case '7':
 
1150
        case '8':
 
1151
        case '9':
 
1152
          if (digit_optind != 0 && digit_optind != this_option_optind)
 
1153
            printf ("digits occur in two different argv-elements.\n");
 
1154
          digit_optind = this_option_optind;
 
1155
          printf ("option %c\n", c);
 
1156
          break;
 
1157
 
 
1158
        case 'a':
 
1159
          printf ("option a\n");
 
1160
          break;
 
1161
 
 
1162
        case 'b':
 
1163
          printf ("option b\n");
 
1164
          break;
 
1165
 
 
1166
        case 'c':
 
1167
          printf ("option c with value '%s'\n", optarg);
 
1168
          break;
 
1169
 
 
1170
        case '?':
 
1171
          break;
 
1172
 
 
1173
        default:
 
1174
          printf ("?? getopt returned character code 0%o ??\n", c);
 
1175
        }
1178
1176
    }
1179
1177
 
1180
1178
  if (optind < argc)
1181
1179
    {
1182
1180
      printf ("non-option ARGV-elements: ");
1183
1181
      while (optind < argc)
1184
 
        printf ("%s ", argv[optind++]);
 
1182
        printf ("%s ", argv[optind++]);
1185
1183
      printf ("\n");
1186
1184
    }
1187
1185