Blame support/getopt.c

Packit 575503
/* Getopt for GNU.
Packit 575503
   NOTE: getopt is part of the C library, so if you don't know what
Packit 575503
   "Keep this file name-space clean" means, talk to drepper@gnu.org
Packit 575503
   before changing it!
Packit 575503
   Copyright (C) 1987-2016 Free Software Foundation, Inc.
Packit 575503
   This file is part of the GNU C Library.
Packit 575503
Packit 575503
   The GNU C Library is free software; you can redistribute it and/or
Packit 575503
   modify it under the terms of the GNU Lesser General Public
Packit 575503
   License as published by the Free Software Foundation; either
Packit 575503
   version 2.1 of the License, or (at your option) any later version.
Packit 575503
Packit 575503
   The GNU C Library is distributed in the hope that it will be useful,
Packit 575503
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 575503
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 575503
   Lesser General Public License for more details.
Packit 575503
Packit 575503
   You should have received a copy of the GNU Lesser General Public
Packit 575503
   License along with the GNU C Library; if not, see
Packit 575503
   <http://www.gnu.org/licenses/>.  */
Packit 575503

Packit 575503
/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
Packit 575503
   Ditto for AIX 3.2 and <stdlib.h>.  */
Packit 575503
#ifndef _NO_PROTO
Packit 575503
# define _NO_PROTO
Packit 575503
#endif
Packit 575503
Packit 575503
#ifdef HAVE_CONFIG_H
Packit 575503
# include <config.h>
Packit 575503
#endif
Packit 575503
Packit 575503
#include <stdio.h>
Packit 575503
#include <stdlib.h>	/* For malloc and free */
Packit 575503
Packit 575503
/* Comment out all this code if we are using the GNU C Library, and are not
Packit 575503
   actually compiling the library itself.  This code is part of the GNU C
Packit 575503
   Library, but also included in many other GNU distributions.  Compiling
Packit 575503
   and linking in this code is a waste when using the GNU C library
Packit 575503
   (especially if it is a shared library).  Rather than having every GNU
Packit 575503
   program understand `configure --with-gnu-libc' and omit the object files,
Packit 575503
   it is simpler to just do this in the source for each such file.  */
Packit 575503
Packit 575503
#define GETOPT_INTERFACE_VERSION 2
Packit 575503
#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
Packit 575503
# include <gnu-versions.h>
Packit 575503
# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
Packit 575503
#  define ELIDE_CODE
Packit 575503
# endif
Packit 575503
#endif
Packit 575503
Packit 575503
/* !@#$%^&*() !!!!!!!! */
Packit 575503
#ifdef GAWK
Packit 575503
#undef ELIDE_CODE
Packit 575503
#endif
Packit 575503
Packit 575503
#ifndef ELIDE_CODE
Packit 575503
Packit 575503
Packit 575503
/* This needs to come after some library #include
Packit 575503
   to get __GNU_LIBRARY__ defined.  */
Packit 575503
#if defined (__GNU_LIBRARY__) || defined (__CYGWIN__) || defined(__DJGPP__) || defined(__APPLE__) || defined(__MINGW32__) || defined(__sun) /* Illumos */
Packit 575503
/* Don't include stdlib.h for
Packit 575503
 * non-GNU C libraries
Packit 575503
 * non-Cygwin
Packit 575503
 * non-DJGPP
Packit 575503
 * non-MinGW
Packit 575503
 * because some of them contain conflicting prototypes for getopt.  */
Packit 575503
# include <stdlib.h>
Packit 575503
# include <unistd.h>
Packit 575503
#endif	/* GNU C library.  */
Packit 575503
Packit 575503
#include <string.h>
Packit 575503
Packit 575503
#ifdef VMS
Packit 575503
# include <unixlib.h>
Packit 575503
#endif
Packit 575503
Packit 575503
#ifdef _LIBC
Packit 575503
# include <libintl.h>
Packit 575503
#else
Packit 575503
# include "gettext.h"
Packit 575503
# define _(msgid) gettext (msgid)
Packit 575503
#endif
Packit 575503
Packit 575503
#if defined _LIBC
Packit 575503
# include <wchar.h>
Packit 575503
#endif
Packit 575503
Packit 575503
#ifndef attribute_hidden
Packit 575503
# define attribute_hidden
Packit 575503
#endif
Packit 575503
Packit 575503
/* This version of `getopt' appears to the caller like standard Unix `getopt'
Packit 575503
   but it behaves differently for the user, since it allows the user
Packit 575503
   to intersperse the options with the other arguments.
Packit 575503
Packit 575503
   As `getopt' works, it permutes the elements of ARGV so that,
Packit 575503
   when it is done, all the options precede everything else.  Thus
Packit 575503
   all application programs are extended to handle flexible argument order.
Packit 575503
Packit 575503
   Setting the environment variable POSIXLY_CORRECT disables permutation.
Packit 575503
   Then the behavior is completely standard.
Packit 575503
Packit 575503
   GNU application programs can use a third alternative mode in which
Packit 575503
   they can distinguish the relative order of options and other arguments.  */
Packit 575503
Packit 575503
#include "getopt.h"
Packit 575503
#include "getopt_int.h"
Packit 575503
Packit 575503
/* For communication from `getopt' to the caller.
Packit 575503
   When `getopt' finds an option that takes an argument,
Packit 575503
   the argument value is returned here.
Packit 575503
   Also, when `ordering' is RETURN_IN_ORDER,
Packit 575503
   each non-option ARGV-element is returned here.  */
Packit 575503
Packit 575503
char *optarg;
Packit 575503
Packit 575503
/* Index in ARGV of the next element to be scanned.
Packit 575503
   This is used for communication to and from the caller
Packit 575503
   and for communication between successive calls to `getopt'.
Packit 575503
Packit 575503
   On entry to `getopt', zero means this is the first call; initialize.
Packit 575503
Packit 575503
   When `getopt' returns -1, this is the index of the first of the
Packit 575503
   non-option elements that the caller should itself scan.
Packit 575503
Packit 575503
   Otherwise, `optind' communicates from one call to the next
Packit 575503
   how much of ARGV has been scanned so far.  */
Packit 575503
Packit 575503
/* 1003.2 says this must be 1 before any call.  */
Packit 575503
int optind = 1;
Packit 575503
Packit 575503
/* Callers store zero here to inhibit the error message
Packit 575503
   for unrecognized options.  */
Packit 575503
Packit 575503
int opterr = 1;
Packit 575503
Packit 575503
/* Set to an option character which was unrecognized.
Packit 575503
   This must be initialized on some systems to avoid linking in the
Packit 575503
   system's own getopt implementation.  */
Packit 575503
Packit 575503
int optopt = '?';
Packit 575503
Packit 575503
/* Keep a global copy of all internal members of getopt_data.  */
Packit 575503
Packit 575503
static struct _getopt_data getopt_data;
Packit 575503
Packit 575503

Packit 575503
#ifndef __GNU_LIBRARY__
Packit 575503
Packit 575503
/* Avoid depending on library functions or files
Packit 575503
   whose names are inconsistent.  */
Packit 575503
Packit 575503
#ifndef getenv
Packit 575503
extern char *getenv ();
Packit 575503
#endif
Packit 575503
Packit 575503
#endif /* not __GNU_LIBRARY__ */
Packit 575503

Packit 575503
#ifdef _LIBC
Packit 575503
/* Stored original parameters.
Packit 575503
   XXX This is no good solution.  We should rather copy the args so
Packit 575503
   that we can compare them later.  But we must not use malloc(3).  */
Packit 575503
extern int __libc_argc;
Packit 575503
extern char **__libc_argv;
Packit 575503
Packit 575503
/* Bash 2.0 gives us an environment variable containing flags
Packit 575503
   indicating ARGV elements that should not be considered arguments.  */
Packit 575503
Packit 575503
# ifdef USE_NONOPTION_FLAGS
Packit 575503
/* Defined in getopt_init.c  */
Packit 575503
extern char *__getopt_nonoption_flags;
Packit 575503
# endif
Packit 575503
Packit 575503
# ifdef USE_NONOPTION_FLAGS
Packit 575503
#  define SWAP_FLAGS(ch1, ch2) \
Packit 575503
  if (d->__nonoption_flags_len > 0)					      \
Packit 575503
    {									      \
Packit 575503
      char __tmp = __getopt_nonoption_flags[ch1];			      \
Packit 575503
      __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];	      \
Packit 575503
      __getopt_nonoption_flags[ch2] = __tmp;				      \
Packit 575503
    }
Packit 575503
# else
Packit 575503
#  define SWAP_FLAGS(ch1, ch2)
Packit 575503
# endif
Packit 575503
#else	/* !_LIBC */
Packit 575503
# define SWAP_FLAGS(ch1, ch2)
Packit 575503
#endif	/* _LIBC */
Packit 575503
Packit 575503
/* Exchange two adjacent subsequences of ARGV.
Packit 575503
   One subsequence is elements [first_nonopt,last_nonopt)
Packit 575503
   which contains all the non-options that have been skipped so far.
Packit 575503
   The other is elements [last_nonopt,optind), which contains all
Packit 575503
   the options processed since those non-options were skipped.
Packit 575503
Packit 575503
   `first_nonopt' and `last_nonopt' are relocated so that they describe
Packit 575503
   the new indices of the non-options in ARGV after they are moved.  */
Packit 575503
Packit 575503
static void
Packit 575503
exchange (char **argv, struct _getopt_data *d)
Packit 575503
{
Packit 575503
  int bottom = d->__first_nonopt;
Packit 575503
  int middle = d->__last_nonopt;
Packit 575503
  int top = d->optind;
Packit 575503
  char *tem;
Packit 575503
Packit 575503
  /* Exchange the shorter segment with the far end of the longer segment.
Packit 575503
     That puts the shorter segment into the right place.
Packit 575503
     It leaves the longer segment in the right place overall,
Packit 575503
     but it consists of two parts that need to be swapped next.  */
Packit 575503
Packit 575503
#if defined _LIBC && defined USE_NONOPTION_FLAGS
Packit 575503
  /* First make sure the handling of the `__getopt_nonoption_flags'
Packit 575503
     string can work normally.  Our top argument must be in the range
Packit 575503
     of the string.  */
Packit 575503
  if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
Packit 575503
    {
Packit 575503
      /* We must extend the array.  The user plays games with us and
Packit 575503
	 presents new arguments.  */
Packit 575503
      char *new_str = malloc (top + 1);
Packit 575503
      if (new_str == NULL)
Packit 575503
	d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
Packit 575503
      else
Packit 575503
	{
Packit 575503
	  memset (__mempcpy (new_str, __getopt_nonoption_flags,
Packit 575503
			     d->__nonoption_flags_max_len),
Packit 575503
		  '\0', top + 1 - d->__nonoption_flags_max_len);
Packit 575503
	  d->__nonoption_flags_max_len = top + 1;
Packit 575503
	  __getopt_nonoption_flags = new_str;
Packit 575503
	}
Packit 575503
    }
Packit 575503
#endif
Packit 575503
Packit 575503
  while (top > middle && middle > bottom)
Packit 575503
    {
Packit 575503
      if (top - middle > middle - bottom)
Packit 575503
	{
Packit 575503
	  /* Bottom segment is the short one.  */
Packit 575503
	  int len = middle - bottom;
Packit 575503
	  int i;
Packit 575503
Packit 575503
	  /* Swap it with the top part of the top segment.  */
Packit 575503
	  for (i = 0; i < len; i++)
Packit 575503
	    {
Packit 575503
	      tem = argv[bottom + i];
Packit 575503
	      argv[bottom + i] = argv[top - (middle - bottom) + i];
Packit 575503
	      argv[top - (middle - bottom) + i] = tem;
Packit 575503
	      SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
Packit 575503
	    }
Packit 575503
	  /* Exclude the moved bottom segment from further swapping.  */
Packit 575503
	  top -= len;
Packit 575503
	}
Packit 575503
      else
Packit 575503
	{
Packit 575503
	  /* Top segment is the short one.  */
Packit 575503
	  int len = top - middle;
Packit 575503
	  int i;
Packit 575503
Packit 575503
	  /* Swap it with the bottom part of the bottom segment.  */
Packit 575503
	  for (i = 0; i < len; i++)
Packit 575503
	    {
Packit 575503
	      tem = argv[bottom + i];
Packit 575503
	      argv[bottom + i] = argv[middle + i];
Packit 575503
	      argv[middle + i] = tem;
Packit 575503
	      SWAP_FLAGS (bottom + i, middle + i);
Packit 575503
	    }
Packit 575503
	  /* Exclude the moved top segment from further swapping.  */
Packit 575503
	  bottom += len;
Packit 575503
	}
Packit 575503
    }
Packit 575503
Packit 575503
  /* Update records for the slots the non-options now occupy.  */
Packit 575503
Packit 575503
  d->__first_nonopt += (d->optind - d->__last_nonopt);
Packit 575503
  d->__last_nonopt = d->optind;
Packit 575503
}
Packit 575503
Packit 575503
/* Initialize the internal data when the first call is made.  */
Packit 575503
Packit 575503
static const char *
Packit 575503
_getopt_initialize (int argc, char *const *argv, const char *optstring,
Packit 575503
		    struct _getopt_data *d, int posixly_correct)
Packit 575503
{
Packit 575503
  /* Start processing options with ARGV-element 1 (since ARGV-element 0
Packit 575503
     is the program name); the sequence of previously skipped
Packit 575503
     non-option ARGV-elements is empty.  */
Packit 575503
Packit 575503
  d->__first_nonopt = d->__last_nonopt = d->optind;
Packit 575503
Packit 575503
  d->__nextchar = NULL;
Packit 575503
Packit 575503
  d->__posixly_correct = posixly_correct | !!getenv ("POSIXLY_CORRECT");
Packit 575503
Packit 575503
  /* Determine how to handle the ordering of options and nonoptions.  */
Packit 575503
Packit 575503
  if (optstring[0] == '-')
Packit 575503
    {
Packit 575503
      d->__ordering = RETURN_IN_ORDER;
Packit 575503
      ++optstring;
Packit 575503
    }
Packit 575503
  else if (optstring[0] == '+')
Packit 575503
    {
Packit 575503
      d->__ordering = REQUIRE_ORDER;
Packit 575503
      ++optstring;
Packit 575503
    }
Packit 575503
  else if (d->__posixly_correct)
Packit 575503
    d->__ordering = REQUIRE_ORDER;
Packit 575503
  else
Packit 575503
    d->__ordering = PERMUTE;
Packit 575503
Packit 575503
#if defined _LIBC && defined USE_NONOPTION_FLAGS
Packit 575503
  if (!d->__posixly_correct
Packit 575503
      && argc == __libc_argc && argv == __libc_argv)
Packit 575503
    {
Packit 575503
      if (d->__nonoption_flags_max_len == 0)
Packit 575503
	{
Packit 575503
	  if (__getopt_nonoption_flags == NULL
Packit 575503
	      || __getopt_nonoption_flags[0] == '\0')
Packit 575503
	    d->__nonoption_flags_max_len = -1;
Packit 575503
	  else
Packit 575503
	    {
Packit 575503
	      const char *orig_str = __getopt_nonoption_flags;
Packit 575503
	      int len = d->__nonoption_flags_max_len = strlen (orig_str);
Packit 575503
	      if (d->__nonoption_flags_max_len < argc)
Packit 575503
		d->__nonoption_flags_max_len = argc;
Packit 575503
	      __getopt_nonoption_flags =
Packit 575503
		(char *) malloc (d->__nonoption_flags_max_len);
Packit 575503
	      if (__getopt_nonoption_flags == NULL)
Packit 575503
		d->__nonoption_flags_max_len = -1;
Packit 575503
	      else
Packit 575503
		memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
Packit 575503
			'\0', d->__nonoption_flags_max_len - len);
Packit 575503
	    }
Packit 575503
	}
Packit 575503
      d->__nonoption_flags_len = d->__nonoption_flags_max_len;
Packit 575503
    }
Packit 575503
  else
Packit 575503
    d->__nonoption_flags_len = 0;
Packit 575503
#endif
Packit 575503
Packit 575503
  return optstring;
Packit 575503
}
Packit 575503

Packit 575503
/* Scan elements of ARGV (whose length is ARGC) for option characters
Packit 575503
   given in OPTSTRING.
Packit 575503
Packit 575503
   If an element of ARGV starts with '-', and is not exactly "-" or "--",
Packit 575503
   then it is an option element.  The characters of this element
Packit 575503
   (aside from the initial '-') are option characters.  If `getopt'
Packit 575503
   is called repeatedly, it returns successively each of the option characters
Packit 575503
   from each of the option elements.
Packit 575503
Packit 575503
   If `getopt' finds another option character, it returns that character,
Packit 575503
   updating `optind' and `nextchar' so that the next call to `getopt' can
Packit 575503
   resume the scan with the following option character or ARGV-element.
Packit 575503
Packit 575503
   If there are no more option characters, `getopt' returns -1.
Packit 575503
   Then `optind' is the index in ARGV of the first ARGV-element
Packit 575503
   that is not an option.  (The ARGV-elements have been permuted
Packit 575503
   so that those that are not options now come last.)
Packit 575503
Packit 575503
   OPTSTRING is a string containing the legitimate option characters.
Packit 575503
   If an option character is seen that is not listed in OPTSTRING,
Packit 575503
   return '?' after printing an error message.  If you set `opterr' to
Packit 575503
   zero, the error message is suppressed but we still return '?'.
Packit 575503
Packit 575503
   If a char in OPTSTRING is followed by a colon, that means it wants an arg,
Packit 575503
   so the following text in the same ARGV-element, or the text of the following
Packit 575503
   ARGV-element, is returned in `optarg'.  Two colons mean an option that
Packit 575503
   wants an optional arg; if there is text in the current ARGV-element,
Packit 575503
   it is returned in `optarg', otherwise `optarg' is set to zero.
Packit 575503
Packit 575503
   If OPTSTRING starts with `-' or `+', it requests different methods of
Packit 575503
   handling the non-option ARGV-elements.
Packit 575503
   See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
Packit 575503
Packit 575503
   Long-named options begin with `--' instead of `-'.
Packit 575503
   Their names may be abbreviated as long as the abbreviation is unique
Packit 575503
   or is an exact match for some defined option.  If they have an
Packit 575503
   argument, it follows the option name in the same ARGV-element, separated
Packit 575503
   from the option name by a `=', or else the in next ARGV-element.
Packit 575503
   When `getopt' finds a long-named option, it returns 0 if that option's
Packit 575503
   `flag' field is nonzero, the value of the option's `val' field
Packit 575503
   if the `flag' field is zero.
Packit 575503
Packit 575503
   The elements of ARGV aren't really const, because we permute them.
Packit 575503
   But we pretend they're const in the prototype to be compatible
Packit 575503
   with other systems.
Packit 575503
Packit 575503
   LONGOPTS is a vector of `struct option' terminated by an
Packit 575503
   element containing a name which is zero.
Packit 575503
Packit 575503
   LONGIND returns the index in LONGOPT of the long-named option found.
Packit 575503
   It is only valid when a long-named option has been found by the most
Packit 575503
   recent call.
Packit 575503
Packit 575503
   If LONG_ONLY is nonzero, '-' as well as '--' can introduce
Packit 575503
   long-named options.  */
Packit 575503
Packit 575503
int
Packit 575503
_getopt_internal_r (int argc, char *const *argv, const char *optstring,
Packit 575503
		    const struct option *longopts, int *longind,
Packit 575503
		    int long_only, struct _getopt_data *d, int posixly_correct)
Packit 575503
{
Packit 575503
  int print_errors = d->opterr;
Packit 575503
Packit 575503
  if (argc < 1)
Packit 575503
    return -1;
Packit 575503
Packit 575503
  d->optarg = NULL;
Packit 575503
Packit 575503
  if (d->optind == 0 || !d->__initialized)
Packit 575503
    {
Packit 575503
      if (d->optind == 0)
Packit 575503
	d->optind = 1;	/* Don't scan ARGV[0], the program name.  */
Packit 575503
      optstring = _getopt_initialize (argc, argv, optstring, d,
Packit 575503
				      posixly_correct);
Packit 575503
      d->__initialized = 1;
Packit 575503
    }
Packit 575503
  else if (optstring[0] == '-' || optstring[0] == '+')
Packit 575503
    optstring++;
Packit 575503
  if (optstring[0] == ':')
Packit 575503
    print_errors = 0;
Packit 575503
Packit 575503
  /* Test whether ARGV[optind] points to a non-option argument.
Packit 575503
     Either it does not have option syntax, or there is an environment flag
Packit 575503
     from the shell indicating it is not an option.  The later information
Packit 575503
     is only used when the used in the GNU libc.  */
Packit 575503
#if defined _LIBC && defined USE_NONOPTION_FLAGS
Packit 575503
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
Packit 575503
		      || (d->optind < d->__nonoption_flags_len		      \
Packit 575503
			  && __getopt_nonoption_flags[d->optind] == '1'))
Packit 575503
#else
Packit 575503
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
Packit 575503
#endif
Packit 575503
Packit 575503
  if (d->__nextchar == NULL || *d->__nextchar == '\0')
Packit 575503
    {
Packit 575503
      /* Advance to the next ARGV-element.  */
Packit 575503
Packit 575503
      /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
Packit 575503
	 moved back by the user (who may also have changed the arguments).  */
Packit 575503
      if (d->__last_nonopt > d->optind)
Packit 575503
	d->__last_nonopt = d->optind;
Packit 575503
      if (d->__first_nonopt > d->optind)
Packit 575503
	d->__first_nonopt = d->optind;
Packit 575503
Packit 575503
      if (d->__ordering == PERMUTE)
Packit 575503
	{
Packit 575503
	  /* If we have just processed some options following some non-options,
Packit 575503
	     exchange them so that the options come first.  */
Packit 575503
Packit 575503
	  if (d->__first_nonopt != d->__last_nonopt
Packit 575503
	      && d->__last_nonopt != d->optind)
Packit 575503
	    exchange ((char **) argv, d);
Packit 575503
	  else if (d->__last_nonopt != d->optind)
Packit 575503
	    d->__first_nonopt = d->optind;
Packit 575503
Packit 575503
	  /* Skip any additional non-options
Packit 575503
	     and extend the range of non-options previously skipped.  */
Packit 575503
Packit 575503
	  while (d->optind < argc && NONOPTION_P)
Packit 575503
	    d->optind++;
Packit 575503
	  d->__last_nonopt = d->optind;
Packit 575503
	}
Packit 575503
Packit 575503
      /* The special ARGV-element `--' means premature end of options.
Packit 575503
	 Skip it like a null option,
Packit 575503
	 then exchange with previous non-options as if it were an option,
Packit 575503
	 then skip everything else like a non-option.  */
Packit 575503
Packit 575503
      if (d->optind != argc && !strcmp (argv[d->optind], "--"))
Packit 575503
	{
Packit 575503
	  d->optind++;
Packit 575503
Packit 575503
	  if (d->__first_nonopt != d->__last_nonopt
Packit 575503
	      && d->__last_nonopt != d->optind)
Packit 575503
	    exchange ((char **) argv, d);
Packit 575503
	  else if (d->__first_nonopt == d->__last_nonopt)
Packit 575503
	    d->__first_nonopt = d->optind;
Packit 575503
	  d->__last_nonopt = argc;
Packit 575503
Packit 575503
	  d->optind = argc;
Packit 575503
	}
Packit 575503
Packit 575503
      /* If we have done all the ARGV-elements, stop the scan
Packit 575503
	 and back over any non-options that we skipped and permuted.  */
Packit 575503
Packit 575503
      if (d->optind == argc)
Packit 575503
	{
Packit 575503
	  /* Set the next-arg-index to point at the non-options
Packit 575503
	     that we previously skipped, so the caller will digest them.  */
Packit 575503
	  if (d->__first_nonopt != d->__last_nonopt)
Packit 575503
	    d->optind = d->__first_nonopt;
Packit 575503
	  return -1;
Packit 575503
	}
Packit 575503
Packit 575503
      /* If we have come to a non-option and did not permute it,
Packit 575503
	 either stop the scan or describe it to the caller and pass it by.  */
Packit 575503
Packit 575503
      if (NONOPTION_P)
Packit 575503
	{
Packit 575503
	  if (d->__ordering == REQUIRE_ORDER)
Packit 575503
	    return -1;
Packit 575503
	  d->optarg = argv[d->optind++];
Packit 575503
	  return 1;
Packit 575503
	}
Packit 575503
Packit 575503
      /* We have found another option-ARGV-element.
Packit 575503
	 Skip the initial punctuation.  */
Packit 575503
Packit 575503
      d->__nextchar = (argv[d->optind] + 1
Packit 575503
		  + (longopts != NULL && argv[d->optind][1] == '-'));
Packit 575503
    }
Packit 575503
Packit 575503
  /* Decode the current option-ARGV-element.  */
Packit 575503
Packit 575503
  /* Check whether the ARGV-element is a long option.
Packit 575503
Packit 575503
     If long_only and the ARGV-element has the form "-f", where f is
Packit 575503
     a valid short option, don't consider it an abbreviated form of
Packit 575503
     a long option that starts with f.  Otherwise there would be no
Packit 575503
     way to give the -f short option.
Packit 575503
Packit 575503
     On the other hand, if there's a long option "fubar" and
Packit 575503
     the ARGV-element is "-fu", do consider that an abbreviation of
Packit 575503
     the long option, just like "--fu", and not "-f" with arg "u".
Packit 575503
Packit 575503
     This distinction seems to be the most useful approach.  */
Packit 575503
Packit 575503
  if (longopts != NULL
Packit 575503
      && (argv[d->optind][1] == '-'
Packit 575503
	  || (long_only && (argv[d->optind][2]
Packit 575503
			    || !strchr (optstring, argv[d->optind][1])))))
Packit 575503
    {
Packit 575503
      char *nameend;
Packit 575503
      unsigned int namelen;
Packit 575503
      const struct option *p;
Packit 575503
      const struct option *pfound = NULL;
Packit 575503
      struct option_list
Packit 575503
      {
Packit 575503
	const struct option *p;
Packit 575503
	struct option_list *next;
Packit 575503
	int needs_free;
Packit 575503
      } *ambig_list = NULL;
Packit 575503
      int exact = 0;
Packit 575503
      int indfound = -1;
Packit 575503
      int option_index;
Packit 575503
Packit 575503
      for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
Packit 575503
	/* Do nothing.  */ ;
Packit 575503
      namelen = nameend - d->__nextchar;
Packit 575503
Packit 575503
      /* Test all long options for either exact match
Packit 575503
	 or abbreviated matches.  */
Packit 575503
      for (p = longopts, option_index = 0; p->name; p++, option_index++)
Packit 575503
	if (!strncmp (p->name, d->__nextchar, namelen))
Packit 575503
	  {
Packit 575503
	    if (namelen == (unsigned int) strlen (p->name))
Packit 575503
	      {
Packit 575503
		/* Exact match found.  */
Packit 575503
		pfound = p;
Packit 575503
		indfound = option_index;
Packit 575503
		exact = 1;
Packit 575503
		break;
Packit 575503
	      }
Packit 575503
	    else if (pfound == NULL)
Packit 575503
	      {
Packit 575503
		/* First nonexact match found.  */
Packit 575503
		pfound = p;
Packit 575503
		indfound = option_index;
Packit 575503
	      }
Packit 575503
	    else if (long_only
Packit 575503
		     || pfound->has_arg != p->has_arg
Packit 575503
		     || pfound->flag != p->flag
Packit 575503
		     || pfound->val != p->val)
Packit 575503
	      {
Packit 575503
		/* Second or later nonexact match found.  */
Packit 575503
		struct option_list *newp = malloc (sizeof (*newp));
Packit 575503
		newp->p = p;
Packit 575503
		newp->needs_free = 1;
Packit 575503
		newp->next = ambig_list;
Packit 575503
		ambig_list = newp;
Packit 575503
	      }
Packit 575503
	  }
Packit 575503
Packit 575503
      if (ambig_list != NULL && !exact)
Packit 575503
	{
Packit 575503
	  if (print_errors)
Packit 575503
	    {
Packit 575503
	      struct option_list first;
Packit 575503
	      first.p = pfound;
Packit 575503
	      first.next = ambig_list;
Packit 575503
	      first.needs_free = 0;
Packit 575503
	      ambig_list = &first;
Packit 575503
Packit 575503
#if defined _LIBC
Packit 575503
	      char *buf = NULL;
Packit 575503
	      size_t buflen = 0;
Packit 575503
Packit 575503
	      FILE *fp = __open_memstream (&buf, &buflen);
Packit 575503
	      if (fp != NULL)
Packit 575503
		{
Packit 575503
		  fprintf (fp,
Packit 575503
			   _("%s: option '%s' is ambiguous; possibilities:"),
Packit 575503
			   argv[0], argv[d->optind]);
Packit 575503
Packit 575503
		  do
Packit 575503
		    {
Packit 575503
		      fprintf (fp, " '--%s'", ambig_list->p->name);
Packit 575503
		      ambig_list = ambig_list->next;
Packit 575503
		    }
Packit 575503
		  while (ambig_list != NULL);
Packit 575503
Packit 575503
		  fputc_unlocked ('\n', fp);
Packit 575503
Packit 575503
		  if (__glibc_likely (fclose (fp) != EOF))
Packit 575503
		    {
Packit 575503
		      _IO_flockfile (stderr);
Packit 575503
Packit 575503
		      int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
Packit 575503
		      ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
Packit 575503
Packit 575503
		      __fxprintf (NULL, "%s", buf);
Packit 575503
Packit 575503
		      ((_IO_FILE *) stderr)->_flags2 = old_flags2;
Packit 575503
		      _IO_funlockfile (stderr);
Packit 575503
Packit 575503
		      free (buf);
Packit 575503
		    }
Packit 575503
		}
Packit 575503
#else
Packit 575503
	      fprintf (stderr,
Packit 575503
		       _("%s: option '%s' is ambiguous; possibilities:"),
Packit 575503
		       argv[0], argv[d->optind]);
Packit 575503
	      do
Packit 575503
		{
Packit 575503
		  struct option_list *tmp_next;
Packit 575503
Packit 575503
		  fprintf (stderr, " '--%s'", ambig_list->p->name);
Packit 575503
		  tmp_next = ambig_list->next;
Packit 575503
		  if (ambig_list->needs_free)
Packit 575503
		    free(ambig_list);
Packit 575503
		  ambig_list = tmp_next;
Packit 575503
		}
Packit 575503
	      while (ambig_list != NULL);
Packit 575503
Packit 575503
	      fputc ('\n', stderr);
Packit 575503
#endif
Packit 575503
	    }
Packit 575503
	  d->__nextchar += strlen (d->__nextchar);
Packit 575503
	  d->optind++;
Packit 575503
	  d->optopt = 0;
Packit 575503
	  return '?';
Packit 575503
	}
Packit 575503
Packit 575503
      if (pfound != NULL)
Packit 575503
	{
Packit 575503
	  option_index = indfound;
Packit 575503
	  d->optind++;
Packit 575503
	  if (*nameend)
Packit 575503
	    {
Packit 575503
	      /* Don't test has_arg with >, because some C compilers don't
Packit 575503
		 allow it to be used on enums.  */
Packit 575503
	      if (pfound->has_arg)
Packit 575503
		d->optarg = nameend + 1;
Packit 575503
	      else
Packit 575503
		{
Packit 575503
		  if (print_errors)
Packit 575503
		    {
Packit 575503
#if defined _LIBC
Packit 575503
		      char *buf;
Packit 575503
		      int n;
Packit 575503
#endif
Packit 575503
Packit 575503
		      if (argv[d->optind - 1][1] == '-')
Packit 575503
			{
Packit 575503
			  /* --option */
Packit 575503
#if defined _LIBC
Packit 575503
			  n = __asprintf (&buf, _("\
Packit 575503
%s: option '--%s' doesn't allow an argument\n"),
Packit 575503
					  argv[0], pfound->name);
Packit 575503
#else
Packit 575503
			  fprintf (stderr, _("\
Packit 575503
%s: option '--%s' doesn't allow an argument\n"),
Packit 575503
				   argv[0], pfound->name);
Packit 575503
#endif
Packit 575503
			}
Packit 575503
		      else
Packit 575503
			{
Packit 575503
			  /* +option or -option */
Packit 575503
#if defined _LIBC
Packit 575503
			  n = __asprintf (&buf, _("\
Packit 575503
%s: option '%c%s' doesn't allow an argument\n"),
Packit 575503
					  argv[0], argv[d->optind - 1][0],
Packit 575503
					  pfound->name);
Packit 575503
#else
Packit 575503
			  fprintf (stderr, _("\
Packit 575503
%s: option '%c%s' doesn't allow an argument\n"),
Packit 575503
				   argv[0], argv[d->optind - 1][0],
Packit 575503
				   pfound->name);
Packit 575503
#endif
Packit 575503
			}
Packit 575503
Packit 575503
#if defined _LIBC
Packit 575503
		      if (n >= 0)
Packit 575503
			{
Packit 575503
			  _IO_flockfile (stderr);
Packit 575503
Packit 575503
			  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
Packit 575503
			  ((_IO_FILE *) stderr)->_flags2
Packit 575503
			    |= _IO_FLAGS2_NOTCANCEL;
Packit 575503
Packit 575503
			  __fxprintf (NULL, "%s", buf);
Packit 575503
Packit 575503
			  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
Packit 575503
			  _IO_funlockfile (stderr);
Packit 575503
Packit 575503
			  free (buf);
Packit 575503
			}
Packit 575503
#endif
Packit 575503
		    }
Packit 575503
Packit 575503
		  d->__nextchar += strlen (d->__nextchar);
Packit 575503
Packit 575503
		  d->optopt = pfound->val;
Packit 575503
		  return '?';
Packit 575503
		}
Packit 575503
	    }
Packit 575503
	  else if (pfound->has_arg == 1)
Packit 575503
	    {
Packit 575503
	      if (d->optind < argc)
Packit 575503
		d->optarg = argv[d->optind++];
Packit 575503
	      else
Packit 575503
		{
Packit 575503
		  if (print_errors)
Packit 575503
		    {
Packit 575503
#if defined _LIBC
Packit 575503
		      char *buf;
Packit 575503
Packit 575503
		      if (__asprintf (&buf, _("\
Packit 575503
%s: option '--%s' requires an argument\n"),
Packit 575503
				      argv[0], pfound->name) >= 0)
Packit 575503
			{
Packit 575503
			  _IO_flockfile (stderr);
Packit 575503
Packit 575503
			  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
Packit 575503
			  ((_IO_FILE *) stderr)->_flags2
Packit 575503
			    |= _IO_FLAGS2_NOTCANCEL;
Packit 575503
Packit 575503
			  __fxprintf (NULL, "%s", buf);
Packit 575503
Packit 575503
			  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
Packit 575503
			  _IO_funlockfile (stderr);
Packit 575503
Packit 575503
			  free (buf);
Packit 575503
			}
Packit 575503
#else
Packit 575503
		      fprintf (stderr,
Packit 575503
			       _("%s: option '--%s' requires an argument\n"),
Packit 575503
			       argv[0], pfound->name);
Packit 575503
#endif
Packit 575503
		    }
Packit 575503
		  d->__nextchar += strlen (d->__nextchar);
Packit 575503
		  d->optopt = pfound->val;
Packit 575503
		  return optstring[0] == ':' ? ':' : '?';
Packit 575503
		}
Packit 575503
	    }
Packit 575503
	  d->__nextchar += strlen (d->__nextchar);
Packit 575503
	  if (longind != NULL)
Packit 575503
	    *longind = option_index;
Packit 575503
	  if (pfound->flag)
Packit 575503
	    {
Packit 575503
	      *(pfound->flag) = pfound->val;
Packit 575503
	      return 0;
Packit 575503
	    }
Packit 575503
	  return pfound->val;
Packit 575503
	}
Packit 575503
Packit 575503
      /* Can't find it as a long option.  If this is not getopt_long_only,
Packit 575503
	 or the option starts with '--' or is not a valid short
Packit 575503
	 option, then it's an error.
Packit 575503
	 Otherwise interpret it as a short option.  */
Packit 575503
      if (!long_only || argv[d->optind][1] == '-'
Packit 575503
	  || strchr (optstring, *d->__nextchar) == NULL)
Packit 575503
	{
Packit 575503
	  if (print_errors)
Packit 575503
	    {
Packit 575503
#if defined _LIBC
Packit 575503
	      char *buf;
Packit 575503
	      int n;
Packit 575503
#endif
Packit 575503
Packit 575503
	      if (argv[d->optind][1] == '-')
Packit 575503
		{
Packit 575503
		  /* --option */
Packit 575503
#if defined _LIBC
Packit 575503
		  n = __asprintf (&buf, _("%s: unrecognized option '--%s'\n"),
Packit 575503
				  argv[0], d->__nextchar);
Packit 575503
#else
Packit 575503
		  fprintf (stderr, _("%s: unrecognized option '--%s'\n"),
Packit 575503
			   argv[0], d->__nextchar);
Packit 575503
#endif
Packit 575503
		}
Packit 575503
	      else
Packit 575503
		{
Packit 575503
		  /* +option or -option */
Packit 575503
#if defined _LIBC
Packit 575503
		  n = __asprintf (&buf, _("%s: unrecognized option '%c%s'\n"),
Packit 575503
				  argv[0], argv[d->optind][0], d->__nextchar);
Packit 575503
#else
Packit 575503
		  fprintf (stderr, _("%s: unrecognized option '%c%s'\n"),
Packit 575503
			   argv[0], argv[d->optind][0], d->__nextchar);
Packit 575503
#endif
Packit 575503
		}
Packit 575503
Packit 575503
#if defined _LIBC
Packit 575503
	      if (n >= 0)
Packit 575503
		{
Packit 575503
		  _IO_flockfile (stderr);
Packit 575503
Packit 575503
		  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
Packit 575503
		  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
Packit 575503
Packit 575503
		  __fxprintf (NULL, "%s", buf);
Packit 575503
Packit 575503
		  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
Packit 575503
		  _IO_funlockfile (stderr);
Packit 575503
Packit 575503
		  free (buf);
Packit 575503
		}
Packit 575503
#endif
Packit 575503
	    }
Packit 575503
	  d->__nextchar = (char *) "";
Packit 575503
	  d->optind++;
Packit 575503
	  d->optopt = 0;
Packit 575503
	  return '?';
Packit 575503
	}
Packit 575503
    }
Packit 575503
Packit 575503
  /* Look at and handle the next short option-character.  */
Packit 575503
Packit 575503
  {
Packit 575503
    char c = *d->__nextchar++;
Packit 575503
    char *temp = strchr (optstring, c);
Packit 575503
Packit 575503
    /* Increment `optind' when we start to process its last character.  */
Packit 575503
    if (*d->__nextchar == '\0')
Packit 575503
      ++d->optind;
Packit 575503
Packit 575503
    if (temp == NULL || c == ':' || c == ';')
Packit 575503
      {
Packit 575503
	if (print_errors)
Packit 575503
	  {
Packit 575503
#if defined _LIBC
Packit 575503
	    char *buf;
Packit 575503
	    int n;
Packit 575503
#endif
Packit 575503
Packit 575503
#if defined _LIBC
Packit 575503
	    n = __asprintf (&buf, _("%s: invalid option -- '%c'\n"),
Packit 575503
			    argv[0], c);
Packit 575503
#else
Packit 575503
	    fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
Packit 575503
#endif
Packit 575503
Packit 575503
#if defined _LIBC
Packit 575503
	    if (n >= 0)
Packit 575503
	      {
Packit 575503
		_IO_flockfile (stderr);
Packit 575503
Packit 575503
		int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
Packit 575503
		((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
Packit 575503
Packit 575503
		__fxprintf (NULL, "%s", buf);
Packit 575503
Packit 575503
		((_IO_FILE *) stderr)->_flags2 = old_flags2;
Packit 575503
		_IO_funlockfile (stderr);
Packit 575503
Packit 575503
		free (buf);
Packit 575503
	      }
Packit 575503
#endif
Packit 575503
	  }
Packit 575503
	d->optopt = c;
Packit 575503
	return '?';
Packit 575503
      }
Packit 575503
    /* Convenience. Treat POSIX -W foo same as long option --foo */
Packit 575503
    if (temp[0] == 'W' && temp[1] == ';')
Packit 575503
      {
Packit 575503
	char *nameend;
Packit 575503
	const struct option *p;
Packit 575503
	const struct option *pfound = NULL;
Packit 575503
	int exact = 0;
Packit 575503
	int ambig = 0;
Packit 575503
	int indfound = 0;
Packit 575503
	int option_index;
Packit 575503
Packit 575503
	if (longopts == NULL)
Packit 575503
	  goto no_longs;
Packit 575503
Packit 575503
	/* This is an option that requires an argument.  */
Packit 575503
	if (*d->__nextchar != '\0')
Packit 575503
	  {
Packit 575503
	    d->optarg = d->__nextchar;
Packit 575503
	    /* If we end this ARGV-element by taking the rest as an arg,
Packit 575503
	       we must advance to the next element now.  */
Packit 575503
	    d->optind++;
Packit 575503
	  }
Packit 575503
	else if (d->optind == argc)
Packit 575503
	  {
Packit 575503
	    if (print_errors)
Packit 575503
	      {
Packit 575503
#if defined _LIBC
Packit 575503
		char *buf;
Packit 575503
Packit 575503
		if (__asprintf (&buf,
Packit 575503
				_("%s: option requires an argument -- '%c'\n"),
Packit 575503
				argv[0], c) >= 0)
Packit 575503
		  {
Packit 575503
		    _IO_flockfile (stderr);
Packit 575503
Packit 575503
		    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
Packit 575503
		    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
Packit 575503
Packit 575503
		    __fxprintf (NULL, "%s", buf);
Packit 575503
Packit 575503
		    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
Packit 575503
		    _IO_funlockfile (stderr);
Packit 575503
Packit 575503
		    free (buf);
Packit 575503
		  }
Packit 575503
#else
Packit 575503
		fprintf (stderr,
Packit 575503
			 _("%s: option requires an argument -- '%c'\n"),
Packit 575503
			 argv[0], c);
Packit 575503
#endif
Packit 575503
	      }
Packit 575503
	    d->optopt = c;
Packit 575503
	    if (optstring[0] == ':')
Packit 575503
	      c = ':';
Packit 575503
	    else
Packit 575503
	      c = '?';
Packit 575503
	    return c;
Packit 575503
	  }
Packit 575503
	else
Packit 575503
	  /* We already incremented `d->optind' once;
Packit 575503
	     increment it again when taking next ARGV-elt as argument.  */
Packit 575503
	  d->optarg = argv[d->optind++];
Packit 575503
Packit 575503
	/* optarg is now the argument, see if it's in the
Packit 575503
	   table of longopts.  */
Packit 575503
Packit 575503
	for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
Packit 575503
	     nameend++)
Packit 575503
	  /* Do nothing.  */ ;
Packit 575503
Packit 575503
	/* Test all long options for either exact match
Packit 575503
	   or abbreviated matches.  */
Packit 575503
	for (p = longopts, option_index = 0; p->name; p++, option_index++)
Packit 575503
	  if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
Packit 575503
	    {
Packit 575503
	      if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
Packit 575503
		{
Packit 575503
		  /* Exact match found.  */
Packit 575503
		  pfound = p;
Packit 575503
		  indfound = option_index;
Packit 575503
		  exact = 1;
Packit 575503
		  break;
Packit 575503
		}
Packit 575503
	      else if (pfound == NULL)
Packit 575503
		{
Packit 575503
		  /* First nonexact match found.  */
Packit 575503
		  pfound = p;
Packit 575503
		  indfound = option_index;
Packit 575503
		}
Packit 575503
	      else if (long_only
Packit 575503
		       || pfound->has_arg != p->has_arg
Packit 575503
		       || pfound->flag != p->flag
Packit 575503
		       || pfound->val != p->val)
Packit 575503
		/* Second or later nonexact match found.  */
Packit 575503
		ambig = 1;
Packit 575503
	    }
Packit 575503
	if (ambig && !exact)
Packit 575503
	  {
Packit 575503
	    if (print_errors)
Packit 575503
	      {
Packit 575503
#if defined _LIBC
Packit 575503
		char *buf;
Packit 575503
Packit 575503
		if (__asprintf (&buf, _("%s: option '-W %s' is ambiguous\n"),
Packit 575503
				argv[0], d->optarg) >= 0)
Packit 575503
		  {
Packit 575503
		    _IO_flockfile (stderr);
Packit 575503
Packit 575503
		    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
Packit 575503
		    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
Packit 575503
Packit 575503
		    __fxprintf (NULL, "%s", buf);
Packit 575503
Packit 575503
		    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
Packit 575503
		    _IO_funlockfile (stderr);
Packit 575503
Packit 575503
		    free (buf);
Packit 575503
		  }
Packit 575503
#else
Packit 575503
		fprintf (stderr, _("%s: option '-W %s' is ambiguous\n"),
Packit 575503
			 argv[0], d->optarg);
Packit 575503
#endif
Packit 575503
	      }
Packit 575503
	    d->__nextchar += strlen (d->__nextchar);
Packit 575503
	    d->optind++;
Packit 575503
	    return '?';
Packit 575503
	  }
Packit 575503
	if (pfound != NULL)
Packit 575503
	  {
Packit 575503
	    option_index = indfound;
Packit 575503
	    if (*nameend)
Packit 575503
	      {
Packit 575503
		/* Don't test has_arg with >, because some C compilers don't
Packit 575503
		   allow it to be used on enums.  */
Packit 575503
		if (pfound->has_arg)
Packit 575503
		  d->optarg = nameend + 1;
Packit 575503
		else
Packit 575503
		  {
Packit 575503
		    if (print_errors)
Packit 575503
		      {
Packit 575503
#if defined _LIBC
Packit 575503
			char *buf;
Packit 575503
Packit 575503
			if (__asprintf (&buf, _("\
Packit 575503
%s: option '-W %s' doesn't allow an argument\n"),
Packit 575503
					argv[0], pfound->name) >= 0)
Packit 575503
			  {
Packit 575503
			    _IO_flockfile (stderr);
Packit 575503
Packit 575503
			    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
Packit 575503
			    ((_IO_FILE *) stderr)->_flags2
Packit 575503
			      |= _IO_FLAGS2_NOTCANCEL;
Packit 575503
Packit 575503
			    __fxprintf (NULL, "%s", buf);
Packit 575503
Packit 575503
			    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
Packit 575503
			    _IO_funlockfile (stderr);
Packit 575503
Packit 575503
			    free (buf);
Packit 575503
			  }
Packit 575503
#else
Packit 575503
			fprintf (stderr, _("\
Packit 575503
%s: option '-W %s' doesn't allow an argument\n"),
Packit 575503
				 argv[0], pfound->name);
Packit 575503
#endif
Packit 575503
		      }
Packit 575503
Packit 575503
		    d->__nextchar += strlen (d->__nextchar);
Packit 575503
		    return '?';
Packit 575503
		  }
Packit 575503
	      }
Packit 575503
	    else if (pfound->has_arg == 1)
Packit 575503
	      {
Packit 575503
		if (d->optind < argc)
Packit 575503
		  d->optarg = argv[d->optind++];
Packit 575503
		else
Packit 575503
		  {
Packit 575503
		    if (print_errors)
Packit 575503
		      {
Packit 575503
#if defined _LIBC
Packit 575503
			char *buf;
Packit 575503
Packit 575503
			if (__asprintf (&buf, _("\
Packit 575503
%s: option '-W %s' requires an argument\n"),
Packit 575503
					argv[0], pfound->name) >= 0)
Packit 575503
			  {
Packit 575503
			    _IO_flockfile (stderr);
Packit 575503
Packit 575503
			    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
Packit 575503
			    ((_IO_FILE *) stderr)->_flags2
Packit 575503
			      |= _IO_FLAGS2_NOTCANCEL;
Packit 575503
Packit 575503
			    __fxprintf (NULL, "%s", buf);
Packit 575503
Packit 575503
			    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
Packit 575503
			    _IO_funlockfile (stderr);
Packit 575503
Packit 575503
			    free (buf);
Packit 575503
			  }
Packit 575503
#else
Packit 575503
			fprintf (stderr, _("\
Packit 575503
%s: option '-W %s' requires an argument\n"),
Packit 575503
				 argv[0], pfound->name);
Packit 575503
#endif
Packit 575503
		      }
Packit 575503
		    d->__nextchar += strlen (d->__nextchar);
Packit 575503
		    return optstring[0] == ':' ? ':' : '?';
Packit 575503
		  }
Packit 575503
	      }
Packit 575503
	    else
Packit 575503
	      d->optarg = NULL;
Packit 575503
	    d->__nextchar += strlen (d->__nextchar);
Packit 575503
	    if (longind != NULL)
Packit 575503
	      *longind = option_index;
Packit 575503
	    if (pfound->flag)
Packit 575503
	      {
Packit 575503
		*(pfound->flag) = pfound->val;
Packit 575503
		return 0;
Packit 575503
	      }
Packit 575503
	    return pfound->val;
Packit 575503
	  }
Packit 575503
Packit 575503
      no_longs:
Packit 575503
	d->__nextchar = NULL;
Packit 575503
	return 'W';	/* Let the application handle it.   */
Packit 575503
      }
Packit 575503
    if (temp[1] == ':')
Packit 575503
      {
Packit 575503
	if (temp[2] == ':')
Packit 575503
	  {
Packit 575503
	    /* This is an option that accepts an argument optionally.  */
Packit 575503
	    if (*d->__nextchar != '\0')
Packit 575503
	      {
Packit 575503
		d->optarg = d->__nextchar;
Packit 575503
		d->optind++;
Packit 575503
	      }
Packit 575503
	    else
Packit 575503
	      d->optarg = NULL;
Packit 575503
	    d->__nextchar = NULL;
Packit 575503
	  }
Packit 575503
	else
Packit 575503
	  {
Packit 575503
	    /* This is an option that requires an argument.  */
Packit 575503
	    if (*d->__nextchar != '\0')
Packit 575503
	      {
Packit 575503
		d->optarg = d->__nextchar;
Packit 575503
		/* If we end this ARGV-element by taking the rest as an arg,
Packit 575503
		   we must advance to the next element now.  */
Packit 575503
		d->optind++;
Packit 575503
	      }
Packit 575503
	    else if (d->optind == argc)
Packit 575503
	      {
Packit 575503
		if (print_errors)
Packit 575503
		  {
Packit 575503
#if defined _LIBC
Packit 575503
		    char *buf;
Packit 575503
Packit 575503
		    if (__asprintf (&buf, _("\
Packit 575503
%s: option requires an argument -- '%c'\n"),
Packit 575503
				    argv[0], c) >= 0)
Packit 575503
		      {
Packit 575503
			_IO_flockfile (stderr);
Packit 575503
Packit 575503
			int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
Packit 575503
			((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
Packit 575503
Packit 575503
			__fxprintf (NULL, "%s", buf);
Packit 575503
Packit 575503
			((_IO_FILE *) stderr)->_flags2 = old_flags2;
Packit 575503
			_IO_funlockfile (stderr);
Packit 575503
Packit 575503
			free (buf);
Packit 575503
		      }
Packit 575503
#else
Packit 575503
		    fprintf (stderr,
Packit 575503
			     _("%s: option requires an argument -- '%c'\n"),
Packit 575503
			     argv[0], c);
Packit 575503
#endif
Packit 575503
		  }
Packit 575503
		d->optopt = c;
Packit 575503
		if (optstring[0] == ':')
Packit 575503
		  c = ':';
Packit 575503
		else
Packit 575503
		  c = '?';
Packit 575503
	      }
Packit 575503
	    else
Packit 575503
	      /* We already incremented `optind' once;
Packit 575503
		 increment it again when taking next ARGV-elt as argument.  */
Packit 575503
	      d->optarg = argv[d->optind++];
Packit 575503
	    d->__nextchar = NULL;
Packit 575503
	  }
Packit 575503
      }
Packit 575503
    return c;
Packit 575503
  }
Packit 575503
}
Packit 575503
Packit 575503
int
Packit 575503
_getopt_internal (int argc, char *const *argv, const char *optstring,
Packit 575503
		  const struct option *longopts, int *longind, int long_only,
Packit 575503
		  int posixly_correct)
Packit 575503
{
Packit 575503
  int result;
Packit 575503
Packit 575503
  getopt_data.optind = optind;
Packit 575503
  getopt_data.opterr = opterr;
Packit 575503
Packit 575503
  result = _getopt_internal_r (argc, argv, optstring, longopts,
Packit 575503
			       longind, long_only, &getopt_data,
Packit 575503
			       posixly_correct);
Packit 575503
Packit 575503
  optind = getopt_data.optind;
Packit 575503
  optarg = getopt_data.optarg;
Packit 575503
  optopt = getopt_data.optopt;
Packit 575503
Packit 575503
  return result;
Packit 575503
}
Packit 575503
Packit 575503
int
Packit 575503
getopt (int argc, char *const *argv, const char *optstring)
Packit 575503
{
Packit 575503
  return _getopt_internal (argc, argv, optstring,
Packit 575503
			   (const struct option *) 0,
Packit 575503
			   (int *) 0,
Packit 575503
			   0, 0);
Packit 575503
}
Packit 575503
Packit 575503
#ifdef _LIBC
Packit 575503
int
Packit 575503
__posix_getopt (int argc, char *const *argv, const char *optstring)
Packit 575503
{
Packit 575503
  return _getopt_internal (argc, argv, optstring,
Packit 575503
			   (const struct option *) 0,
Packit 575503
			   (int *) 0,
Packit 575503
			   0, 1);
Packit 575503
}
Packit 575503
#endif
Packit 575503
Packit 575503
#endif	/* Not ELIDE_CODE.  */
Packit 575503

Packit 575503
#ifdef TEST
Packit 575503
Packit 575503
/* Compile with -DTEST to make an executable for use in testing
Packit 575503
   the above definition of `getopt'.  */
Packit 575503
Packit 575503
int
Packit 575503
main (int argc, char **argv)
Packit 575503
{
Packit 575503
  int c;
Packit 575503
  int digit_optind = 0;
Packit 575503
Packit 575503
  while (1)
Packit 575503
    {
Packit 575503
      int this_option_optind = optind ? optind : 1;
Packit 575503
Packit 575503
      c = getopt (argc, argv, "abc:d:0123456789");
Packit 575503
      if (c == -1)
Packit 575503
	break;
Packit 575503
Packit 575503
      switch (c)
Packit 575503
	{
Packit 575503
	case '0':
Packit 575503
	case '1':
Packit 575503
	case '2':
Packit 575503
	case '3':
Packit 575503
	case '4':
Packit 575503
	case '5':
Packit 575503
	case '6':
Packit 575503
	case '7':
Packit 575503
	case '8':
Packit 575503
	case '9':
Packit 575503
	  if (digit_optind != 0 && digit_optind != this_option_optind)
Packit 575503
	    printf ("digits occur in two different argv-elements.\n");
Packit 575503
	  digit_optind = this_option_optind;
Packit 575503
	  printf ("option %c\n", c);
Packit 575503
	  break;
Packit 575503
Packit 575503
	case 'a':
Packit 575503
	  printf ("option a\n");
Packit 575503
	  break;
Packit 575503
Packit 575503
	case 'b':
Packit 575503
	  printf ("option b\n");
Packit 575503
	  break;
Packit 575503
Packit 575503
	case 'c':
Packit 575503
	  printf ("option c with value '%s'\n", optarg);
Packit 575503
	  break;
Packit 575503
Packit 575503
	case '?':
Packit 575503
	  break;
Packit 575503
Packit 575503
	default:
Packit 575503
	  printf ("?? getopt returned character code 0%o ??\n", c);
Packit 575503
	}
Packit 575503
    }
Packit 575503
Packit 575503
  if (optind < argc)
Packit 575503
    {
Packit 575503
      printf ("non-option ARGV-elements: ");
Packit 575503
      while (optind < argc)
Packit 575503
	printf ("%s ", argv[optind++]);
Packit 575503
      printf ("\n");
Packit 575503
    }
Packit 575503
Packit 575503
  exit (0);
Packit 575503
}
Packit 575503
Packit 575503
#endif /* TEST */