~ubuntu-branches/ubuntu/trusty/renameutils/trusty

« back to all changes in this revision

Viewing changes to lib/getopt.c

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2011-08-20 10:58:41 UTC
  • mfrom: (0.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20110820105841-a9l1sqmww8aeqol4
Tags: 0.11.0-1
* New upstream release
* Bump Standards-Version up to 3.9.2
* Add empty build-arch and build-indep Makefile targets

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