Blame src/getopt.c

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