Home | History | Annotate | Line # | Download | only in gnulib-lib
      1  1.1  christos /* Getopt for GNU.
      2  1.1  christos    NOTE: getopt is now part of the C library, so if you don't know what
      3  1.1  christos    "Keep this file name-space clean" means, talk to drepper (at) gnu.org
      4  1.1  christos    before changing it!
      5  1.1  christos    Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004,2006
      6  1.1  christos 	Free Software Foundation, Inc.
      7  1.1  christos    This file is part of the GNU C Library.
      8  1.1  christos 
      9  1.1  christos    This program is free software; you can redistribute it and/or modify
     10  1.1  christos    it under the terms of the GNU General Public License as published by
     11  1.1  christos    the Free Software Foundation; either version 2, or (at your option)
     12  1.1  christos    any later version.
     13  1.1  christos 
     14  1.1  christos    This program is distributed in the hope that it will be useful,
     15  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17  1.1  christos    GNU General Public License for more details.
     18  1.1  christos 
     19  1.1  christos    You should have received a copy of the GNU General Public License along
     20  1.1  christos    with this program; if not, write to the Free Software Foundation,
     21  1.1  christos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
     22  1.1  christos 
     23  1.1  christos #ifndef _LIBC
     25  1.1  christos # include <config.h>
     26  1.1  christos #endif
     27  1.1  christos 
     28  1.1  christos #include "getopt.h"
     29  1.1  christos 
     30  1.1  christos #include <stdio.h>
     31  1.1  christos #include <stdlib.h>
     32  1.1  christos #include <string.h>
     33  1.1  christos #include <unistd.h>
     34  1.1  christos 
     35  1.1  christos #ifdef __VMS
     36  1.1  christos # include <unixlib.h>
     37  1.1  christos #endif
     38  1.1  christos 
     39  1.1  christos #ifdef _LIBC
     40  1.1  christos # include <libintl.h>
     41  1.1  christos #else
     42  1.1  christos # include "gettext.h"
     43  1.1  christos # define _(msgid) gettext (msgid)
     44  1.1  christos #endif
     45  1.1  christos 
     46  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
     47  1.1  christos # include <wchar.h>
     48  1.1  christos #endif
     49  1.1  christos 
     50  1.1  christos #ifndef attribute_hidden
     51  1.1  christos # define attribute_hidden
     52  1.1  christos #endif
     53  1.1  christos 
     54  1.1  christos /* Unlike standard Unix `getopt', functions like `getopt_long'
     55  1.1  christos    let the user intersperse the options with the other arguments.
     56  1.1  christos 
     57  1.1  christos    As `getopt_long' works, it permutes the elements of ARGV so that,
     58  1.1  christos    when it is done, all the options precede everything else.  Thus
     59  1.1  christos    all application programs are extended to handle flexible argument order.
     60  1.1  christos 
     61  1.1  christos    Using `getopt' or setting the environment variable POSIXLY_CORRECT
     62  1.1  christos    disables permutation.
     63  1.1  christos    Then the application's behavior is completely standard.
     64  1.1  christos 
     65  1.1  christos    GNU application programs can use a third alternative mode in which
     66  1.1  christos    they can distinguish the relative order of options and other arguments.  */
     67  1.1  christos 
     68  1.1  christos #include "getopt_int.h"
     69  1.1  christos 
     70  1.1  christos /* For communication from `getopt' to the caller.
     71  1.1  christos    When `getopt' finds an option that takes an argument,
     72  1.1  christos    the argument value is returned here.
     73  1.1  christos    Also, when `ordering' is RETURN_IN_ORDER,
     74  1.1  christos    each non-option ARGV-element is returned here.  */
     75  1.1  christos 
     76  1.1  christos char *optarg;
     77  1.1  christos 
     78  1.1  christos /* Index in ARGV of the next element to be scanned.
     79  1.1  christos    This is used for communication to and from the caller
     80  1.1  christos    and for communication between successive calls to `getopt'.
     81  1.1  christos 
     82  1.1  christos    On entry to `getopt', zero means this is the first call; initialize.
     83  1.1  christos 
     84  1.1  christos    When `getopt' returns -1, this is the index of the first of the
     85  1.1  christos    non-option elements that the caller should itself scan.
     86  1.1  christos 
     87  1.1  christos    Otherwise, `optind' communicates from one call to the next
     88  1.1  christos    how much of ARGV has been scanned so far.  */
     89  1.1  christos 
     90  1.1  christos /* 1003.2 says this must be 1 before any call.  */
     91  1.1  christos int optind = 1;
     92  1.1  christos 
     93  1.1  christos /* Callers store zero here to inhibit the error message
     94  1.1  christos    for unrecognized options.  */
     95  1.1  christos 
     96  1.1  christos int opterr = 1;
     97  1.1  christos 
     98  1.1  christos /* Set to an option character which was unrecognized.
     99  1.1  christos    This must be initialized on some systems to avoid linking in the
    100  1.1  christos    system's own getopt implementation.  */
    101  1.1  christos 
    102  1.1  christos int optopt = '?';
    103  1.1  christos 
    104  1.1  christos /* Keep a global copy of all internal members of getopt_data.  */
    105  1.1  christos 
    106  1.1  christos static struct _getopt_data getopt_data;
    107  1.1  christos 
    108  1.1  christos 
    109  1.1  christos #if defined HAVE_DECL_GETENV && !HAVE_DECL_GETENV
    111  1.1  christos extern char *getenv ();
    112  1.1  christos #endif
    113  1.1  christos 
    114  1.1  christos #ifdef _LIBC
    116  1.1  christos /* Stored original parameters.
    117  1.1  christos    XXX This is no good solution.  We should rather copy the args so
    118  1.1  christos    that we can compare them later.  But we must not use malloc(3).  */
    119  1.1  christos extern int __libc_argc;
    120  1.1  christos extern char **__libc_argv;
    121  1.1  christos 
    122  1.1  christos /* Bash 2.0 gives us an environment variable containing flags
    123  1.1  christos    indicating ARGV elements that should not be considered arguments.  */
    124  1.1  christos 
    125  1.1  christos # ifdef USE_NONOPTION_FLAGS
    126  1.1  christos /* Defined in getopt_init.c  */
    127  1.1  christos extern char *__getopt_nonoption_flags;
    128  1.1  christos # endif
    129  1.1  christos 
    130  1.1  christos # ifdef USE_NONOPTION_FLAGS
    131  1.1  christos #  define SWAP_FLAGS(ch1, ch2) \
    132  1.1  christos   if (d->__nonoption_flags_len > 0)					      \
    133  1.1  christos     {									      \
    134  1.1  christos       char __tmp = __getopt_nonoption_flags[ch1];			      \
    135  1.1  christos       __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];	      \
    136  1.1  christos       __getopt_nonoption_flags[ch2] = __tmp;				      \
    137  1.1  christos     }
    138  1.1  christos # else
    139  1.1  christos #  define SWAP_FLAGS(ch1, ch2)
    140  1.1  christos # endif
    141  1.1  christos #else	/* !_LIBC */
    142  1.1  christos # define SWAP_FLAGS(ch1, ch2)
    143  1.1  christos #endif	/* _LIBC */
    144  1.1  christos 
    145  1.1  christos /* Exchange two adjacent subsequences of ARGV.
    146  1.1  christos    One subsequence is elements [first_nonopt,last_nonopt)
    147  1.1  christos    which contains all the non-options that have been skipped so far.
    148  1.1  christos    The other is elements [last_nonopt,optind), which contains all
    149  1.1  christos    the options processed since those non-options were skipped.
    150  1.1  christos 
    151  1.1  christos    `first_nonopt' and `last_nonopt' are relocated so that they describe
    152  1.1  christos    the new indices of the non-options in ARGV after they are moved.  */
    153  1.1  christos 
    154  1.1  christos static void
    155  1.1  christos exchange (char **argv, struct _getopt_data *d)
    156  1.1  christos {
    157  1.1  christos   int bottom = d->__first_nonopt;
    158  1.1  christos   int middle = d->__last_nonopt;
    159  1.1  christos   int top = d->optind;
    160  1.1  christos   char *tem;
    161  1.1  christos 
    162  1.1  christos   /* Exchange the shorter segment with the far end of the longer segment.
    163  1.1  christos      That puts the shorter segment into the right place.
    164  1.1  christos      It leaves the longer segment in the right place overall,
    165  1.1  christos      but it consists of two parts that need to be swapped next.  */
    166  1.1  christos 
    167  1.1  christos #if defined _LIBC && defined USE_NONOPTION_FLAGS
    168  1.1  christos   /* First make sure the handling of the `__getopt_nonoption_flags'
    169  1.1  christos      string can work normally.  Our top argument must be in the range
    170  1.1  christos      of the string.  */
    171  1.1  christos   if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
    172  1.1  christos     {
    173  1.1  christos       /* We must extend the array.  The user plays games with us and
    174  1.1  christos 	 presents new arguments.  */
    175  1.1  christos       char *new_str = malloc (top + 1);
    176  1.1  christos       if (new_str == NULL)
    177  1.1  christos 	d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
    178  1.1  christos       else
    179  1.1  christos 	{
    180  1.1  christos 	  memset (__mempcpy (new_str, __getopt_nonoption_flags,
    181  1.1  christos 			     d->__nonoption_flags_max_len),
    182  1.1  christos 		  '\0', top + 1 - d->__nonoption_flags_max_len);
    183  1.1  christos 	  d->__nonoption_flags_max_len = top + 1;
    184  1.1  christos 	  __getopt_nonoption_flags = new_str;
    185  1.1  christos 	}
    186  1.1  christos     }
    187  1.1  christos #endif
    188  1.1  christos 
    189  1.1  christos   while (top > middle && middle > bottom)
    190  1.1  christos     {
    191  1.1  christos       if (top - middle > middle - bottom)
    192  1.1  christos 	{
    193  1.1  christos 	  /* Bottom segment is the short one.  */
    194  1.1  christos 	  int len = middle - bottom;
    195  1.1  christos 	  register int i;
    196  1.1  christos 
    197  1.1  christos 	  /* Swap it with the top part of the top segment.  */
    198  1.1  christos 	  for (i = 0; i < len; i++)
    199  1.1  christos 	    {
    200  1.1  christos 	      tem = argv[bottom + i];
    201  1.1  christos 	      argv[bottom + i] = argv[top - (middle - bottom) + i];
    202  1.1  christos 	      argv[top - (middle - bottom) + i] = tem;
    203  1.1  christos 	      SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
    204  1.1  christos 	    }
    205  1.1  christos 	  /* Exclude the moved bottom segment from further swapping.  */
    206  1.1  christos 	  top -= len;
    207  1.1  christos 	}
    208  1.1  christos       else
    209  1.1  christos 	{
    210  1.1  christos 	  /* Top segment is the short one.  */
    211  1.1  christos 	  int len = top - middle;
    212  1.1  christos 	  register int i;
    213  1.1  christos 
    214  1.1  christos 	  /* Swap it with the bottom part of the bottom segment.  */
    215  1.1  christos 	  for (i = 0; i < len; i++)
    216  1.1  christos 	    {
    217  1.1  christos 	      tem = argv[bottom + i];
    218  1.1  christos 	      argv[bottom + i] = argv[middle + i];
    219  1.1  christos 	      argv[middle + i] = tem;
    220  1.1  christos 	      SWAP_FLAGS (bottom + i, middle + i);
    221  1.1  christos 	    }
    222  1.1  christos 	  /* Exclude the moved top segment from further swapping.  */
    223  1.1  christos 	  bottom += len;
    224  1.1  christos 	}
    225  1.1  christos     }
    226  1.1  christos 
    227  1.1  christos   /* Update records for the slots the non-options now occupy.  */
    228  1.1  christos 
    229  1.1  christos   d->__first_nonopt += (d->optind - d->__last_nonopt);
    230  1.1  christos   d->__last_nonopt = d->optind;
    231  1.1  christos }
    232  1.1  christos 
    233  1.1  christos /* Initialize the internal data when the first call is made.  */
    234  1.1  christos 
    235  1.1  christos static const char *
    236  1.1  christos _getopt_initialize (int argc, char **argv, const char *optstring,
    237  1.1  christos 		    int posixly_correct, struct _getopt_data *d)
    238  1.1  christos {
    239  1.1  christos   /* Start processing options with ARGV-element 1 (since ARGV-element 0
    240  1.1  christos      is the program name); the sequence of previously skipped
    241  1.1  christos      non-option ARGV-elements is empty.  */
    242  1.1  christos 
    243  1.1  christos   d->__first_nonopt = d->__last_nonopt = d->optind;
    244  1.1  christos 
    245  1.1  christos   d->__nextchar = NULL;
    246  1.1  christos 
    247  1.1  christos   d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
    248  1.1  christos 
    249  1.1  christos   /* Determine how to handle the ordering of options and nonoptions.  */
    250  1.1  christos 
    251  1.1  christos   if (optstring[0] == '-')
    252  1.1  christos     {
    253  1.1  christos       d->__ordering = RETURN_IN_ORDER;
    254  1.1  christos       ++optstring;
    255  1.1  christos     }
    256  1.1  christos   else if (optstring[0] == '+')
    257  1.1  christos     {
    258  1.1  christos       d->__ordering = REQUIRE_ORDER;
    259  1.1  christos       ++optstring;
    260  1.1  christos     }
    261  1.1  christos   else if (d->__posixly_correct)
    262  1.1  christos     d->__ordering = REQUIRE_ORDER;
    263  1.1  christos   else
    264  1.1  christos     d->__ordering = PERMUTE;
    265  1.1  christos 
    266  1.1  christos #if defined _LIBC && defined USE_NONOPTION_FLAGS
    267  1.1  christos   if (!d->__posixly_correct
    268  1.1  christos       && argc == __libc_argc && argv == __libc_argv)
    269  1.1  christos     {
    270  1.1  christos       if (d->__nonoption_flags_max_len == 0)
    271  1.1  christos 	{
    272  1.1  christos 	  if (__getopt_nonoption_flags == NULL
    273  1.1  christos 	      || __getopt_nonoption_flags[0] == '\0')
    274  1.1  christos 	    d->__nonoption_flags_max_len = -1;
    275  1.1  christos 	  else
    276  1.1  christos 	    {
    277  1.1  christos 	      const char *orig_str = __getopt_nonoption_flags;
    278  1.1  christos 	      int len = d->__nonoption_flags_max_len = strlen (orig_str);
    279  1.1  christos 	      if (d->__nonoption_flags_max_len < argc)
    280  1.1  christos 		d->__nonoption_flags_max_len = argc;
    281  1.1  christos 	      __getopt_nonoption_flags =
    282  1.1  christos 		(char *) malloc (d->__nonoption_flags_max_len);
    283  1.1  christos 	      if (__getopt_nonoption_flags == NULL)
    284  1.1  christos 		d->__nonoption_flags_max_len = -1;
    285  1.1  christos 	      else
    286  1.1  christos 		memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
    287  1.1  christos 			'\0', d->__nonoption_flags_max_len - len);
    288  1.1  christos 	    }
    289  1.1  christos 	}
    290  1.1  christos       d->__nonoption_flags_len = d->__nonoption_flags_max_len;
    291  1.1  christos     }
    292  1.1  christos   else
    293  1.1  christos     d->__nonoption_flags_len = 0;
    294  1.1  christos #endif
    295  1.1  christos 
    296  1.1  christos   return optstring;
    297  1.1  christos }
    298  1.1  christos 
    299  1.1  christos /* Scan elements of ARGV (whose length is ARGC) for option characters
    301  1.1  christos    given in OPTSTRING.
    302  1.1  christos 
    303  1.1  christos    If an element of ARGV starts with '-', and is not exactly "-" or "--",
    304  1.1  christos    then it is an option element.  The characters of this element
    305  1.1  christos    (aside from the initial '-') are option characters.  If `getopt'
    306  1.1  christos    is called repeatedly, it returns successively each of the option characters
    307  1.1  christos    from each of the option elements.
    308  1.1  christos 
    309  1.1  christos    If `getopt' finds another option character, it returns that character,
    310  1.1  christos    updating `optind' and `nextchar' so that the next call to `getopt' can
    311  1.1  christos    resume the scan with the following option character or ARGV-element.
    312  1.1  christos 
    313  1.1  christos    If there are no more option characters, `getopt' returns -1.
    314  1.1  christos    Then `optind' is the index in ARGV of the first ARGV-element
    315  1.1  christos    that is not an option.  (The ARGV-elements have been permuted
    316  1.1  christos    so that those that are not options now come last.)
    317  1.1  christos 
    318  1.1  christos    OPTSTRING is a string containing the legitimate option characters.
    319  1.1  christos    If an option character is seen that is not listed in OPTSTRING,
    320  1.1  christos    return '?' after printing an error message.  If you set `opterr' to
    321  1.1  christos    zero, the error message is suppressed but we still return '?'.
    322  1.1  christos 
    323  1.1  christos    If a char in OPTSTRING is followed by a colon, that means it wants an arg,
    324  1.1  christos    so the following text in the same ARGV-element, or the text of the following
    325  1.1  christos    ARGV-element, is returned in `optarg'.  Two colons mean an option that
    326  1.1  christos    wants an optional arg; if there is text in the current ARGV-element,
    327  1.1  christos    it is returned in `optarg', otherwise `optarg' is set to zero.
    328  1.1  christos 
    329  1.1  christos    If OPTSTRING starts with `-' or `+', it requests different methods of
    330  1.1  christos    handling the non-option ARGV-elements.
    331  1.1  christos    See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
    332  1.1  christos 
    333  1.1  christos    Long-named options begin with `--' instead of `-'.
    334  1.1  christos    Their names may be abbreviated as long as the abbreviation is unique
    335  1.1  christos    or is an exact match for some defined option.  If they have an
    336  1.1  christos    argument, it follows the option name in the same ARGV-element, separated
    337  1.1  christos    from the option name by a `=', or else the in next ARGV-element.
    338  1.1  christos    When `getopt' finds a long-named option, it returns 0 if that option's
    339  1.1  christos    `flag' field is nonzero, the value of the option's `val' field
    340  1.1  christos    if the `flag' field is zero.
    341  1.1  christos 
    342  1.1  christos    LONGOPTS is a vector of `struct option' terminated by an
    343  1.1  christos    element containing a name which is zero.
    344  1.1  christos 
    345  1.1  christos    LONGIND returns the index in LONGOPT of the long-named option found.
    346  1.1  christos    It is only valid when a long-named option has been found by the most
    347  1.1  christos    recent call.
    348  1.1  christos 
    349  1.1  christos    If LONG_ONLY is nonzero, '-' as well as '--' can introduce
    350  1.1  christos    long-named options.
    351  1.1  christos 
    352  1.1  christos    If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
    353  1.1  christos    environment variable were set.  */
    354  1.1  christos 
    355  1.1  christos int
    356  1.1  christos _getopt_internal_r (int argc, char **argv, const char *optstring,
    357  1.1  christos 		    const struct option *longopts, int *longind,
    358  1.1  christos 		    int long_only, int posixly_correct, struct _getopt_data *d)
    359  1.1  christos {
    360  1.1  christos   int print_errors = d->opterr;
    361  1.1  christos   if (optstring[0] == ':')
    362  1.1  christos     print_errors = 0;
    363  1.1  christos 
    364  1.1  christos   if (argc < 1)
    365  1.1  christos     return -1;
    366  1.1  christos 
    367  1.1  christos   d->optarg = NULL;
    368  1.1  christos 
    369  1.1  christos   if (d->optind == 0 || !d->__initialized)
    370  1.1  christos     {
    371  1.1  christos       if (d->optind == 0)
    372  1.1  christos 	d->optind = 1;	/* Don't scan ARGV[0], the program name.  */
    373  1.1  christos       optstring = _getopt_initialize (argc, argv, optstring,
    374  1.1  christos 				      posixly_correct, d);
    375  1.1  christos       d->__initialized = 1;
    376  1.1  christos     }
    377  1.1  christos 
    378  1.1  christos   /* Test whether ARGV[optind] points to a non-option argument.
    379  1.1  christos      Either it does not have option syntax, or there is an environment flag
    380  1.1  christos      from the shell indicating it is not an option.  The later information
    381  1.1  christos      is only used when the used in the GNU libc.  */
    382  1.1  christos #if defined _LIBC && defined USE_NONOPTION_FLAGS
    383  1.1  christos # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
    384  1.1  christos 		      || (d->optind < d->__nonoption_flags_len		      \
    385  1.1  christos 			  && __getopt_nonoption_flags[d->optind] == '1'))
    386  1.1  christos #else
    387  1.1  christos # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
    388  1.1  christos #endif
    389  1.1  christos 
    390  1.1  christos   if (d->__nextchar == NULL || *d->__nextchar == '\0')
    391  1.1  christos     {
    392  1.1  christos       /* Advance to the next ARGV-element.  */
    393  1.1  christos 
    394  1.1  christos       /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
    395  1.1  christos 	 moved back by the user (who may also have changed the arguments).  */
    396  1.1  christos       if (d->__last_nonopt > d->optind)
    397  1.1  christos 	d->__last_nonopt = d->optind;
    398  1.1  christos       if (d->__first_nonopt > d->optind)
    399  1.1  christos 	d->__first_nonopt = d->optind;
    400  1.1  christos 
    401  1.1  christos       if (d->__ordering == PERMUTE)
    402  1.1  christos 	{
    403  1.1  christos 	  /* If we have just processed some options following some non-options,
    404  1.1  christos 	     exchange them so that the options come first.  */
    405  1.1  christos 
    406  1.1  christos 	  if (d->__first_nonopt != d->__last_nonopt
    407  1.1  christos 	      && d->__last_nonopt != d->optind)
    408  1.1  christos 	    exchange ((char **) argv, d);
    409  1.1  christos 	  else if (d->__last_nonopt != d->optind)
    410  1.1  christos 	    d->__first_nonopt = d->optind;
    411  1.1  christos 
    412  1.1  christos 	  /* Skip any additional non-options
    413  1.1  christos 	     and extend the range of non-options previously skipped.  */
    414  1.1  christos 
    415  1.1  christos 	  while (d->optind < argc && NONOPTION_P)
    416  1.1  christos 	    d->optind++;
    417  1.1  christos 	  d->__last_nonopt = d->optind;
    418  1.1  christos 	}
    419  1.1  christos 
    420  1.1  christos       /* The special ARGV-element `--' means premature end of options.
    421  1.1  christos 	 Skip it like a null option,
    422  1.1  christos 	 then exchange with previous non-options as if it were an option,
    423  1.1  christos 	 then skip everything else like a non-option.  */
    424  1.1  christos 
    425  1.1  christos       if (d->optind != argc && !strcmp (argv[d->optind], "--"))
    426  1.1  christos 	{
    427  1.1  christos 	  d->optind++;
    428  1.1  christos 
    429  1.1  christos 	  if (d->__first_nonopt != d->__last_nonopt
    430  1.1  christos 	      && d->__last_nonopt != d->optind)
    431  1.1  christos 	    exchange ((char **) argv, d);
    432  1.1  christos 	  else if (d->__first_nonopt == d->__last_nonopt)
    433  1.1  christos 	    d->__first_nonopt = d->optind;
    434  1.1  christos 	  d->__last_nonopt = argc;
    435  1.1  christos 
    436  1.1  christos 	  d->optind = argc;
    437  1.1  christos 	}
    438  1.1  christos 
    439  1.1  christos       /* If we have done all the ARGV-elements, stop the scan
    440  1.1  christos 	 and back over any non-options that we skipped and permuted.  */
    441  1.1  christos 
    442  1.1  christos       if (d->optind == argc)
    443  1.1  christos 	{
    444  1.1  christos 	  /* Set the next-arg-index to point at the non-options
    445  1.1  christos 	     that we previously skipped, so the caller will digest them.  */
    446  1.1  christos 	  if (d->__first_nonopt != d->__last_nonopt)
    447  1.1  christos 	    d->optind = d->__first_nonopt;
    448  1.1  christos 	  return -1;
    449  1.1  christos 	}
    450  1.1  christos 
    451  1.1  christos       /* If we have come to a non-option and did not permute it,
    452  1.1  christos 	 either stop the scan or describe it to the caller and pass it by.  */
    453  1.1  christos 
    454  1.1  christos       if (NONOPTION_P)
    455  1.1  christos 	{
    456  1.1  christos 	  if (d->__ordering == REQUIRE_ORDER)
    457  1.1  christos 	    return -1;
    458  1.1  christos 	  d->optarg = argv[d->optind++];
    459  1.1  christos 	  return 1;
    460  1.1  christos 	}
    461  1.1  christos 
    462  1.1  christos       /* We have found another option-ARGV-element.
    463  1.1  christos 	 Skip the initial punctuation.  */
    464  1.1  christos 
    465  1.1  christos       d->__nextchar = (argv[d->optind] + 1
    466  1.1  christos 		  + (longopts != NULL && argv[d->optind][1] == '-'));
    467  1.1  christos     }
    468  1.1  christos 
    469  1.1  christos   /* Decode the current option-ARGV-element.  */
    470  1.1  christos 
    471  1.1  christos   /* Check whether the ARGV-element is a long option.
    472  1.1  christos 
    473  1.1  christos      If long_only and the ARGV-element has the form "-f", where f is
    474  1.1  christos      a valid short option, don't consider it an abbreviated form of
    475  1.1  christos      a long option that starts with f.  Otherwise there would be no
    476  1.1  christos      way to give the -f short option.
    477  1.1  christos 
    478  1.1  christos      On the other hand, if there's a long option "fubar" and
    479  1.1  christos      the ARGV-element is "-fu", do consider that an abbreviation of
    480  1.1  christos      the long option, just like "--fu", and not "-f" with arg "u".
    481  1.1  christos 
    482  1.1  christos      This distinction seems to be the most useful approach.  */
    483  1.1  christos 
    484  1.1  christos   if (longopts != NULL
    485  1.1  christos       && (argv[d->optind][1] == '-'
    486  1.1  christos 	  || (long_only && (argv[d->optind][2]
    487  1.1  christos 			    || !strchr (optstring, argv[d->optind][1])))))
    488  1.1  christos     {
    489  1.1  christos       char *nameend;
    490  1.1  christos       const struct option *p;
    491  1.1  christos       const struct option *pfound = NULL;
    492  1.1  christos       int exact = 0;
    493  1.1  christos       int ambig = 0;
    494  1.1  christos       int indfound = -1;
    495  1.1  christos       int option_index;
    496  1.1  christos 
    497  1.1  christos       for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
    498  1.1  christos 	/* Do nothing.  */ ;
    499  1.1  christos 
    500  1.1  christos       /* Test all long options for either exact match
    501  1.1  christos 	 or abbreviated matches.  */
    502  1.1  christos       for (p = longopts, option_index = 0; p->name; p++, option_index++)
    503  1.1  christos 	if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
    504  1.1  christos 	  {
    505  1.1  christos 	    if ((unsigned int) (nameend - d->__nextchar)
    506  1.1  christos 		== (unsigned int) strlen (p->name))
    507  1.1  christos 	      {
    508  1.1  christos 		/* Exact match found.  */
    509  1.1  christos 		pfound = p;
    510  1.1  christos 		indfound = option_index;
    511  1.1  christos 		exact = 1;
    512  1.1  christos 		break;
    513  1.1  christos 	      }
    514  1.1  christos 	    else if (pfound == NULL)
    515  1.1  christos 	      {
    516  1.1  christos 		/* First nonexact match found.  */
    517  1.1  christos 		pfound = p;
    518  1.1  christos 		indfound = option_index;
    519  1.1  christos 	      }
    520  1.1  christos 	    else if (long_only
    521  1.1  christos 		     || pfound->has_arg != p->has_arg
    522  1.1  christos 		     || pfound->flag != p->flag
    523  1.1  christos 		     || pfound->val != p->val)
    524  1.1  christos 	      /* Second or later nonexact match found.  */
    525  1.1  christos 	      ambig = 1;
    526  1.1  christos 	  }
    527  1.1  christos 
    528  1.1  christos       if (ambig && !exact)
    529  1.1  christos 	{
    530  1.1  christos 	  if (print_errors)
    531  1.1  christos 	    {
    532  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    533  1.1  christos 	      char *buf;
    534  1.1  christos 
    535  1.1  christos 	      if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
    536  1.1  christos 			      argv[0], argv[d->optind]) >= 0)
    537  1.1  christos 		{
    538  1.1  christos 		  _IO_flockfile (stderr);
    539  1.1  christos 
    540  1.1  christos 		  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
    541  1.1  christos 		  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
    542  1.1  christos 
    543  1.1  christos 		  __fxprintf (NULL, "%s", buf);
    544  1.1  christos 
    545  1.1  christos 		  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
    546  1.1  christos 		  _IO_funlockfile (stderr);
    547  1.1  christos 
    548  1.1  christos 		  free (buf);
    549  1.1  christos 		}
    550  1.1  christos #else
    551  1.1  christos 	      fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
    552  1.1  christos 		       argv[0], argv[d->optind]);
    553  1.1  christos #endif
    554  1.1  christos 	    }
    555  1.1  christos 	  d->__nextchar += strlen (d->__nextchar);
    556  1.1  christos 	  d->optind++;
    557  1.1  christos 	  d->optopt = 0;
    558  1.1  christos 	  return '?';
    559  1.1  christos 	}
    560  1.1  christos 
    561  1.1  christos       if (pfound != NULL)
    562  1.1  christos 	{
    563  1.1  christos 	  option_index = indfound;
    564  1.1  christos 	  d->optind++;
    565  1.1  christos 	  if (*nameend)
    566  1.1  christos 	    {
    567  1.1  christos 	      /* Don't test has_arg with >, because some C compilers don't
    568  1.1  christos 		 allow it to be used on enums.  */
    569  1.1  christos 	      if (pfound->has_arg)
    570  1.1  christos 		d->optarg = nameend + 1;
    571  1.1  christos 	      else
    572  1.1  christos 		{
    573  1.1  christos 		  if (print_errors)
    574  1.1  christos 		    {
    575  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    576  1.1  christos 		      char *buf;
    577  1.1  christos 		      int n;
    578  1.1  christos #endif
    579  1.1  christos 
    580  1.1  christos 		      if (argv[d->optind - 1][1] == '-')
    581  1.1  christos 			{
    582  1.1  christos 			  /* --option */
    583  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    584  1.1  christos 			  n = __asprintf (&buf, _("\
    585  1.1  christos %s: option `--%s' doesn't allow an argument\n"),
    586  1.1  christos 					  argv[0], pfound->name);
    587  1.1  christos #else
    588  1.1  christos 			  fprintf (stderr, _("\
    589  1.1  christos %s: option `--%s' doesn't allow an argument\n"),
    590  1.1  christos 				   argv[0], pfound->name);
    591  1.1  christos #endif
    592  1.1  christos 			}
    593  1.1  christos 		      else
    594  1.1  christos 			{
    595  1.1  christos 			  /* +option or -option */
    596  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    597  1.1  christos 			  n = __asprintf (&buf, _("\
    598  1.1  christos %s: option `%c%s' doesn't allow an argument\n"),
    599  1.1  christos 					  argv[0], argv[d->optind - 1][0],
    600  1.1  christos 					  pfound->name);
    601  1.1  christos #else
    602  1.1  christos 			  fprintf (stderr, _("\
    603  1.1  christos %s: option `%c%s' doesn't allow an argument\n"),
    604  1.1  christos 				   argv[0], argv[d->optind - 1][0],
    605  1.1  christos 				   pfound->name);
    606  1.1  christos #endif
    607  1.1  christos 			}
    608  1.1  christos 
    609  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    610  1.1  christos 		      if (n >= 0)
    611  1.1  christos 			{
    612  1.1  christos 			  _IO_flockfile (stderr);
    613  1.1  christos 
    614  1.1  christos 			  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
    615  1.1  christos 			  ((_IO_FILE *) stderr)->_flags2
    616  1.1  christos 			    |= _IO_FLAGS2_NOTCANCEL;
    617  1.1  christos 
    618  1.1  christos 			  __fxprintf (NULL, "%s", buf);
    619  1.1  christos 
    620  1.1  christos 			  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
    621  1.1  christos 			  _IO_funlockfile (stderr);
    622  1.1  christos 
    623  1.1  christos 			  free (buf);
    624  1.1  christos 			}
    625  1.1  christos #endif
    626  1.1  christos 		    }
    627  1.1  christos 
    628  1.1  christos 		  d->__nextchar += strlen (d->__nextchar);
    629  1.1  christos 
    630  1.1  christos 		  d->optopt = pfound->val;
    631  1.1  christos 		  return '?';
    632  1.1  christos 		}
    633  1.1  christos 	    }
    634  1.1  christos 	  else if (pfound->has_arg == 1)
    635  1.1  christos 	    {
    636  1.1  christos 	      if (d->optind < argc)
    637  1.1  christos 		d->optarg = argv[d->optind++];
    638  1.1  christos 	      else
    639  1.1  christos 		{
    640  1.1  christos 		  if (print_errors)
    641  1.1  christos 		    {
    642  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    643  1.1  christos 		      char *buf;
    644  1.1  christos 
    645  1.1  christos 		      if (__asprintf (&buf, _("\
    646  1.1  christos %s: option `%s' requires an argument\n"),
    647  1.1  christos 				      argv[0], argv[d->optind - 1]) >= 0)
    648  1.1  christos 			{
    649  1.1  christos 			  _IO_flockfile (stderr);
    650  1.1  christos 
    651  1.1  christos 			  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
    652  1.1  christos 			  ((_IO_FILE *) stderr)->_flags2
    653  1.1  christos 			    |= _IO_FLAGS2_NOTCANCEL;
    654  1.1  christos 
    655  1.1  christos 			  __fxprintf (NULL, "%s", buf);
    656  1.1  christos 
    657  1.1  christos 			  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
    658  1.1  christos 			  _IO_funlockfile (stderr);
    659  1.1  christos 
    660  1.1  christos 			  free (buf);
    661  1.1  christos 			}
    662  1.1  christos #else
    663  1.1  christos 		      fprintf (stderr,
    664  1.1  christos 			       _("%s: option `%s' requires an argument\n"),
    665  1.1  christos 			       argv[0], argv[d->optind - 1]);
    666  1.1  christos #endif
    667  1.1  christos 		    }
    668  1.1  christos 		  d->__nextchar += strlen (d->__nextchar);
    669  1.1  christos 		  d->optopt = pfound->val;
    670  1.1  christos 		  return optstring[0] == ':' ? ':' : '?';
    671  1.1  christos 		}
    672  1.1  christos 	    }
    673  1.1  christos 	  d->__nextchar += strlen (d->__nextchar);
    674  1.1  christos 	  if (longind != NULL)
    675  1.1  christos 	    *longind = option_index;
    676  1.1  christos 	  if (pfound->flag)
    677  1.1  christos 	    {
    678  1.1  christos 	      *(pfound->flag) = pfound->val;
    679  1.1  christos 	      return 0;
    680  1.1  christos 	    }
    681  1.1  christos 	  return pfound->val;
    682  1.1  christos 	}
    683  1.1  christos 
    684  1.1  christos       /* Can't find it as a long option.  If this is not getopt_long_only,
    685  1.1  christos 	 or the option starts with '--' or is not a valid short
    686  1.1  christos 	 option, then it's an error.
    687  1.1  christos 	 Otherwise interpret it as a short option.  */
    688  1.1  christos       if (!long_only || argv[d->optind][1] == '-'
    689  1.1  christos 	  || strchr (optstring, *d->__nextchar) == NULL)
    690  1.1  christos 	{
    691  1.1  christos 	  if (print_errors)
    692  1.1  christos 	    {
    693  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    694  1.1  christos 	      char *buf;
    695  1.1  christos 	      int n;
    696  1.1  christos #endif
    697  1.1  christos 
    698  1.1  christos 	      if (argv[d->optind][1] == '-')
    699  1.1  christos 		{
    700  1.1  christos 		  /* --option */
    701  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    702  1.1  christos 		  n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
    703  1.1  christos 				  argv[0], d->__nextchar);
    704  1.1  christos #else
    705  1.1  christos 		  fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
    706  1.1  christos 			   argv[0], d->__nextchar);
    707  1.1  christos #endif
    708  1.1  christos 		}
    709  1.1  christos 	      else
    710  1.1  christos 		{
    711  1.1  christos 		  /* +option or -option */
    712  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    713  1.1  christos 		  n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
    714  1.1  christos 				  argv[0], argv[d->optind][0], d->__nextchar);
    715  1.1  christos #else
    716  1.1  christos 		  fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
    717  1.1  christos 			   argv[0], argv[d->optind][0], d->__nextchar);
    718  1.1  christos #endif
    719  1.1  christos 		}
    720  1.1  christos 
    721  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    722  1.1  christos 	      if (n >= 0)
    723  1.1  christos 		{
    724  1.1  christos 		  _IO_flockfile (stderr);
    725  1.1  christos 
    726  1.1  christos 		  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
    727  1.1  christos 		  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
    728  1.1  christos 
    729  1.1  christos 		  __fxprintf (NULL, "%s", buf);
    730  1.1  christos 
    731  1.1  christos 		  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
    732  1.1  christos 		  _IO_funlockfile (stderr);
    733  1.1  christos 
    734  1.1  christos 		  free (buf);
    735  1.1  christos 		}
    736  1.1  christos #endif
    737  1.1  christos 	    }
    738  1.1  christos 	  d->__nextchar = (char *) "";
    739  1.1  christos 	  d->optind++;
    740  1.1  christos 	  d->optopt = 0;
    741  1.1  christos 	  return '?';
    742  1.1  christos 	}
    743  1.1  christos     }
    744  1.1  christos 
    745  1.1  christos   /* Look at and handle the next short option-character.  */
    746  1.1  christos 
    747  1.1  christos   {
    748  1.1  christos     char c = *d->__nextchar++;
    749  1.1  christos     char *temp = strchr (optstring, c);
    750  1.1  christos 
    751  1.1  christos     /* Increment `optind' when we start to process its last character.  */
    752  1.1  christos     if (*d->__nextchar == '\0')
    753  1.1  christos       ++d->optind;
    754  1.1  christos 
    755  1.1  christos     if (temp == NULL || c == ':')
    756  1.1  christos       {
    757  1.1  christos 	if (print_errors)
    758  1.1  christos 	  {
    759  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    760  1.1  christos 	      char *buf;
    761  1.1  christos 	      int n;
    762  1.1  christos #endif
    763  1.1  christos 
    764  1.1  christos 	    if (d->__posixly_correct)
    765  1.1  christos 	      {
    766  1.1  christos 		/* 1003.2 specifies the format of this message.  */
    767  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    768  1.1  christos 		n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
    769  1.1  christos 				argv[0], c);
    770  1.1  christos #else
    771  1.1  christos 		fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
    772  1.1  christos #endif
    773  1.1  christos 	      }
    774  1.1  christos 	    else
    775  1.1  christos 	      {
    776  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    777  1.1  christos 		n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
    778  1.1  christos 				argv[0], c);
    779  1.1  christos #else
    780  1.1  christos 		fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
    781  1.1  christos #endif
    782  1.1  christos 	      }
    783  1.1  christos 
    784  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    785  1.1  christos 	    if (n >= 0)
    786  1.1  christos 	      {
    787  1.1  christos 		_IO_flockfile (stderr);
    788  1.1  christos 
    789  1.1  christos 		int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
    790  1.1  christos 		((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
    791  1.1  christos 
    792  1.1  christos 		__fxprintf (NULL, "%s", buf);
    793  1.1  christos 
    794  1.1  christos 		((_IO_FILE *) stderr)->_flags2 = old_flags2;
    795  1.1  christos 		_IO_funlockfile (stderr);
    796  1.1  christos 
    797  1.1  christos 		free (buf);
    798  1.1  christos 	      }
    799  1.1  christos #endif
    800  1.1  christos 	  }
    801  1.1  christos 	d->optopt = c;
    802  1.1  christos 	return '?';
    803  1.1  christos       }
    804  1.1  christos     /* Convenience. Treat POSIX -W foo same as long option --foo */
    805  1.1  christos     if (temp[0] == 'W' && temp[1] == ';')
    806  1.1  christos       {
    807  1.1  christos 	char *nameend;
    808  1.1  christos 	const struct option *p;
    809  1.1  christos 	const struct option *pfound = NULL;
    810  1.1  christos 	int exact = 0;
    811  1.1  christos 	int ambig = 0;
    812  1.1  christos 	int indfound = 0;
    813  1.1  christos 	int option_index;
    814  1.1  christos 
    815  1.1  christos 	/* This is an option that requires an argument.  */
    816  1.1  christos 	if (*d->__nextchar != '\0')
    817  1.1  christos 	  {
    818  1.1  christos 	    d->optarg = d->__nextchar;
    819  1.1  christos 	    /* If we end this ARGV-element by taking the rest as an arg,
    820  1.1  christos 	       we must advance to the next element now.  */
    821  1.1  christos 	    d->optind++;
    822  1.1  christos 	  }
    823  1.1  christos 	else if (d->optind == argc)
    824  1.1  christos 	  {
    825  1.1  christos 	    if (print_errors)
    826  1.1  christos 	      {
    827  1.1  christos 		/* 1003.2 specifies the format of this message.  */
    828  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    829  1.1  christos 		char *buf;
    830  1.1  christos 
    831  1.1  christos 		if (__asprintf (&buf,
    832  1.1  christos 				_("%s: option requires an argument -- %c\n"),
    833  1.1  christos 				argv[0], c) >= 0)
    834  1.1  christos 		  {
    835  1.1  christos 		    _IO_flockfile (stderr);
    836  1.1  christos 
    837  1.1  christos 		    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
    838  1.1  christos 		    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
    839  1.1  christos 
    840  1.1  christos 		    __fxprintf (NULL, "%s", buf);
    841  1.1  christos 
    842  1.1  christos 		    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
    843  1.1  christos 		    _IO_funlockfile (stderr);
    844  1.1  christos 
    845  1.1  christos 		    free (buf);
    846  1.1  christos 		  }
    847  1.1  christos #else
    848  1.1  christos 		fprintf (stderr, _("%s: option requires an argument -- %c\n"),
    849  1.1  christos 			 argv[0], c);
    850  1.1  christos #endif
    851  1.1  christos 	      }
    852  1.1  christos 	    d->optopt = c;
    853  1.1  christos 	    if (optstring[0] == ':')
    854  1.1  christos 	      c = ':';
    855  1.1  christos 	    else
    856  1.1  christos 	      c = '?';
    857  1.1  christos 	    return c;
    858  1.1  christos 	  }
    859  1.1  christos 	else
    860  1.1  christos 	  /* We already incremented `d->optind' once;
    861  1.1  christos 	     increment it again when taking next ARGV-elt as argument.  */
    862  1.1  christos 	  d->optarg = argv[d->optind++];
    863  1.1  christos 
    864  1.1  christos 	/* optarg is now the argument, see if it's in the
    865  1.1  christos 	   table of longopts.  */
    866  1.1  christos 
    867  1.1  christos 	for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
    868  1.1  christos 	     nameend++)
    869  1.1  christos 	  /* Do nothing.  */ ;
    870  1.1  christos 
    871  1.1  christos 	/* Test all long options for either exact match
    872  1.1  christos 	   or abbreviated matches.  */
    873  1.1  christos 	for (p = longopts, option_index = 0; p->name; p++, option_index++)
    874  1.1  christos 	  if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
    875  1.1  christos 	    {
    876  1.1  christos 	      if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
    877  1.1  christos 		{
    878  1.1  christos 		  /* Exact match found.  */
    879  1.1  christos 		  pfound = p;
    880  1.1  christos 		  indfound = option_index;
    881  1.1  christos 		  exact = 1;
    882  1.1  christos 		  break;
    883  1.1  christos 		}
    884  1.1  christos 	      else if (pfound == NULL)
    885  1.1  christos 		{
    886  1.1  christos 		  /* First nonexact match found.  */
    887  1.1  christos 		  pfound = p;
    888  1.1  christos 		  indfound = option_index;
    889  1.1  christos 		}
    890  1.1  christos 	      else
    891  1.1  christos 		/* Second or later nonexact match found.  */
    892  1.1  christos 		ambig = 1;
    893  1.1  christos 	    }
    894  1.1  christos 	if (ambig && !exact)
    895  1.1  christos 	  {
    896  1.1  christos 	    if (print_errors)
    897  1.1  christos 	      {
    898  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    899  1.1  christos 		char *buf;
    900  1.1  christos 
    901  1.1  christos 		if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
    902  1.1  christos 				argv[0], argv[d->optind]) >= 0)
    903  1.1  christos 		  {
    904  1.1  christos 		    _IO_flockfile (stderr);
    905  1.1  christos 
    906  1.1  christos 		    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
    907  1.1  christos 		    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
    908  1.1  christos 
    909  1.1  christos 		    __fxprintf (NULL, "%s", buf);
    910  1.1  christos 
    911  1.1  christos 		    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
    912  1.1  christos 		    _IO_funlockfile (stderr);
    913  1.1  christos 
    914  1.1  christos 		    free (buf);
    915  1.1  christos 		  }
    916  1.1  christos #else
    917  1.1  christos 		fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
    918  1.1  christos 			 argv[0], argv[d->optind]);
    919  1.1  christos #endif
    920  1.1  christos 	      }
    921  1.1  christos 	    d->__nextchar += strlen (d->__nextchar);
    922  1.1  christos 	    d->optind++;
    923  1.1  christos 	    return '?';
    924  1.1  christos 	  }
    925  1.1  christos 	if (pfound != NULL)
    926  1.1  christos 	  {
    927  1.1  christos 	    option_index = indfound;
    928  1.1  christos 	    if (*nameend)
    929  1.1  christos 	      {
    930  1.1  christos 		/* Don't test has_arg with >, because some C compilers don't
    931  1.1  christos 		   allow it to be used on enums.  */
    932  1.1  christos 		if (pfound->has_arg)
    933  1.1  christos 		  d->optarg = nameend + 1;
    934  1.1  christos 		else
    935  1.1  christos 		  {
    936  1.1  christos 		    if (print_errors)
    937  1.1  christos 		      {
    938  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    939  1.1  christos 			char *buf;
    940  1.1  christos 
    941  1.1  christos 			if (__asprintf (&buf, _("\
    942  1.1  christos %s: option `-W %s' doesn't allow an argument\n"),
    943  1.1  christos 					argv[0], pfound->name) >= 0)
    944  1.1  christos 			  {
    945  1.1  christos 			    _IO_flockfile (stderr);
    946  1.1  christos 
    947  1.1  christos 			    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
    948  1.1  christos 			    ((_IO_FILE *) stderr)->_flags2
    949  1.1  christos 			      |= _IO_FLAGS2_NOTCANCEL;
    950  1.1  christos 
    951  1.1  christos 			    __fxprintf (NULL, "%s", buf);
    952  1.1  christos 
    953  1.1  christos 			    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
    954  1.1  christos 			    _IO_funlockfile (stderr);
    955  1.1  christos 
    956  1.1  christos 			    free (buf);
    957  1.1  christos 			  }
    958  1.1  christos #else
    959  1.1  christos 			fprintf (stderr, _("\
    960  1.1  christos %s: option `-W %s' doesn't allow an argument\n"),
    961  1.1  christos 				 argv[0], pfound->name);
    962  1.1  christos #endif
    963  1.1  christos 		      }
    964  1.1  christos 
    965  1.1  christos 		    d->__nextchar += strlen (d->__nextchar);
    966  1.1  christos 		    return '?';
    967  1.1  christos 		  }
    968  1.1  christos 	      }
    969  1.1  christos 	    else if (pfound->has_arg == 1)
    970  1.1  christos 	      {
    971  1.1  christos 		if (d->optind < argc)
    972  1.1  christos 		  d->optarg = argv[d->optind++];
    973  1.1  christos 		else
    974  1.1  christos 		  {
    975  1.1  christos 		    if (print_errors)
    976  1.1  christos 		      {
    977  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
    978  1.1  christos 			char *buf;
    979  1.1  christos 
    980  1.1  christos 			if (__asprintf (&buf, _("\
    981  1.1  christos %s: option `%s' requires an argument\n"),
    982  1.1  christos 					argv[0], argv[d->optind - 1]) >= 0)
    983  1.1  christos 			  {
    984  1.1  christos 			    _IO_flockfile (stderr);
    985  1.1  christos 
    986  1.1  christos 			    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
    987  1.1  christos 			    ((_IO_FILE *) stderr)->_flags2
    988  1.1  christos 			      |= _IO_FLAGS2_NOTCANCEL;
    989  1.1  christos 
    990  1.1  christos 			    __fxprintf (NULL, "%s", buf);
    991  1.1  christos 
    992  1.1  christos 			    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
    993  1.1  christos 			    _IO_funlockfile (stderr);
    994  1.1  christos 
    995  1.1  christos 			    free (buf);
    996  1.1  christos 			  }
    997  1.1  christos #else
    998  1.1  christos 			fprintf (stderr,
    999  1.1  christos 				 _("%s: option `%s' requires an argument\n"),
   1000  1.1  christos 				 argv[0], argv[d->optind - 1]);
   1001  1.1  christos #endif
   1002  1.1  christos 		      }
   1003  1.1  christos 		    d->__nextchar += strlen (d->__nextchar);
   1004  1.1  christos 		    return optstring[0] == ':' ? ':' : '?';
   1005  1.1  christos 		  }
   1006  1.1  christos 	      }
   1007  1.1  christos 	    d->__nextchar += strlen (d->__nextchar);
   1008  1.1  christos 	    if (longind != NULL)
   1009  1.1  christos 	      *longind = option_index;
   1010  1.1  christos 	    if (pfound->flag)
   1011  1.1  christos 	      {
   1012  1.1  christos 		*(pfound->flag) = pfound->val;
   1013  1.1  christos 		return 0;
   1014  1.1  christos 	      }
   1015  1.1  christos 	    return pfound->val;
   1016  1.1  christos 	  }
   1017  1.1  christos 	  d->__nextchar = NULL;
   1018  1.1  christos 	  return 'W';	/* Let the application handle it.   */
   1019  1.1  christos       }
   1020  1.1  christos     if (temp[1] == ':')
   1021  1.1  christos       {
   1022  1.1  christos 	if (temp[2] == ':')
   1023  1.1  christos 	  {
   1024  1.1  christos 	    /* This is an option that accepts an argument optionally.  */
   1025  1.1  christos 	    if (*d->__nextchar != '\0')
   1026  1.1  christos 	      {
   1027  1.1  christos 		d->optarg = d->__nextchar;
   1028  1.1  christos 		d->optind++;
   1029  1.1  christos 	      }
   1030  1.1  christos 	    else
   1031  1.1  christos 	      d->optarg = NULL;
   1032  1.1  christos 	    d->__nextchar = NULL;
   1033  1.1  christos 	  }
   1034  1.1  christos 	else
   1035  1.1  christos 	  {
   1036  1.1  christos 	    /* This is an option that requires an argument.  */
   1037  1.1  christos 	    if (*d->__nextchar != '\0')
   1038  1.1  christos 	      {
   1039  1.1  christos 		d->optarg = d->__nextchar;
   1040  1.1  christos 		/* If we end this ARGV-element by taking the rest as an arg,
   1041  1.1  christos 		   we must advance to the next element now.  */
   1042  1.1  christos 		d->optind++;
   1043  1.1  christos 	      }
   1044  1.1  christos 	    else if (d->optind == argc)
   1045  1.1  christos 	      {
   1046  1.1  christos 		if (print_errors)
   1047  1.1  christos 		  {
   1048  1.1  christos 		    /* 1003.2 specifies the format of this message.  */
   1049  1.1  christos #if defined _LIBC && defined USE_IN_LIBIO
   1050  1.1  christos 		    char *buf;
   1051  1.1  christos 
   1052  1.1  christos 		    if (__asprintf (&buf, _("\
   1053  1.1  christos %s: option requires an argument -- %c\n"),
   1054  1.1  christos 				    argv[0], c) >= 0)
   1055  1.1  christos 		      {
   1056  1.1  christos 			_IO_flockfile (stderr);
   1057  1.1  christos 
   1058  1.1  christos 			int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
   1059  1.1  christos 			((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
   1060  1.1  christos 
   1061  1.1  christos 			__fxprintf (NULL, "%s", buf);
   1062  1.1  christos 
   1063  1.1  christos 			((_IO_FILE *) stderr)->_flags2 = old_flags2;
   1064  1.1  christos 			_IO_funlockfile (stderr);
   1065  1.1  christos 
   1066  1.1  christos 			free (buf);
   1067  1.1  christos 		      }
   1068  1.1  christos #else
   1069  1.1  christos 		    fprintf (stderr,
   1070  1.1  christos 			     _("%s: option requires an argument -- %c\n"),
   1071  1.1  christos 			     argv[0], c);
   1072  1.1  christos #endif
   1073  1.1  christos 		  }
   1074  1.1  christos 		d->optopt = c;
   1075  1.1  christos 		if (optstring[0] == ':')
   1076  1.1  christos 		  c = ':';
   1077  1.1  christos 		else
   1078  1.1  christos 		  c = '?';
   1079  1.1  christos 	      }
   1080  1.1  christos 	    else
   1081  1.1  christos 	      /* We already incremented `optind' once;
   1082  1.1  christos 		 increment it again when taking next ARGV-elt as argument.  */
   1083  1.1  christos 	      d->optarg = argv[d->optind++];
   1084  1.1  christos 	    d->__nextchar = NULL;
   1085  1.1  christos 	  }
   1086  1.1  christos       }
   1087  1.1  christos     return c;
   1088  1.1  christos   }
   1089  1.1  christos }
   1090  1.1  christos 
   1091  1.1  christos int
   1092  1.1  christos _getopt_internal (int argc, char **argv, const char *optstring,
   1093  1.1  christos 		  const struct option *longopts, int *longind,
   1094  1.1  christos 		  int long_only, int posixly_correct)
   1095  1.1  christos {
   1096  1.1  christos   int result;
   1097  1.1  christos 
   1098  1.1  christos   getopt_data.optind = optind;
   1099  1.1  christos   getopt_data.opterr = opterr;
   1100  1.1  christos 
   1101  1.1  christos   result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
   1102  1.1  christos 			       long_only, posixly_correct, &getopt_data);
   1103  1.1  christos 
   1104  1.1  christos   optind = getopt_data.optind;
   1105  1.1  christos   optarg = getopt_data.optarg;
   1106  1.1  christos   optopt = getopt_data.optopt;
   1107  1.1  christos 
   1108  1.1  christos   return result;
   1109  1.1  christos }
   1110  1.1  christos 
   1111  1.1  christos /* glibc gets a LSB-compliant getopt.
   1112  1.1  christos    Standalone applications get a POSIX-compliant getopt.  */
   1113  1.1  christos #if _LIBC
   1114  1.1  christos enum { POSIXLY_CORRECT = 0 };
   1115  1.1  christos #else
   1116  1.1  christos enum { POSIXLY_CORRECT = 1 };
   1117  1.1  christos #endif
   1118  1.1  christos 
   1119  1.1  christos int
   1120  1.1  christos getopt (int argc, char *const *argv, const char *optstring)
   1121  1.1  christos {
   1122  1.1  christos   return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
   1123  1.1  christos 			   POSIXLY_CORRECT);
   1124  1.1  christos }
   1125  1.1  christos 
   1126  1.1  christos 
   1127  1.1  christos #ifdef TEST
   1129  1.1  christos 
   1130  1.1  christos /* Compile with -DTEST to make an executable for use in testing
   1131  1.1  christos    the above definition of `getopt'.  */
   1132  1.1  christos 
   1133  1.1  christos int
   1134  1.1  christos main (int argc, char **argv)
   1135  1.1  christos {
   1136  1.1  christos   int c;
   1137  1.1  christos   int digit_optind = 0;
   1138  1.1  christos 
   1139  1.1  christos   while (1)
   1140  1.1  christos     {
   1141  1.1  christos       int this_option_optind = optind ? optind : 1;
   1142  1.1  christos 
   1143  1.1  christos       c = getopt (argc, argv, "abc:d:0123456789");
   1144  1.1  christos       if (c == -1)
   1145  1.1  christos 	break;
   1146  1.1  christos 
   1147  1.1  christos       switch (c)
   1148  1.1  christos 	{
   1149  1.1  christos 	case '0':
   1150  1.1  christos 	case '1':
   1151  1.1  christos 	case '2':
   1152  1.1  christos 	case '3':
   1153  1.1  christos 	case '4':
   1154  1.1  christos 	case '5':
   1155  1.1  christos 	case '6':
   1156  1.1  christos 	case '7':
   1157  1.1  christos 	case '8':
   1158  1.1  christos 	case '9':
   1159  1.1  christos 	  if (digit_optind != 0 && digit_optind != this_option_optind)
   1160  1.1  christos 	    printf ("digits occur in two different argv-elements.\n");
   1161  1.1  christos 	  digit_optind = this_option_optind;
   1162  1.1  christos 	  printf ("option %c\n", c);
   1163  1.1  christos 	  break;
   1164  1.1  christos 
   1165  1.1  christos 	case 'a':
   1166  1.1  christos 	  printf ("option a\n");
   1167  1.1  christos 	  break;
   1168  1.1  christos 
   1169  1.1  christos 	case 'b':
   1170  1.1  christos 	  printf ("option b\n");
   1171  1.1  christos 	  break;
   1172  1.1  christos 
   1173  1.1  christos 	case 'c':
   1174  1.1  christos 	  printf ("option c with value `%s'\n", optarg);
   1175  1.1  christos 	  break;
   1176  1.1  christos 
   1177  1.1  christos 	case '?':
   1178  1.1  christos 	  break;
   1179  1.1  christos 
   1180  1.1  christos 	default:
   1181  1.1  christos 	  printf ("?? getopt returned character code 0%o ??\n", c);
   1182  1.1  christos 	}
   1183  1.1  christos     }
   1184  1.1  christos 
   1185  1.1  christos   if (optind < argc)
   1186  1.1  christos     {
   1187  1.1  christos       printf ("non-option ARGV-elements: ");
   1188  1.1  christos       while (optind < argc)
   1189  1.1  christos 	printf ("%s ", argv[optind++]);
   1190  1.1  christos       printf ("\n");
   1191  1.1  christos     }
   1192                
   1193                  exit (0);
   1194                }
   1195                
   1196                #endif /* TEST */
   1197