Blame src/getopt.c

Packit 762fc5
/* Getopt for GNU.
Packit 762fc5
   NOTE: getopt is now part of the C library, so if you don't know what
Packit 762fc5
   "Keep this file name-space clean" means, talk to drepper@gnu.org
Packit 762fc5
   before changing it!
Packit 762fc5
Packit 762fc5
   Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98
Packit 762fc5
   	Free Software Foundation, Inc.
Packit 762fc5
Packit 762fc5
   The GNU C Library is free software; you can redistribute it and/or
Packit 762fc5
   modify it under the terms of the GNU Library General Public License as
Packit 762fc5
   published by the Free Software Foundation; either version 2 of the
Packit 762fc5
   License, or (at your option) any later version.
Packit 762fc5
Packit 762fc5
   The GNU C Library is distributed in the hope that it will be useful,
Packit 762fc5
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 762fc5
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 762fc5
   Library General Public License for more details.
Packit 762fc5
Packit 762fc5
   You should have received a copy of the GNU Library General Public
Packit 762fc5
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
Packit 762fc5
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit 762fc5
   Boston, MA 02111-1307, USA.  */
Packit 762fc5

Packit 762fc5
/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
Packit 762fc5
   Ditto for AIX 3.2 and <stdlib.h>.  */
Packit 762fc5
#ifndef _NO_PROTO
Packit 762fc5
# define _NO_PROTO
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
#include "aide.h"
Packit 762fc5
Packit 762fc5
#if !defined __STDC__ || !__STDC__
Packit 762fc5
/* This is a separate conditional since some stdc systems
Packit 762fc5
   reject `defined (const)'.  */
Packit 762fc5
# ifndef const
Packit 762fc5
#  define const
Packit 762fc5
# endif
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
#include <stdio.h>
Packit 762fc5
Packit 762fc5
/* Comment out all this code if we are using the GNU C Library, and are not
Packit 762fc5
   actually compiling the library itself.  This code is part of the GNU C
Packit 762fc5
   Library, but also included in many other GNU distributions.  Compiling
Packit 762fc5
   and linking in this code is a waste when using the GNU C library
Packit 762fc5
   (especially if it is a shared library).  Rather than having every GNU
Packit 762fc5
   program understand `configure --with-gnu-libc' and omit the object files,
Packit 762fc5
   it is simpler to just do this in the source for each such file.  */
Packit 762fc5
Packit 762fc5
#define GETOPT_INTERFACE_VERSION 2
Packit 762fc5
#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
Packit 762fc5
# include <gnu-versions.h>
Packit 762fc5
# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
Packit 762fc5
#  define ELIDE_CODE
Packit 762fc5
# endif
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
#ifndef ELIDE_CODE
Packit 762fc5
Packit 762fc5
Packit 762fc5
/* This needs to come after some library #include
Packit 762fc5
   to get __GNU_LIBRARY__ defined.  */
Packit 762fc5
#ifdef	__GNU_LIBRARY__
Packit 762fc5
/* Don't include stdlib.h for non-GNU C libraries because some of them
Packit 762fc5
   contain conflicting prototypes for getopt.  */
Packit 762fc5
# include <stdlib.h>
Packit 762fc5
# include <unistd.h>
Packit 762fc5
#endif	/* GNU C library.  */
Packit 762fc5
Packit 762fc5
#ifdef VMS
Packit 762fc5
# include <unixlib.h>
Packit 762fc5
# if HAVE_STRING_H - 0
Packit 762fc5
#  include <string.h>
Packit 762fc5
# endif
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
#ifndef _
Packit 762fc5
/* This is for other GNU distributions with internationalized messages.
Packit 762fc5
   When compiling libc, the _ macro is predefined.  */
Packit 762fc5
# ifdef HAVE_LIBINTL_H
Packit 762fc5
#  include <libintl.h>
Packit 762fc5
#  define _(msgid)	gettext (msgid)
Packit 762fc5
# else
Packit 762fc5
#  define _(msgid)	(msgid)
Packit 762fc5
# endif
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
/* This version of `getopt' appears to the caller like standard Unix `getopt'
Packit 762fc5
   but it behaves differently for the user, since it allows the user
Packit 762fc5
   to intersperse the options with the other arguments.
Packit 762fc5
Packit 762fc5
   As `getopt' works, it permutes the elements of ARGV so that,
Packit 762fc5
   when it is done, all the options precede everything else.  Thus
Packit 762fc5
   all application programs are extended to handle flexible argument order.
Packit 762fc5
Packit 762fc5
   Setting the environment variable POSIXLY_CORRECT disables permutation.
Packit 762fc5
   Then the behavior is completely standard.
Packit 762fc5
Packit 762fc5
   GNU application programs can use a third alternative mode in which
Packit 762fc5
   they can distinguish the relative order of options and other arguments.  */
Packit 762fc5
Packit 762fc5
#include "getopt.h"
Packit 762fc5
Packit 762fc5
/* For communication from `getopt' to the caller.
Packit 762fc5
   When `getopt' finds an option that takes an argument,
Packit 762fc5
   the argument value is returned here.
Packit 762fc5
   Also, when `ordering' is RETURN_IN_ORDER,
Packit 762fc5
   each non-option ARGV-element is returned here.  */
Packit 762fc5
Packit 762fc5
char *optarg = NULL;
Packit 762fc5
Packit 762fc5
/* Index in ARGV of the next element to be scanned.
Packit 762fc5
   This is used for communication to and from the caller
Packit 762fc5
   and for communication between successive calls to `getopt'.
Packit 762fc5
Packit 762fc5
   On entry to `getopt', zero means this is the first call; initialize.
Packit 762fc5
Packit 762fc5
   When `getopt' returns -1, this is the index of the first of the
Packit 762fc5
   non-option elements that the caller should itself scan.
Packit 762fc5
Packit 762fc5
   Otherwise, `optind' communicates from one call to the next
Packit 762fc5
   how much of ARGV has been scanned so far.  */
Packit 762fc5
Packit 762fc5
/* 1003.2 says this must be 1 before any call.  */
Packit 762fc5
int optind = 1;
Packit 762fc5
Packit 762fc5
/* Formerly, initialization of getopt depended on optind==0, which
Packit 762fc5
   causes problems with re-calling getopt as programs generally don't
Packit 762fc5
   know that. */
Packit 762fc5
Packit 762fc5
int __getopt_initialized = 0;
Packit 762fc5
Packit 762fc5
/* The next char to be scanned in the option-element
Packit 762fc5
   in which the last option character we returned was found.
Packit 762fc5
   This allows us to pick up the scan where we left off.
Packit 762fc5
Packit 762fc5
   If this is zero, or a null string, it means resume the scan
Packit 762fc5
   by advancing to the next ARGV-element.  */
Packit 762fc5
Packit 762fc5
static char *nextchar;
Packit 762fc5
Packit 762fc5
/* Callers store zero here to inhibit the error message
Packit 762fc5
   for unrecognized options.  */
Packit 762fc5
Packit 762fc5
int opterr = 1;
Packit 762fc5
Packit 762fc5
/* Set to an option character which was unrecognized.
Packit 762fc5
   This must be initialized on some systems to avoid linking in the
Packit 762fc5
   system's own getopt implementation.  */
Packit 762fc5
Packit 762fc5
int optopt = '?';
Packit 762fc5
Packit 762fc5
/* Describe how to deal with options that follow non-option ARGV-elements.
Packit 762fc5
Packit 762fc5
   If the caller did not specify anything,
Packit 762fc5
   the default is REQUIRE_ORDER if the environment variable
Packit 762fc5
   POSIXLY_CORRECT is defined, PERMUTE otherwise.
Packit 762fc5
Packit 762fc5
   REQUIRE_ORDER means don't recognize them as options;
Packit 762fc5
   stop option processing when the first non-option is seen.
Packit 762fc5
   This is what Unix does.
Packit 762fc5
   This mode of operation is selected by either setting the environment
Packit 762fc5
   variable POSIXLY_CORRECT, or using `+' as the first character
Packit 762fc5
   of the list of option characters.
Packit 762fc5
Packit 762fc5
   PERMUTE is the default.  We permute the contents of ARGV as we scan,
Packit 762fc5
   so that eventually all the non-options are at the end.  This allows options
Packit 762fc5
   to be given in any order, even with programs that were not written to
Packit 762fc5
   expect this.
Packit 762fc5
Packit 762fc5
   RETURN_IN_ORDER is an option available to programs that were written
Packit 762fc5
   to expect options and other ARGV-elements in any order and that care about
Packit 762fc5
   the ordering of the two.  We describe each non-option ARGV-element
Packit 762fc5
   as if it were the argument of an option with character code 1.
Packit 762fc5
   Using `-' as the first character of the list of option characters
Packit 762fc5
   selects this mode of operation.
Packit 762fc5
Packit 762fc5
   The special argument `--' forces an end of option-scanning regardless
Packit 762fc5
   of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
Packit 762fc5
   `--' can cause `getopt' to return -1 with `optind' != ARGC.  */
Packit 762fc5
Packit 762fc5
static enum
Packit 762fc5
{
Packit 762fc5
  REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
Packit 762fc5
} ordering;
Packit 762fc5
Packit 762fc5
/* Value of POSIXLY_CORRECT environment variable.  */
Packit 762fc5
static char *posixly_correct;
Packit 762fc5

Packit 762fc5
#ifdef	__GNU_LIBRARY__
Packit 762fc5
/* We want to avoid inclusion of string.h with non-GNU libraries
Packit 762fc5
   because there are many ways it can cause trouble.
Packit 762fc5
   On some systems, it contains special magic macros that don't work
Packit 762fc5
   in GCC.  */
Packit 762fc5
# include <string.h>
Packit 762fc5
# define my_index	strchr
Packit 762fc5
#else
Packit 762fc5
Packit 762fc5
# if HAVE_STRING_H
Packit 762fc5
#  include <string.h>
Packit 762fc5
# else
Packit 762fc5
#  include <strings.h>
Packit 762fc5
# endif
Packit 762fc5
Packit 762fc5
/* Avoid depending on library functions or files
Packit 762fc5
   whose names are inconsistent.  */
Packit 762fc5
Packit 762fc5
#ifndef getenv
Packit 762fc5
extern char *getenv ();
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
static char *
Packit 762fc5
my_index (str, chr)
Packit 762fc5
     const char *str;
Packit 762fc5
     int chr;
Packit 762fc5
{
Packit 762fc5
  while (*str)
Packit 762fc5
    {
Packit 762fc5
      if (*str == chr)
Packit 762fc5
	return (char *) str;
Packit 762fc5
      str++;
Packit 762fc5
    }
Packit 762fc5
  return 0;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
/* If using GCC, we can safely declare strlen this way.
Packit 762fc5
   If not using GCC, it is ok not to declare it.  */
Packit 762fc5
#ifdef __GNUC__
Packit 762fc5
/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
Packit 762fc5
   That was relevant to code that was here before.  */
Packit 762fc5
# if (!defined __STDC__ || !__STDC__) && !defined strlen
Packit 762fc5
/* gcc with -traditional declares the built-in strlen to return int,
Packit 762fc5
   and has done so at least since version 2.4.5. -- rms.  */
Packit 762fc5
extern int strlen (const char *);
Packit 762fc5
# endif /* not __STDC__ */
Packit 762fc5
#endif /* __GNUC__ */
Packit 762fc5
Packit 762fc5
#endif /* not __GNU_LIBRARY__ */
Packit 762fc5

Packit 762fc5
/* Handle permutation of arguments.  */
Packit 762fc5
Packit 762fc5
/* Describe the part of ARGV that contains non-options that have
Packit 762fc5
   been skipped.  `first_nonopt' is the index in ARGV of the first of them;
Packit 762fc5
   `last_nonopt' is the index after the last of them.  */
Packit 762fc5
Packit 762fc5
static int first_nonopt;
Packit 762fc5
static int last_nonopt;
Packit 762fc5
Packit 762fc5
#ifdef _LIBC
Packit 762fc5
/* Bash 2.0 gives us an environment variable containing flags
Packit 762fc5
   indicating ARGV elements that should not be considered arguments.  */
Packit 762fc5
Packit 762fc5
/* Defined in getopt_init.c  */
Packit 762fc5
extern char *__getopt_nonoption_flags;
Packit 762fc5
Packit 762fc5
static int nonoption_flags_max_len;
Packit 762fc5
static int nonoption_flags_len;
Packit 762fc5
Packit 762fc5
static int original_argc;
Packit 762fc5
static char *const *original_argv;
Packit 762fc5
Packit 762fc5
/* Make sure the environment variable bash 2.0 puts in the environment
Packit 762fc5
   is valid for the getopt call we must make sure that the ARGV passed
Packit 762fc5
   to getopt is that one passed to the process.  */
Packit 762fc5
static void
Packit 762fc5
__attribute__ ((unused))
Packit 762fc5
store_args_and_env (int argc, char *const *argv)
Packit 762fc5
{
Packit 762fc5
  /* XXX This is no good solution.  We should rather copy the args so
Packit 762fc5
     that we can compare them later.  But we must not use malloc(3).  */
Packit 762fc5
  original_argc = argc;
Packit 762fc5
  original_argv = argv;
Packit 762fc5
}
Packit 762fc5
# ifdef text_set_element
Packit 762fc5
text_set_element (__libc_subinit, store_args_and_env);
Packit 762fc5
# endif /* text_set_element */
Packit 762fc5
Packit 762fc5
# define SWAP_FLAGS(ch1, ch2) \
Packit 762fc5
  if (nonoption_flags_len > 0)						      \
Packit 762fc5
    {									      \
Packit 762fc5
      char __tmp = __getopt_nonoption_flags[ch1];			      \
Packit 762fc5
      __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];	      \
Packit 762fc5
      __getopt_nonoption_flags[ch2] = __tmp;				      \
Packit 762fc5
    }
Packit 762fc5
#else	/* !_LIBC */
Packit 762fc5
# define SWAP_FLAGS(ch1, ch2)
Packit 762fc5
#endif	/* _LIBC */
Packit 762fc5
Packit 762fc5
/* Exchange two adjacent subsequences of ARGV.
Packit 762fc5
   One subsequence is elements [first_nonopt,last_nonopt)
Packit 762fc5
   which contains all the non-options that have been skipped so far.
Packit 762fc5
   The other is elements [last_nonopt,optind), which contains all
Packit 762fc5
   the options processed since those non-options were skipped.
Packit 762fc5
Packit 762fc5
   `first_nonopt' and `last_nonopt' are relocated so that they describe
Packit 762fc5
   the new indices of the non-options in ARGV after they are moved.  */
Packit 762fc5
Packit 762fc5
#if defined __STDC__ && __STDC__
Packit 762fc5
static void exchange (char **);
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
static void
Packit 762fc5
exchange (argv)
Packit 762fc5
     char **argv;
Packit 762fc5
{
Packit 762fc5
  int bottom = first_nonopt;
Packit 762fc5
  int middle = last_nonopt;
Packit 762fc5
  int top = optind;
Packit 762fc5
  char *tem;
Packit 762fc5
Packit 762fc5
  /* Exchange the shorter segment with the far end of the longer segment.
Packit 762fc5
     That puts the shorter segment into the right place.
Packit 762fc5
     It leaves the longer segment in the right place overall,
Packit 762fc5
     but it consists of two parts that need to be swapped next.  */
Packit 762fc5
Packit 762fc5
#ifdef _LIBC
Packit 762fc5
  /* First make sure the handling of the `__getopt_nonoption_flags'
Packit 762fc5
     string can work normally.  Our top argument must be in the range
Packit 762fc5
     of the string.  */
Packit 762fc5
  if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
Packit 762fc5
    {
Packit 762fc5
      /* We must extend the array.  The user plays games with us and
Packit 762fc5
	 presents new arguments.  */
Packit 762fc5
      char *new_str = malloc (top + 1);
Packit 762fc5
      if (new_str == NULL)
Packit 762fc5
	nonoption_flags_len = nonoption_flags_max_len = 0;
Packit 762fc5
      else
Packit 762fc5
	{
Packit 762fc5
	  memset (__mempcpy (new_str, __getopt_nonoption_flags,
Packit 762fc5
			     nonoption_flags_max_len),
Packit 762fc5
		  '\0', top + 1 - nonoption_flags_max_len);
Packit 762fc5
	  nonoption_flags_max_len = top + 1;
Packit 762fc5
	  __getopt_nonoption_flags = new_str;
Packit 762fc5
	}
Packit 762fc5
    }
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
  while (top > middle && middle > bottom)
Packit 762fc5
    {
Packit 762fc5
      if (top - middle > middle - bottom)
Packit 762fc5
	{
Packit 762fc5
	  /* Bottom segment is the short one.  */
Packit 762fc5
	  int len = middle - bottom;
Packit 762fc5
	  register int i;
Packit 762fc5
Packit 762fc5
	  /* Swap it with the top part of the top segment.  */
Packit 762fc5
	  for (i = 0; i < len; i++)
Packit 762fc5
	    {
Packit 762fc5
	      tem = argv[bottom + i];
Packit 762fc5
	      argv[bottom + i] = argv[top - (middle - bottom) + i];
Packit 762fc5
	      argv[top - (middle - bottom) + i] = tem;
Packit 762fc5
	      SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
Packit 762fc5
	    }
Packit 762fc5
	  /* Exclude the moved bottom segment from further swapping.  */
Packit 762fc5
	  top -= len;
Packit 762fc5
	}
Packit 762fc5
      else
Packit 762fc5
	{
Packit 762fc5
	  /* Top segment is the short one.  */
Packit 762fc5
	  int len = top - middle;
Packit 762fc5
	  register int i;
Packit 762fc5
Packit 762fc5
	  /* Swap it with the bottom part of the bottom segment.  */
Packit 762fc5
	  for (i = 0; i < len; i++)
Packit 762fc5
	    {
Packit 762fc5
	      tem = argv[bottom + i];
Packit 762fc5
	      argv[bottom + i] = argv[middle + i];
Packit 762fc5
	      argv[middle + i] = tem;
Packit 762fc5
	      SWAP_FLAGS (bottom + i, middle + i);
Packit 762fc5
	    }
Packit 762fc5
	  /* Exclude the moved top segment from further swapping.  */
Packit 762fc5
	  bottom += len;
Packit 762fc5
	}
Packit 762fc5
    }
Packit 762fc5
Packit 762fc5
  /* Update records for the slots the non-options now occupy.  */
Packit 762fc5
Packit 762fc5
  first_nonopt += (optind - last_nonopt);
Packit 762fc5
  last_nonopt = optind;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
/* Initialize the internal data when the first call is made.  */
Packit 762fc5
Packit 762fc5
#if defined __STDC__ && __STDC__
Packit 762fc5
static const char *_getopt_initialize (int, char *const *, const char *);
Packit 762fc5
#endif
Packit 762fc5
static const char *
Packit 762fc5
_getopt_initialize (argc, argv, optstring)
Packit 762fc5
     int argc;
Packit 762fc5
     char *const *argv;
Packit 762fc5
     const char *optstring;
Packit 762fc5
{
Packit 762fc5
  /* Start processing options with ARGV-element 1 (since ARGV-element 0
Packit 762fc5
     is the program name); the sequence of previously skipped
Packit 762fc5
     non-option ARGV-elements is empty.  */
Packit 762fc5
Packit 762fc5
  first_nonopt = last_nonopt = optind;
Packit 762fc5
Packit 762fc5
  nextchar = NULL;
Packit 762fc5
Packit 762fc5
  posixly_correct = getenv ("POSIXLY_CORRECT");
Packit 762fc5
Packit 762fc5
  /* Determine how to handle the ordering of options and nonoptions.  */
Packit 762fc5
Packit 762fc5
  if (optstring[0] == '-')
Packit 762fc5
    {
Packit 762fc5
      ordering = RETURN_IN_ORDER;
Packit 762fc5
      ++optstring;
Packit 762fc5
    }
Packit 762fc5
  else if (optstring[0] == '+')
Packit 762fc5
    {
Packit 762fc5
      ordering = REQUIRE_ORDER;
Packit 762fc5
      ++optstring;
Packit 762fc5
    }
Packit 762fc5
  else if (posixly_correct != NULL)
Packit 762fc5
    ordering = REQUIRE_ORDER;
Packit 762fc5
  else
Packit 762fc5
    ordering = PERMUTE;
Packit 762fc5
Packit 762fc5
#ifdef _LIBC
Packit 762fc5
  if (posixly_correct == NULL
Packit 762fc5
      && argc == original_argc && argv == original_argv)
Packit 762fc5
    {
Packit 762fc5
      if (nonoption_flags_max_len == 0)
Packit 762fc5
	{
Packit 762fc5
	  if (__getopt_nonoption_flags == NULL
Packit 762fc5
	      || __getopt_nonoption_flags[0] == '\0')
Packit 762fc5
	    nonoption_flags_max_len = -1;
Packit 762fc5
	  else
Packit 762fc5
	    {
Packit 762fc5
	      const char *orig_str = __getopt_nonoption_flags;
Packit 762fc5
	      int len = nonoption_flags_max_len = strlen (orig_str);
Packit 762fc5
	      if (nonoption_flags_max_len < argc)
Packit 762fc5
		nonoption_flags_max_len = argc;
Packit 762fc5
	      __getopt_nonoption_flags =
Packit 762fc5
		(char *) malloc (nonoption_flags_max_len);
Packit 762fc5
	      if (__getopt_nonoption_flags == NULL)
Packit 762fc5
		nonoption_flags_max_len = -1;
Packit 762fc5
	      else
Packit 762fc5
		memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
Packit 762fc5
			'\0', nonoption_flags_max_len - len);
Packit 762fc5
	    }
Packit 762fc5
	}
Packit 762fc5
      nonoption_flags_len = nonoption_flags_max_len;
Packit 762fc5
    }
Packit 762fc5
  else
Packit 762fc5
    nonoption_flags_len = 0;
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
  return optstring;
Packit 762fc5
}
Packit 762fc5

Packit 762fc5
/* Scan elements of ARGV (whose length is ARGC) for option characters
Packit 762fc5
   given in OPTSTRING.
Packit 762fc5
Packit 762fc5
   If an element of ARGV starts with '-', and is not exactly "-" or "--",
Packit 762fc5
   then it is an option element.  The characters of this element
Packit 762fc5
   (aside from the initial '-') are option characters.  If `getopt'
Packit 762fc5
   is called repeatedly, it returns successively each of the option characters
Packit 762fc5
   from each of the option elements.
Packit 762fc5
Packit 762fc5
   If `getopt' finds another option character, it returns that character,
Packit 762fc5
   updating `optind' and `nextchar' so that the next call to `getopt' can
Packit 762fc5
   resume the scan with the following option character or ARGV-element.
Packit 762fc5
Packit 762fc5
   If there are no more option characters, `getopt' returns -1.
Packit 762fc5
   Then `optind' is the index in ARGV of the first ARGV-element
Packit 762fc5
   that is not an option.  (The ARGV-elements have been permuted
Packit 762fc5
   so that those that are not options now come last.)
Packit 762fc5
Packit 762fc5
   OPTSTRING is a string containing the legitimate option characters.
Packit 762fc5
   If an option character is seen that is not listed in OPTSTRING,
Packit 762fc5
   return '?' after printing an error message.  If you set `opterr' to
Packit 762fc5
   zero, the error message is suppressed but we still return '?'.
Packit 762fc5
Packit 762fc5
   If a char in OPTSTRING is followed by a colon, that means it wants an arg,
Packit 762fc5
   so the following text in the same ARGV-element, or the text of the following
Packit 762fc5
   ARGV-element, is returned in `optarg'.  Two colons mean an option that
Packit 762fc5
   wants an optional arg; if there is text in the current ARGV-element,
Packit 762fc5
   it is returned in `optarg', otherwise `optarg' is set to zero.
Packit 762fc5
Packit 762fc5
   If OPTSTRING starts with `-' or `+', it requests different methods of
Packit 762fc5
   handling the non-option ARGV-elements.
Packit 762fc5
   See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
Packit 762fc5
Packit 762fc5
   Long-named options begin with `--' instead of `-'.
Packit 762fc5
   Their names may be abbreviated as long as the abbreviation is unique
Packit 762fc5
   or is an exact match for some defined option.  If they have an
Packit 762fc5
   argument, it follows the option name in the same ARGV-element, separated
Packit 762fc5
   from the option name by a `=', or else the in next ARGV-element.
Packit 762fc5
   When `getopt' finds a long-named option, it returns 0 if that option's
Packit 762fc5
   `flag' field is nonzero, the value of the option's `val' field
Packit 762fc5
   if the `flag' field is zero.
Packit 762fc5
Packit 762fc5
   The elements of ARGV aren't really const, because we permute them.
Packit 762fc5
   But we pretend they're const in the prototype to be compatible
Packit 762fc5
   with other systems.
Packit 762fc5
Packit 762fc5
   LONGOPTS is a vector of `struct option' terminated by an
Packit 762fc5
   element containing a name which is zero.
Packit 762fc5
Packit 762fc5
   LONGIND returns the index in LONGOPT of the long-named option found.
Packit 762fc5
   It is only valid when a long-named option has been found by the most
Packit 762fc5
   recent call.
Packit 762fc5
Packit 762fc5
   If LONG_ONLY is nonzero, '-' as well as '--' can introduce
Packit 762fc5
   long-named options.  */
Packit 762fc5
Packit 762fc5
int
Packit 762fc5
_getopt_internal (argc, argv, optstring, longopts, longind, long_only)
Packit 762fc5
     int argc;
Packit 762fc5
     char *const *argv;
Packit 762fc5
     const char *optstring;
Packit 762fc5
     const struct option *longopts;
Packit 762fc5
     int *longind;
Packit 762fc5
     int long_only;
Packit 762fc5
{
Packit 762fc5
  optarg = NULL;
Packit 762fc5
Packit 762fc5
  if (optind == 0 || !__getopt_initialized)
Packit 762fc5
    {
Packit 762fc5
      if (optind == 0)
Packit 762fc5
	optind = 1;	/* Don't scan ARGV[0], the program name.  */
Packit 762fc5
      optstring = _getopt_initialize (argc, argv, optstring);
Packit 762fc5
      __getopt_initialized = 1;
Packit 762fc5
    }
Packit 762fc5
Packit 762fc5
  /* Test whether ARGV[optind] points to a non-option argument.
Packit 762fc5
     Either it does not have option syntax, or there is an environment flag
Packit 762fc5
     from the shell indicating it is not an option.  The later information
Packit 762fc5
     is only used when the used in the GNU libc.  */
Packit 762fc5
#ifdef _LIBC
Packit 762fc5
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'	      \
Packit 762fc5
		      || (optind < nonoption_flags_len			      \
Packit 762fc5
			  && __getopt_nonoption_flags[optind] == '1'))
Packit 762fc5
#else
Packit 762fc5
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
  if (nextchar == NULL || *nextchar == '\0')
Packit 762fc5
    {
Packit 762fc5
      /* Advance to the next ARGV-element.  */
Packit 762fc5
Packit 762fc5
      /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
Packit 762fc5
	 moved back by the user (who may also have changed the arguments).  */
Packit 762fc5
      if (last_nonopt > optind)
Packit 762fc5
	last_nonopt = optind;
Packit 762fc5
      if (first_nonopt > optind)
Packit 762fc5
	first_nonopt = optind;
Packit 762fc5
Packit 762fc5
      if (ordering == PERMUTE)
Packit 762fc5
	{
Packit 762fc5
	  /* If we have just processed some options following some non-options,
Packit 762fc5
	     exchange them so that the options come first.  */
Packit 762fc5
Packit 762fc5
	  if (first_nonopt != last_nonopt && last_nonopt != optind)
Packit 762fc5
	    exchange ((char **) argv);
Packit 762fc5
	  else if (last_nonopt != optind)
Packit 762fc5
	    first_nonopt = optind;
Packit 762fc5
Packit 762fc5
	  /* Skip any additional non-options
Packit 762fc5
	     and extend the range of non-options previously skipped.  */
Packit 762fc5
Packit 762fc5
	  while (optind < argc && NONOPTION_P)
Packit 762fc5
	    optind++;
Packit 762fc5
	  last_nonopt = optind;
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
      /* The special ARGV-element `--' means premature end of options.
Packit 762fc5
	 Skip it like a null option,
Packit 762fc5
	 then exchange with previous non-options as if it were an option,
Packit 762fc5
	 then skip everything else like a non-option.  */
Packit 762fc5
Packit 762fc5
      if (optind != argc && !strcmp (argv[optind], "--"))
Packit 762fc5
	{
Packit 762fc5
	  optind++;
Packit 762fc5
Packit 762fc5
	  if (first_nonopt != last_nonopt && last_nonopt != optind)
Packit 762fc5
	    exchange ((char **) argv);
Packit 762fc5
	  else if (first_nonopt == last_nonopt)
Packit 762fc5
	    first_nonopt = optind;
Packit 762fc5
	  last_nonopt = argc;
Packit 762fc5
Packit 762fc5
	  optind = argc;
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
      /* If we have done all the ARGV-elements, stop the scan
Packit 762fc5
	 and back over any non-options that we skipped and permuted.  */
Packit 762fc5
Packit 762fc5
      if (optind == argc)
Packit 762fc5
	{
Packit 762fc5
	  /* Set the next-arg-index to point at the non-options
Packit 762fc5
	     that we previously skipped, so the caller will digest them.  */
Packit 762fc5
	  if (first_nonopt != last_nonopt)
Packit 762fc5
	    optind = first_nonopt;
Packit 762fc5
	  return -1;
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
      /* If we have come to a non-option and did not permute it,
Packit 762fc5
	 either stop the scan or describe it to the caller and pass it by.  */
Packit 762fc5
Packit 762fc5
      if (NONOPTION_P)
Packit 762fc5
	{
Packit 762fc5
	  if (ordering == REQUIRE_ORDER)
Packit 762fc5
	    return -1;
Packit 762fc5
	  optarg = argv[optind++];
Packit 762fc5
	  return 1;
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
      /* We have found another option-ARGV-element.
Packit 762fc5
	 Skip the initial punctuation.  */
Packit 762fc5
Packit 762fc5
      nextchar = (argv[optind] + 1
Packit 762fc5
		  + (longopts != NULL && argv[optind][1] == '-'));
Packit 762fc5
    }
Packit 762fc5
Packit 762fc5
  /* Decode the current option-ARGV-element.  */
Packit 762fc5
Packit 762fc5
  /* Check whether the ARGV-element is a long option.
Packit 762fc5
Packit 762fc5
     If long_only and the ARGV-element has the form "-f", where f is
Packit 762fc5
     a valid short option, don't consider it an abbreviated form of
Packit 762fc5
     a long option that starts with f.  Otherwise there would be no
Packit 762fc5
     way to give the -f short option.
Packit 762fc5
Packit 762fc5
     On the other hand, if there's a long option "fubar" and
Packit 762fc5
     the ARGV-element is "-fu", do consider that an abbreviation of
Packit 762fc5
     the long option, just like "--fu", and not "-f" with arg "u".
Packit 762fc5
Packit 762fc5
     This distinction seems to be the most useful approach.  */
Packit 762fc5
Packit 762fc5
  if (longopts != NULL
Packit 762fc5
      && (argv[optind][1] == '-'
Packit 762fc5
	  || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
Packit 762fc5
    {
Packit 762fc5
      char *nameend;
Packit 762fc5
      const struct option *p;
Packit 762fc5
      const struct option *pfound = NULL;
Packit 762fc5
      int exact = 0;
Packit 762fc5
      int ambig = 0;
Packit 762fc5
      int indfound = -1;
Packit 762fc5
      int option_index;
Packit 762fc5
Packit 762fc5
      for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
Packit 762fc5
	/* Do nothing.  */ ;
Packit 762fc5
Packit 762fc5
      /* Test all long options for either exact match
Packit 762fc5
	 or abbreviated matches.  */
Packit 762fc5
      for (p = longopts, option_index = 0; p->name; p++, option_index++)
Packit 762fc5
	if (!strncmp (p->name, nextchar, nameend - nextchar))
Packit 762fc5
	  {
Packit 762fc5
	    if ((unsigned int) (nameend - nextchar)
Packit 762fc5
		== (unsigned int) strlen (p->name))
Packit 762fc5
	      {
Packit 762fc5
		/* Exact match found.  */
Packit 762fc5
		pfound = p;
Packit 762fc5
		indfound = option_index;
Packit 762fc5
		exact = 1;
Packit 762fc5
		break;
Packit 762fc5
	      }
Packit 762fc5
	    else if (pfound == NULL)
Packit 762fc5
	      {
Packit 762fc5
		/* First nonexact match found.  */
Packit 762fc5
		pfound = p;
Packit 762fc5
		indfound = option_index;
Packit 762fc5
	      }
Packit 762fc5
	    else
Packit 762fc5
	      /* Second or later nonexact match found.  */
Packit 762fc5
	      ambig = 1;
Packit 762fc5
	  }
Packit 762fc5
Packit 762fc5
      if (ambig && !exact)
Packit 762fc5
	{
Packit 762fc5
	  if (opterr)
Packit 762fc5
	    fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
Packit 762fc5
		     argv[0], argv[optind]);
Packit 762fc5
	  nextchar += strlen (nextchar);
Packit 762fc5
	  optind++;
Packit 762fc5
	  optopt = 0;
Packit 762fc5
	  return '?';
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
      if (pfound != NULL)
Packit 762fc5
	{
Packit 762fc5
	  option_index = indfound;
Packit 762fc5
	  optind++;
Packit 762fc5
	  if (*nameend)
Packit 762fc5
	    {
Packit 762fc5
	      /* Don't test has_arg with >, because some C compilers don't
Packit 762fc5
		 allow it to be used on enums.  */
Packit 762fc5
	      if (pfound->has_arg)
Packit 762fc5
		optarg = nameend + 1;
Packit 762fc5
	      else
Packit 762fc5
		{
Packit 762fc5
		  if (opterr)
Packit 762fc5
		    {
Packit 762fc5
		      if (argv[optind - 1][1] == '-')
Packit 762fc5
			/* --option */
Packit 762fc5
			fprintf (stderr,
Packit 762fc5
				 _("%s: option `--%s' doesn't allow an argument\n"),
Packit 762fc5
				 argv[0], pfound->name);
Packit 762fc5
		      else
Packit 762fc5
			/* +option or -option */
Packit 762fc5
			fprintf (stderr,
Packit 762fc5
				 _("%s: option `%c%s' doesn't allow an argument\n"),
Packit 762fc5
				 argv[0], argv[optind - 1][0], pfound->name);
Packit 762fc5
		    }
Packit 762fc5
Packit 762fc5
		  nextchar += strlen (nextchar);
Packit 762fc5
Packit 762fc5
		  optopt = pfound->val;
Packit 762fc5
		  return '?';
Packit 762fc5
		}
Packit 762fc5
	    }
Packit 762fc5
	  else if (pfound->has_arg == 1)
Packit 762fc5
	    {
Packit 762fc5
	      if (optind < argc)
Packit 762fc5
		optarg = argv[optind++];
Packit 762fc5
	      else
Packit 762fc5
		{
Packit 762fc5
		  if (opterr)
Packit 762fc5
		    fprintf (stderr,
Packit 762fc5
			   _("%s: option `%s' requires an argument\n"),
Packit 762fc5
			   argv[0], argv[optind - 1]);
Packit 762fc5
		  nextchar += strlen (nextchar);
Packit 762fc5
		  optopt = pfound->val;
Packit 762fc5
		  return optstring[0] == ':' ? ':' : '?';
Packit 762fc5
		}
Packit 762fc5
	    }
Packit 762fc5
	  nextchar += strlen (nextchar);
Packit 762fc5
	  if (longind != NULL)
Packit 762fc5
	    *longind = option_index;
Packit 762fc5
	  if (pfound->flag)
Packit 762fc5
	    {
Packit 762fc5
	      *(pfound->flag) = pfound->val;
Packit 762fc5
	      return 0;
Packit 762fc5
	    }
Packit 762fc5
	  return pfound->val;
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
      /* Can't find it as a long option.  If this is not getopt_long_only,
Packit 762fc5
	 or the option starts with '--' or is not a valid short
Packit 762fc5
	 option, then it's an error.
Packit 762fc5
	 Otherwise interpret it as a short option.  */
Packit 762fc5
      if (!long_only || argv[optind][1] == '-'
Packit 762fc5
	  || my_index (optstring, *nextchar) == NULL)
Packit 762fc5
	{
Packit 762fc5
	  if (opterr)
Packit 762fc5
	    {
Packit 762fc5
	      if (argv[optind][1] == '-')
Packit 762fc5
		/* --option */
Packit 762fc5
		fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
Packit 762fc5
			 argv[0], nextchar);
Packit 762fc5
	      else
Packit 762fc5
		/* +option or -option */
Packit 762fc5
		fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
Packit 762fc5
			 argv[0], argv[optind][0], nextchar);
Packit 762fc5
	    }
Packit 762fc5
	  nextchar = (char *) "";
Packit 762fc5
	  optind++;
Packit 762fc5
	  optopt = 0;
Packit 762fc5
	  return '?';
Packit 762fc5
	}
Packit 762fc5
    }
Packit 762fc5
Packit 762fc5
  /* Look at and handle the next short option-character.  */
Packit 762fc5
Packit 762fc5
  {
Packit 762fc5
    char c = *nextchar++;
Packit 762fc5
    char *temp = my_index (optstring, c);
Packit 762fc5
Packit 762fc5
    /* Increment `optind' when we start to process its last character.  */
Packit 762fc5
    if (*nextchar == '\0')
Packit 762fc5
      ++optind;
Packit 762fc5
Packit 762fc5
    if (temp == NULL || c == ':')
Packit 762fc5
      {
Packit 762fc5
	if (opterr)
Packit 762fc5
	  {
Packit 762fc5
	    if (posixly_correct)
Packit 762fc5
	      /* 1003.2 specifies the format of this message.  */
Packit 762fc5
	      fprintf (stderr, _("%s: illegal option -- %c\n"),
Packit 762fc5
		       argv[0], c);
Packit 762fc5
	    else
Packit 762fc5
	      fprintf (stderr, _("%s: invalid option -- %c\n"),
Packit 762fc5
		       argv[0], c);
Packit 762fc5
	  }
Packit 762fc5
	optopt = c;
Packit 762fc5
	return '?';
Packit 762fc5
      }
Packit 762fc5
    /* Convenience. Treat POSIX -W foo same as long option --foo */
Packit 762fc5
    if (temp[0] == 'W' && temp[1] == ';')
Packit 762fc5
      {
Packit 762fc5
	char *nameend;
Packit 762fc5
	const struct option *p;
Packit 762fc5
	const struct option *pfound = NULL;
Packit 762fc5
	int exact = 0;
Packit 762fc5
	int ambig = 0;
Packit 762fc5
	int indfound = 0;
Packit 762fc5
	int option_index;
Packit 762fc5
Packit 762fc5
	/* This is an option that requires an argument.  */
Packit 762fc5
	if (*nextchar != '\0')
Packit 762fc5
	  {
Packit 762fc5
	    optarg = nextchar;
Packit 762fc5
	    /* If we end this ARGV-element by taking the rest as an arg,
Packit 762fc5
	       we must advance to the next element now.  */
Packit 762fc5
	    optind++;
Packit 762fc5
	  }
Packit 762fc5
	else if (optind == argc)
Packit 762fc5
	  {
Packit 762fc5
	    if (opterr)
Packit 762fc5
	      {
Packit 762fc5
		/* 1003.2 specifies the format of this message.  */
Packit 762fc5
		fprintf (stderr, _("%s: option requires an argument -- %c\n"),
Packit 762fc5
			 argv[0], c);
Packit 762fc5
	      }
Packit 762fc5
	    optopt = c;
Packit 762fc5
	    if (optstring[0] == ':')
Packit 762fc5
	      c = ':';
Packit 762fc5
	    else
Packit 762fc5
	      c = '?';
Packit 762fc5
	    return c;
Packit 762fc5
	  }
Packit 762fc5
	else
Packit 762fc5
	  /* We already incremented `optind' once;
Packit 762fc5
	     increment it again when taking next ARGV-elt as argument.  */
Packit 762fc5
	  optarg = argv[optind++];
Packit 762fc5
Packit 762fc5
	/* optarg is now the argument, see if it's in the
Packit 762fc5
	   table of longopts.  */
Packit 762fc5
Packit 762fc5
	for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
Packit 762fc5
	  /* Do nothing.  */ ;
Packit 762fc5
Packit 762fc5
	/* Test all long options for either exact match
Packit 762fc5
	   or abbreviated matches.  */
Packit 762fc5
	for (p = longopts, option_index = 0; p->name; p++, option_index++)
Packit 762fc5
	  if (!strncmp (p->name, nextchar, nameend - nextchar))
Packit 762fc5
	    {
Packit 762fc5
	      if ((unsigned int) (nameend - nextchar) == strlen (p->name))
Packit 762fc5
		{
Packit 762fc5
		  /* Exact match found.  */
Packit 762fc5
		  pfound = p;
Packit 762fc5
		  indfound = option_index;
Packit 762fc5
		  exact = 1;
Packit 762fc5
		  break;
Packit 762fc5
		}
Packit 762fc5
	      else if (pfound == NULL)
Packit 762fc5
		{
Packit 762fc5
		  /* First nonexact match found.  */
Packit 762fc5
		  pfound = p;
Packit 762fc5
		  indfound = option_index;
Packit 762fc5
		}
Packit 762fc5
	      else
Packit 762fc5
		/* Second or later nonexact match found.  */
Packit 762fc5
		ambig = 1;
Packit 762fc5
	    }
Packit 762fc5
	if (ambig && !exact)
Packit 762fc5
	  {
Packit 762fc5
	    if (opterr)
Packit 762fc5
	      fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
Packit 762fc5
		       argv[0], argv[optind]);
Packit 762fc5
	    nextchar += strlen (nextchar);
Packit 762fc5
	    optind++;
Packit 762fc5
	    return '?';
Packit 762fc5
	  }
Packit 762fc5
	if (pfound != NULL)
Packit 762fc5
	  {
Packit 762fc5
	    option_index = indfound;
Packit 762fc5
	    if (*nameend)
Packit 762fc5
	      {
Packit 762fc5
		/* Don't test has_arg with >, because some C compilers don't
Packit 762fc5
		   allow it to be used on enums.  */
Packit 762fc5
		if (pfound->has_arg)
Packit 762fc5
		  optarg = nameend + 1;
Packit 762fc5
		else
Packit 762fc5
		  {
Packit 762fc5
		    if (opterr)
Packit 762fc5
		      fprintf (stderr, _("\
Packit 762fc5
%s: option `-W %s' doesn't allow an argument\n"),
Packit 762fc5
			       argv[0], pfound->name);
Packit 762fc5
Packit 762fc5
		    nextchar += strlen (nextchar);
Packit 762fc5
		    return '?';
Packit 762fc5
		  }
Packit 762fc5
	      }
Packit 762fc5
	    else if (pfound->has_arg == 1)
Packit 762fc5
	      {
Packit 762fc5
		if (optind < argc)
Packit 762fc5
		  optarg = argv[optind++];
Packit 762fc5
		else
Packit 762fc5
		  {
Packit 762fc5
		    if (opterr)
Packit 762fc5
		      fprintf (stderr,
Packit 762fc5
			       _("%s: option `%s' requires an argument\n"),
Packit 762fc5
			       argv[0], argv[optind - 1]);
Packit 762fc5
		    nextchar += strlen (nextchar);
Packit 762fc5
		    return optstring[0] == ':' ? ':' : '?';
Packit 762fc5
		  }
Packit 762fc5
	      }
Packit 762fc5
	    nextchar += strlen (nextchar);
Packit 762fc5
	    if (longind != NULL)
Packit 762fc5
	      *longind = option_index;
Packit 762fc5
	    if (pfound->flag)
Packit 762fc5
	      {
Packit 762fc5
		*(pfound->flag) = pfound->val;
Packit 762fc5
		return 0;
Packit 762fc5
	      }
Packit 762fc5
	    return pfound->val;
Packit 762fc5
	  }
Packit 762fc5
	  nextchar = NULL;
Packit 762fc5
	  return 'W';	/* Let the application handle it.   */
Packit 762fc5
      }
Packit 762fc5
    if (temp[1] == ':')
Packit 762fc5
      {
Packit 762fc5
	if (temp[2] == ':')
Packit 762fc5
	  {
Packit 762fc5
	    /* This is an option that accepts an argument optionally.  */
Packit 762fc5
	    if (*nextchar != '\0')
Packit 762fc5
	      {
Packit 762fc5
		optarg = nextchar;
Packit 762fc5
		optind++;
Packit 762fc5
	      }
Packit 762fc5
	    else
Packit 762fc5
	      optarg = NULL;
Packit 762fc5
	    nextchar = NULL;
Packit 762fc5
	  }
Packit 762fc5
	else
Packit 762fc5
	  {
Packit 762fc5
	    /* This is an option that requires an argument.  */
Packit 762fc5
	    if (*nextchar != '\0')
Packit 762fc5
	      {
Packit 762fc5
		optarg = nextchar;
Packit 762fc5
		/* If we end this ARGV-element by taking the rest as an arg,
Packit 762fc5
		   we must advance to the next element now.  */
Packit 762fc5
		optind++;
Packit 762fc5
	      }
Packit 762fc5
	    else if (optind == argc)
Packit 762fc5
	      {
Packit 762fc5
		if (opterr)
Packit 762fc5
		  {
Packit 762fc5
		    /* 1003.2 specifies the format of this message.  */
Packit 762fc5
		    fprintf (stderr,
Packit 762fc5
			   _("%s: option requires an argument -- %c\n"),
Packit 762fc5
			   argv[0], c);
Packit 762fc5
		  }
Packit 762fc5
		optopt = c;
Packit 762fc5
		if (optstring[0] == ':')
Packit 762fc5
		  c = ':';
Packit 762fc5
		else
Packit 762fc5
		  c = '?';
Packit 762fc5
	      }
Packit 762fc5
	    else
Packit 762fc5
	      /* We already incremented `optind' once;
Packit 762fc5
		 increment it again when taking next ARGV-elt as argument.  */
Packit 762fc5
	      optarg = argv[optind++];
Packit 762fc5
	    nextchar = NULL;
Packit 762fc5
	  }
Packit 762fc5
      }
Packit 762fc5
    return c;
Packit 762fc5
  }
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
int
Packit 762fc5
getopt (argc, argv, optstring)
Packit 762fc5
     int argc;
Packit 762fc5
     char *const *argv;
Packit 762fc5
     const char *optstring;
Packit 762fc5
{
Packit 762fc5
  return _getopt_internal (argc, argv, optstring,
Packit 762fc5
			   (const struct option *) 0,
Packit 762fc5
			   (int *) 0,
Packit 762fc5
			   0);
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
#endif	/* Not ELIDE_CODE.  */
Packit 762fc5

Packit 762fc5
#ifdef TEST
Packit 762fc5
Packit 762fc5
/* Compile with -DTEST to make an executable for use in testing
Packit 762fc5
   the above definition of `getopt'.  */
Packit 762fc5
Packit 762fc5
int
Packit 762fc5
main (argc, argv)
Packit 762fc5
     int argc;
Packit 762fc5
     char **argv;
Packit 762fc5
{
Packit 762fc5
  int c;
Packit 762fc5
  int digit_optind = 0;
Packit 762fc5
Packit 762fc5
  while (1)
Packit 762fc5
    {
Packit 762fc5
      int this_option_optind = optind ? optind : 1;
Packit 762fc5
Packit 762fc5
      c = getopt (argc, argv, "abc:d:0123456789");
Packit 762fc5
      if (c == -1)
Packit 762fc5
	break;
Packit 762fc5
Packit 762fc5
      switch (c)
Packit 762fc5
	{
Packit 762fc5
	case '0':
Packit 762fc5
	case '1':
Packit 762fc5
	case '2':
Packit 762fc5
	case '3':
Packit 762fc5
	case '4':
Packit 762fc5
	case '5':
Packit 762fc5
	case '6':
Packit 762fc5
	case '7':
Packit 762fc5
	case '8':
Packit 762fc5
	case '9':
Packit 762fc5
	  if (digit_optind != 0 && digit_optind != this_option_optind)
Packit 762fc5
	    printf ("digits occur in two different argv-elements.\n");
Packit 762fc5
	  digit_optind = this_option_optind;
Packit 762fc5
	  printf ("option %c\n", c);
Packit 762fc5
	  break;
Packit 762fc5
Packit 762fc5
	case 'a':
Packit 762fc5
	  printf ("option a\n");
Packit 762fc5
	  break;
Packit 762fc5
Packit 762fc5
	case 'b':
Packit 762fc5
	  printf ("option b\n");
Packit 762fc5
	  break;
Packit 762fc5
Packit 762fc5
	case 'c':
Packit 762fc5
	  printf ("option c with value `%s'\n", optarg);
Packit 762fc5
	  break;
Packit 762fc5
Packit 762fc5
	case '?':
Packit 762fc5
	  break;
Packit 762fc5
Packit 762fc5
	default:
Packit 762fc5
	  printf ("?? getopt returned character code 0%o ??\n", c);
Packit 762fc5
	}
Packit 762fc5
    }
Packit 762fc5
Packit 762fc5
  if (optind < argc)
Packit 762fc5
    {
Packit 762fc5
      printf ("non-option ARGV-elements: ");
Packit 762fc5
      while (optind < argc)
Packit 762fc5
	printf ("%s ", argv[optind++]);
Packit 762fc5
      printf ("\n");
Packit 762fc5
    }
Packit 762fc5
Packit 762fc5
  exit (0);
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
#endif /* TEST */