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