"Fossies" - the Fresh Open Source Software Archive

Member "bc-1.06.95/lib/getopt.c" (28 Mar 2006, 32860 Bytes) of package /linux/misc/old/bc-1.06.95.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "getopt.c" see the Fossies "Dox" file reference documentation.

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