Blame tools/getopt.c

Packit 664db3
/* Getopt for GNU.
Packit 664db3
   NOTE: getopt is now part of the C library, so if you don't know what
Packit 664db3
   "Keep this file name-space clean" means, talk to drepper@gnu.org
Packit 664db3
   before changing it!
Packit 664db3

Packit 664db3
   Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
Packit 664db3
   	Free Software Foundation, Inc.
Packit 664db3

Packit 664db3
   The GNU C Library is free software; you can redistribute it and/or
Packit 664db3
   modify it under the terms of the GNU Library General Public License as
Packit 664db3
   published by the Free Software Foundation; either version 2 of the
Packit 664db3
   License, or (at your option) any later version.
Packit 664db3

Packit 664db3
   The GNU C Library is distributed in the hope that it will be useful,
Packit 664db3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 664db3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 664db3
   Library General Public License for more details.
Packit 664db3

Packit 664db3
   You should have received a copy of the GNU Library General Public
Packit 664db3
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
Packit 664db3
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit 664db3
   Boston, MA 02111-1307, USA.  */
Packit 664db3

Packit 664db3
/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
Packit 664db3
   Ditto for AIX 3.2 and <stdlib.h>.  */
Packit 664db3
#ifndef _NO_PROTO
Packit 664db3
# define _NO_PROTO
Packit 664db3
#endif
Packit 664db3

Packit 664db3
#ifdef HAVE_CONFIG_H
Packit 664db3
# include "config.h"
Packit 664db3
#endif
Packit 664db3

Packit 664db3
#if !defined __STDC__ || !__STDC__
Packit 664db3
/* This is a separate conditional since some stdc systems
Packit 664db3
   reject `defined (const)'.  */
Packit 664db3
# ifndef const
Packit 664db3
#  define const
Packit 664db3
# endif
Packit 664db3
#endif
Packit 664db3

Packit 664db3
#include <stdio.h>
Packit 664db3

Packit 664db3
/* Comment out all this code if we are using the GNU C Library, and are not
Packit 664db3
   actually compiling the library itself.  This code is part of the GNU C
Packit 664db3
   Library, but also included in many other GNU distributions.  Compiling
Packit 664db3
   and linking in this code is a waste when using the GNU C library
Packit 664db3
   (especially if it is a shared library).  Rather than having every GNU
Packit 664db3
   program understand `configure --with-gnu-libc' and omit the object files,
Packit 664db3
   it is simpler to just do this in the source for each such file.  */
Packit 664db3

Packit 664db3
#define GETOPT_INTERFACE_VERSION 2
Packit 664db3
#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
Packit 664db3
# include <gnu-versions.h>
Packit 664db3
# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
Packit 664db3
#  define ELIDE_CODE
Packit 664db3
# endif
Packit 664db3
#endif
Packit 664db3

Packit 664db3
#ifndef ELIDE_CODE
Packit 664db3

Packit 664db3

Packit 664db3
/* This needs to come after some library #include
Packit 664db3
   to get __GNU_LIBRARY__ defined.  */
Packit 664db3
#ifdef	__GNU_LIBRARY__
Packit 664db3
/* Don't include stdlib.h for non-GNU C libraries because some of them
Packit 664db3
   contain conflicting prototypes for getopt.  */
Packit 664db3
# include <stdlib.h>
Packit 664db3
# include <unistd.h>
Packit 664db3
#endif	/* GNU C library.  */
Packit 664db3

Packit 664db3
#ifdef VMS
Packit 664db3
# include <unixlib.h>
Packit 664db3
# if HAVE_STRING_H - 0
Packit 664db3
#  include <string.h>
Packit 664db3
# endif
Packit 664db3
#endif
Packit 664db3

Packit 664db3
#ifndef _
Packit 664db3
/* This is for other GNU distributions with internationalized messages.
Packit 664db3
   When compiling libc, the _ macro is predefined.  */
Packit 664db3
# ifdef HAVE_LIBINTL_H
Packit 664db3
#  include <libintl.h>
Packit 664db3
#  define _(msgid)	gettext (msgid)
Packit 664db3
# else
Packit 664db3
#  define _(msgid)	(msgid)
Packit 664db3
# endif
Packit 664db3
#endif
Packit 664db3

Packit 664db3
/* This version of `getopt' appears to the caller like standard Unix `getopt'
Packit 664db3
   but it behaves differently for the user, since it allows the user
Packit 664db3
   to intersperse the options with the other arguments.
Packit 664db3

Packit 664db3
   As `getopt' works, it permutes the elements of ARGV so that,
Packit 664db3
   when it is done, all the options precede everything else.  Thus
Packit 664db3
   all application programs are extended to handle flexible argument order.
Packit 664db3

Packit 664db3
   Setting the environment variable POSIXLY_CORRECT disables permutation.
Packit 664db3
   Then the behavior is completely standard.
Packit 664db3

Packit 664db3
   GNU application programs can use a third alternative mode in which
Packit 664db3
   they can distinguish the relative order of options and other arguments.  */
Packit 664db3

Packit 664db3
#include "getopt_win.h"
Packit 664db3

Packit 664db3
/* For communication from `getopt' to the caller.
Packit 664db3
   When `getopt' finds an option that takes an argument,
Packit 664db3
   the argument value is returned here.
Packit 664db3
   Also, when `ordering' is RETURN_IN_ORDER,
Packit 664db3
   each non-option ARGV-element is returned here.  */
Packit 664db3

Packit 664db3
char *optarg;
Packit 664db3

Packit 664db3
/* Index in ARGV of the next element to be scanned.
Packit 664db3
   This is used for communication to and from the caller
Packit 664db3
   and for communication between successive calls to `getopt'.
Packit 664db3

Packit 664db3
   On entry to `getopt', zero means this is the first call; initialize.
Packit 664db3

Packit 664db3
   When `getopt' returns -1, this is the index of the first of the
Packit 664db3
   non-option elements that the caller should itself scan.
Packit 664db3

Packit 664db3
   Otherwise, `optind' communicates from one call to the next
Packit 664db3
   how much of ARGV has been scanned so far.  */
Packit 664db3

Packit 664db3
/* 1003.2 says this must be 1 before any call.  */
Packit 664db3
int optind = 1;
Packit 664db3

Packit 664db3
/* Formerly, initialization of getopt depended on optind==0, which
Packit 664db3
   causes problems with re-calling getopt as programs generally don't
Packit 664db3
   know that. */
Packit 664db3

Packit 664db3
int __getopt_initialized;
Packit 664db3

Packit 664db3
/* The next char to be scanned in the option-element
Packit 664db3
   in which the last option character we returned was found.
Packit 664db3
   This allows us to pick up the scan where we left off.
Packit 664db3

Packit 664db3
   If this is zero, or a null string, it means resume the scan
Packit 664db3
   by advancing to the next ARGV-element.  */
Packit 664db3

Packit 664db3
static char *nextchar;
Packit 664db3

Packit 664db3
/* Callers store zero here to inhibit the error message
Packit 664db3
   for unrecognized options.  */
Packit 664db3

Packit 664db3
int opterr = 1;
Packit 664db3

Packit 664db3
/* Set to an option character which was unrecognized.
Packit 664db3
   This must be initialized on some systems to avoid linking in the
Packit 664db3
   system's own getopt implementation.  */
Packit 664db3

Packit 664db3
int optopt = '?';
Packit 664db3

Packit 664db3
/* Describe how to deal with options that follow non-option ARGV-elements.
Packit 664db3

Packit 664db3
   If the caller did not specify anything,
Packit 664db3
   the default is REQUIRE_ORDER if the environment variable
Packit 664db3
   POSIXLY_CORRECT is defined, PERMUTE otherwise.
Packit 664db3

Packit 664db3
   REQUIRE_ORDER means don't recognize them as options;
Packit 664db3
   stop option processing when the first non-option is seen.
Packit 664db3
   This is what Unix does.
Packit 664db3
   This mode of operation is selected by either setting the environment
Packit 664db3
   variable POSIXLY_CORRECT, or using `+' as the first character
Packit 664db3
   of the list of option characters.
Packit 664db3

Packit 664db3
   PERMUTE is the default.  We permute the contents of ARGV as we scan,
Packit 664db3
   so that eventually all the non-options are at the end.  This allows options
Packit 664db3
   to be given in any order, even with programs that were not written to
Packit 664db3
   expect this.
Packit 664db3

Packit 664db3
   RETURN_IN_ORDER is an option available to programs that were written
Packit 664db3
   to expect options and other ARGV-elements in any order and that care about
Packit 664db3
   the ordering of the two.  We describe each non-option ARGV-element
Packit 664db3
   as if it were the argument of an option with character code 1.
Packit 664db3
   Using `-' as the first character of the list of option characters
Packit 664db3
   selects this mode of operation.
Packit 664db3

Packit 664db3
   The special argument `--' forces an end of option-scanning regardless
Packit 664db3
   of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
Packit 664db3
   `--' can cause `getopt' to return -1 with `optind' != ARGC.  */
Packit 664db3

Packit 664db3
static enum
Packit 664db3
{
Packit 664db3
  REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
Packit 664db3
} ordering;
Packit 664db3

Packit 664db3
/* Value of POSIXLY_CORRECT environment variable.  */
Packit 664db3
static char *posixly_correct;
Packit 664db3

Packit 664db3
#ifdef	__GNU_LIBRARY__
Packit 664db3
/* We want to avoid inclusion of string.h with non-GNU libraries
Packit 664db3
   because there are many ways it can cause trouble.
Packit 664db3
   On some systems, it contains special magic macros that don't work
Packit 664db3
   in GCC.  */
Packit 664db3
# include <string.h>
Packit 664db3
# define my_index	strchr
Packit 664db3
#else
Packit 664db3

Packit 664db3
#include <string.h>
Packit 664db3

Packit 664db3
/* Avoid depending on library functions or files
Packit 664db3
   whose names are inconsistent.  */
Packit 664db3

Packit 664db3
#ifndef getenv
Packit 664db3
extern char *getenv ();
Packit 664db3
#endif
Packit 664db3

Packit 664db3
static char *
Packit 664db3
my_index (str, chr)
Packit 664db3
     const char *str;
Packit 664db3
     int chr;
Packit 664db3
{
Packit 664db3
  while (*str)
Packit 664db3
    {
Packit 664db3
      if (*str == chr)
Packit 664db3
	return (char *) str;
Packit 664db3
      str++;
Packit 664db3
    }
Packit 664db3
  return 0;
Packit 664db3
}
Packit 664db3

Packit 664db3
/* If using GCC, we can safely declare strlen this way.
Packit 664db3
   If not using GCC, it is ok not to declare it.  */
Packit 664db3
#ifdef __GNUC__
Packit 664db3
/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
Packit 664db3
   That was relevant to code that was here before.  */
Packit 664db3
# if (!defined __STDC__ || !__STDC__) && !defined strlen
Packit 664db3
/* gcc with -traditional declares the built-in strlen to return int,
Packit 664db3
   and has done so at least since version 2.4.5. -- rms.  */
Packit 664db3
extern int strlen (const char *);
Packit 664db3
# endif /* not __STDC__ */
Packit 664db3
#endif /* __GNUC__ */
Packit 664db3

Packit 664db3
#endif /* not __GNU_LIBRARY__ */
Packit 664db3

Packit 664db3
/* Handle permutation of arguments.  */
Packit 664db3

Packit 664db3
/* Describe the part of ARGV that contains non-options that have
Packit 664db3
   been skipped.  `first_nonopt' is the index in ARGV of the first of them;
Packit 664db3
   `last_nonopt' is the index after the last of them.  */
Packit 664db3

Packit 664db3
static int first_nonopt;
Packit 664db3
static int last_nonopt;
Packit 664db3

Packit 664db3
#ifdef _LIBC
Packit 664db3
/* Bash 2.0 gives us an environment variable containing flags
Packit 664db3
   indicating ARGV elements that should not be considered arguments.  */
Packit 664db3

Packit 664db3
/* Defined in getopt_init.c  */
Packit 664db3
extern char *__getopt_nonoption_flags;
Packit 664db3

Packit 664db3
static int nonoption_flags_max_len;
Packit 664db3
static int nonoption_flags_len;
Packit 664db3

Packit 664db3
static int original_argc;
Packit 664db3
static char *const *original_argv;
Packit 664db3

Packit 664db3
/* Make sure the environment variable bash 2.0 puts in the environment
Packit 664db3
   is valid for the getopt call we must make sure that the ARGV passed
Packit 664db3
   to getopt is that one passed to the process.  */
Packit 664db3
static void
Packit 664db3
__attribute__ ((unused))
Packit 664db3
store_args_and_env (int argc, char *const *argv)
Packit 664db3
{
Packit 664db3
  /* XXX This is no good solution.  We should rather copy the args so
Packit 664db3
     that we can compare them later.  But we must not use malloc(3).  */
Packit 664db3
  original_argc = argc;
Packit 664db3
  original_argv = argv;
Packit 664db3
}
Packit 664db3
# ifdef text_set_element
Packit 664db3
text_set_element (__libc_subinit, store_args_and_env);
Packit 664db3
# endif /* text_set_element */
Packit 664db3

Packit 664db3
# define SWAP_FLAGS(ch1, ch2) \
Packit 664db3
  if (nonoption_flags_len > 0)						      \
Packit 664db3
    {									      \
Packit 664db3
      char __tmp = __getopt_nonoption_flags[ch1];			      \
Packit 664db3
      __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];	      \
Packit 664db3
      __getopt_nonoption_flags[ch2] = __tmp;				      \
Packit 664db3
    }
Packit 664db3
#else	/* !_LIBC */
Packit 664db3
# define SWAP_FLAGS(ch1, ch2)
Packit 664db3
#endif	/* _LIBC */
Packit 664db3

Packit 664db3
/* Exchange two adjacent subsequences of ARGV.
Packit 664db3
   One subsequence is elements [first_nonopt,last_nonopt)
Packit 664db3
   which contains all the non-options that have been skipped so far.
Packit 664db3
   The other is elements [last_nonopt,optind), which contains all
Packit 664db3
   the options processed since those non-options were skipped.
Packit 664db3

Packit 664db3
   `first_nonopt' and `last_nonopt' are relocated so that they describe
Packit 664db3
   the new indices of the non-options in ARGV after they are moved.  */
Packit 664db3

Packit 664db3
#if defined __STDC__ && __STDC__
Packit 664db3
static void exchange (char **);
Packit 664db3
#endif
Packit 664db3

Packit 664db3
static void
Packit 664db3
exchange (argv)
Packit 664db3
     char **argv;
Packit 664db3
{
Packit 664db3
  int bottom = first_nonopt;
Packit 664db3
  int middle = last_nonopt;
Packit 664db3
  int top = optind;
Packit 664db3
  char *tem;
Packit 664db3

Packit 664db3
  /* Exchange the shorter segment with the far end of the longer segment.
Packit 664db3
     That puts the shorter segment into the right place.
Packit 664db3
     It leaves the longer segment in the right place overall,
Packit 664db3
     but it consists of two parts that need to be swapped next.  */
Packit 664db3

Packit 664db3
#ifdef _LIBC
Packit 664db3
  /* First make sure the handling of the `__getopt_nonoption_flags'
Packit 664db3
     string can work normally.  Our top argument must be in the range
Packit 664db3
     of the string.  */
Packit 664db3
  if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
Packit 664db3
    {
Packit 664db3
      /* We must extend the array.  The user plays games with us and
Packit 664db3
	 presents new arguments.  */
Packit 664db3
      char *new_str = malloc (top + 1);
Packit 664db3
      if (new_str == NULL)
Packit 664db3
	nonoption_flags_len = nonoption_flags_max_len = 0;
Packit 664db3
      else
Packit 664db3
	{
Packit 664db3
	  memset (__mempcpy (new_str, __getopt_nonoption_flags,
Packit 664db3
			     nonoption_flags_max_len),
Packit 664db3
		  '\0', top + 1 - nonoption_flags_max_len);
Packit 664db3
	  nonoption_flags_max_len = top + 1;
Packit 664db3
	  __getopt_nonoption_flags = new_str;
Packit 664db3
	}
Packit 664db3
    }
Packit 664db3
#endif
Packit 664db3

Packit 664db3
  while (top > middle && middle > bottom)
Packit 664db3
    {
Packit 664db3
      if (top - middle > middle - bottom)
Packit 664db3
	{
Packit 664db3
	  /* Bottom segment is the short one.  */
Packit 664db3
	  int len = middle - bottom;
Packit 664db3
	  register int i;
Packit 664db3

Packit 664db3
	  /* Swap it with the top part of the top segment.  */
Packit 664db3
	  for (i = 0; i < len; i++)
Packit 664db3
	    {
Packit 664db3
	      tem = argv[bottom + i];
Packit 664db3
	      argv[bottom + i] = argv[top - (middle - bottom) + i];
Packit 664db3
	      argv[top - (middle - bottom) + i] = tem;
Packit 664db3
	      SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
Packit 664db3
	    }
Packit 664db3
	  /* Exclude the moved bottom segment from further swapping.  */
Packit 664db3
	  top -= len;
Packit 664db3
	}
Packit 664db3
      else
Packit 664db3
	{
Packit 664db3
	  /* Top segment is the short one.  */
Packit 664db3
	  int len = top - middle;
Packit 664db3
	  register int i;
Packit 664db3

Packit 664db3
	  /* Swap it with the bottom part of the bottom segment.  */
Packit 664db3
	  for (i = 0; i < len; i++)
Packit 664db3
	    {
Packit 664db3
	      tem = argv[bottom + i];
Packit 664db3
	      argv[bottom + i] = argv[middle + i];
Packit 664db3
	      argv[middle + i] = tem;
Packit 664db3
	      SWAP_FLAGS (bottom + i, middle + i);
Packit 664db3
	    }
Packit 664db3
	  /* Exclude the moved top segment from further swapping.  */
Packit 664db3
	  bottom += len;
Packit 664db3
	}
Packit 664db3
    }
Packit 664db3

Packit 664db3
  /* Update records for the slots the non-options now occupy.  */
Packit 664db3

Packit 664db3
  first_nonopt += (optind - last_nonopt);
Packit 664db3
  last_nonopt = optind;
Packit 664db3
}
Packit 664db3

Packit 664db3
/* Initialize the internal data when the first call is made.  */
Packit 664db3

Packit 664db3
#if defined __STDC__ && __STDC__
Packit 664db3
static const char *_getopt_initialize (int, char *const *, const char *);
Packit 664db3
#endif
Packit 664db3
static const char *
Packit 664db3
_getopt_initialize (argc, argv, optstring)
Packit 664db3
     int argc;
Packit 664db3
     char *const *argv;
Packit 664db3
     const char *optstring;
Packit 664db3
{
Packit 664db3
  /* Start processing options with ARGV-element 1 (since ARGV-element 0
Packit 664db3
     is the program name); the sequence of previously skipped
Packit 664db3
     non-option ARGV-elements is empty.  */
Packit 664db3

Packit 664db3
  first_nonopt = last_nonopt = optind;
Packit 664db3

Packit 664db3
  nextchar = NULL;
Packit 664db3

Packit 664db3
  posixly_correct = getenv ("POSIXLY_CORRECT");
Packit 664db3

Packit 664db3
  /* Determine how to handle the ordering of options and nonoptions.  */
Packit 664db3

Packit 664db3
  if (optstring[0] == '-')
Packit 664db3
    {
Packit 664db3
      ordering = RETURN_IN_ORDER;
Packit 664db3
      ++optstring;
Packit 664db3
    }
Packit 664db3
  else if (optstring[0] == '+')
Packit 664db3
    {
Packit 664db3
      ordering = REQUIRE_ORDER;
Packit 664db3
      ++optstring;
Packit 664db3
    }
Packit 664db3
  else if (posixly_correct != NULL)
Packit 664db3
    ordering = REQUIRE_ORDER;
Packit 664db3
  else
Packit 664db3
    ordering = PERMUTE;
Packit 664db3

Packit 664db3
#ifdef _LIBC
Packit 664db3
  if (posixly_correct == NULL
Packit 664db3
      && argc == original_argc && argv == original_argv)
Packit 664db3
    {
Packit 664db3
      if (nonoption_flags_max_len == 0)
Packit 664db3
	{
Packit 664db3
	  if (__getopt_nonoption_flags == NULL
Packit 664db3
	      || __getopt_nonoption_flags[0] == '\0')
Packit 664db3
	    nonoption_flags_max_len = -1;
Packit 664db3
	  else
Packit 664db3
	    {
Packit 664db3
	      const char *orig_str = __getopt_nonoption_flags;
Packit 664db3
	      int len = nonoption_flags_max_len = strlen (orig_str);
Packit 664db3
	      if (nonoption_flags_max_len < argc)
Packit 664db3
		nonoption_flags_max_len = argc;
Packit 664db3
	      __getopt_nonoption_flags =
Packit 664db3
		(char *) malloc (nonoption_flags_max_len);
Packit 664db3
	      if (__getopt_nonoption_flags == NULL)
Packit 664db3
		nonoption_flags_max_len = -1;
Packit 664db3
	      else
Packit 664db3
		memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
Packit 664db3
			'\0', nonoption_flags_max_len - len);
Packit 664db3
	    }
Packit 664db3
	}
Packit 664db3
      nonoption_flags_len = nonoption_flags_max_len;
Packit 664db3
    }
Packit 664db3
  else
Packit 664db3
    nonoption_flags_len = 0;
Packit 664db3
#endif
Packit 664db3

Packit 664db3
  return optstring;
Packit 664db3
}
Packit 664db3

Packit 664db3
/* Scan elements of ARGV (whose length is ARGC) for option characters
Packit 664db3
   given in OPTSTRING.
Packit 664db3

Packit 664db3
   If an element of ARGV starts with '-', and is not exactly "-" or "--",
Packit 664db3
   then it is an option element.  The characters of this element
Packit 664db3
   (aside from the initial '-') are option characters.  If `getopt'
Packit 664db3
   is called repeatedly, it returns successively each of the option characters
Packit 664db3
   from each of the option elements.
Packit 664db3

Packit 664db3
   If `getopt' finds another option character, it returns that character,
Packit 664db3
   updating `optind' and `nextchar' so that the next call to `getopt' can
Packit 664db3
   resume the scan with the following option character or ARGV-element.
Packit 664db3

Packit 664db3
   If there are no more option characters, `getopt' returns -1.
Packit 664db3
   Then `optind' is the index in ARGV of the first ARGV-element
Packit 664db3
   that is not an option.  (The ARGV-elements have been permuted
Packit 664db3
   so that those that are not options now come last.)
Packit 664db3

Packit 664db3
   OPTSTRING is a string containing the legitimate option characters.
Packit 664db3
   If an option character is seen that is not listed in OPTSTRING,
Packit 664db3
   return '?' after printing an error message.  If you set `opterr' to
Packit 664db3
   zero, the error message is suppressed but we still return '?'.
Packit 664db3

Packit 664db3
   If a char in OPTSTRING is followed by a colon, that means it wants an arg,
Packit 664db3
   so the following text in the same ARGV-element, or the text of the following
Packit 664db3
   ARGV-element, is returned in `optarg'.  Two colons mean an option that
Packit 664db3
   wants an optional arg; if there is text in the current ARGV-element,
Packit 664db3
   it is returned in `optarg', otherwise `optarg' is set to zero.
Packit 664db3

Packit 664db3
   If OPTSTRING starts with `-' or `+', it requests different methods of
Packit 664db3
   handling the non-option ARGV-elements.
Packit 664db3
   See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
Packit 664db3

Packit 664db3
   Long-named options begin with `--' instead of `-'.
Packit 664db3
   Their names may be abbreviated as long as the abbreviation is unique
Packit 664db3
   or is an exact match for some defined option.  If they have an
Packit 664db3
   argument, it follows the option name in the same ARGV-element, separated
Packit 664db3
   from the option name by a `=', or else the in next ARGV-element.
Packit 664db3
   When `getopt' finds a long-named option, it returns 0 if that option's
Packit 664db3
   `flag' field is nonzero, the value of the option's `val' field
Packit 664db3
   if the `flag' field is zero.
Packit 664db3

Packit 664db3
   The elements of ARGV aren't really const, because we permute them.
Packit 664db3
   But we pretend they're const in the prototype to be compatible
Packit 664db3
   with other systems.
Packit 664db3

Packit 664db3
   LONGOPTS is a vector of `struct option' terminated by an
Packit 664db3
   element containing a name which is zero.
Packit 664db3

Packit 664db3
   LONGIND returns the index in LONGOPT of the long-named option found.
Packit 664db3
   It is only valid when a long-named option has been found by the most
Packit 664db3
   recent call.
Packit 664db3

Packit 664db3
   If LONG_ONLY is nonzero, '-' as well as '--' can introduce
Packit 664db3
   long-named options.  */
Packit 664db3

Packit 664db3
int
Packit 664db3
_getopt_internal (argc, argv, optstring, longopts, longind, long_only)
Packit 664db3
     int argc;
Packit 664db3
     char *const *argv;
Packit 664db3
     const char *optstring;
Packit 664db3
     const struct option *longopts;
Packit 664db3
     int *longind;
Packit 664db3
     int long_only;
Packit 664db3
{
Packit 664db3
  optarg = NULL;
Packit 664db3

Packit 664db3
  if (optind == 0 || !__getopt_initialized)
Packit 664db3
    {
Packit 664db3
      if (optind == 0)
Packit 664db3
	optind = 1;	/* Don't scan ARGV[0], the program name.  */
Packit 664db3
      optstring = _getopt_initialize (argc, argv, optstring);
Packit 664db3
      __getopt_initialized = 1;
Packit 664db3
    }
Packit 664db3

Packit 664db3
  /* Test whether ARGV[optind] points to a non-option argument.
Packit 664db3
     Either it does not have option syntax, or there is an environment flag
Packit 664db3
     from the shell indicating it is not an option.  The later information
Packit 664db3
     is only used when the used in the GNU libc.  */
Packit 664db3
#ifdef _LIBC
Packit 664db3
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'	      \
Packit 664db3
		      || (optind < nonoption_flags_len			      \
Packit 664db3
			  && __getopt_nonoption_flags[optind] == '1'))
Packit 664db3
#else
Packit 664db3
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
Packit 664db3
#endif
Packit 664db3

Packit 664db3
  if (nextchar == NULL || *nextchar == '\0')
Packit 664db3
    {
Packit 664db3
      /* Advance to the next ARGV-element.  */
Packit 664db3

Packit 664db3
      /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
Packit 664db3
	 moved back by the user (who may also have changed the arguments).  */
Packit 664db3
      if (last_nonopt > optind)
Packit 664db3
	last_nonopt = optind;
Packit 664db3
      if (first_nonopt > optind)
Packit 664db3
	first_nonopt = optind;
Packit 664db3

Packit 664db3
      if (ordering == PERMUTE)
Packit 664db3
	{
Packit 664db3
	  /* If we have just processed some options following some non-options,
Packit 664db3
	     exchange them so that the options come first.  */
Packit 664db3

Packit 664db3
	  if (first_nonopt != last_nonopt && last_nonopt != optind)
Packit 664db3
	    exchange ((char **) argv);
Packit 664db3
	  else if (last_nonopt != optind)
Packit 664db3
	    first_nonopt = optind;
Packit 664db3

Packit 664db3
	  /* Skip any additional non-options
Packit 664db3
	     and extend the range of non-options previously skipped.  */
Packit 664db3

Packit 664db3
	  while (optind < argc && NONOPTION_P)
Packit 664db3
	    optind++;
Packit 664db3
	  last_nonopt = optind;
Packit 664db3
	}
Packit 664db3

Packit 664db3
      /* The special ARGV-element `--' means premature end of options.
Packit 664db3
	 Skip it like a null option,
Packit 664db3
	 then exchange with previous non-options as if it were an option,
Packit 664db3
	 then skip everything else like a non-option.  */
Packit 664db3

Packit 664db3
      if (optind != argc && !strcmp (argv[optind], "--"))
Packit 664db3
	{
Packit 664db3
	  optind++;
Packit 664db3

Packit 664db3
	  if (first_nonopt != last_nonopt && last_nonopt != optind)
Packit 664db3
	    exchange ((char **) argv);
Packit 664db3
	  else if (first_nonopt == last_nonopt)
Packit 664db3
	    first_nonopt = optind;
Packit 664db3
	  last_nonopt = argc;
Packit 664db3

Packit 664db3
	  optind = argc;
Packit 664db3
	}
Packit 664db3

Packit 664db3
      /* If we have done all the ARGV-elements, stop the scan
Packit 664db3
	 and back over any non-options that we skipped and permuted.  */
Packit 664db3

Packit 664db3
      if (optind == argc)
Packit 664db3
	{
Packit 664db3
	  /* Set the next-arg-index to point at the non-options
Packit 664db3
	     that we previously skipped, so the caller will digest them.  */
Packit 664db3
	  if (first_nonopt != last_nonopt)
Packit 664db3
	    optind = first_nonopt;
Packit 664db3
	  return -1;
Packit 664db3
	}
Packit 664db3

Packit 664db3
      /* If we have come to a non-option and did not permute it,
Packit 664db3
	 either stop the scan or describe it to the caller and pass it by.  */
Packit 664db3

Packit 664db3
      if (NONOPTION_P)
Packit 664db3
	{
Packit 664db3
	  if (ordering == REQUIRE_ORDER)
Packit 664db3
	    return -1;
Packit 664db3
	  optarg = argv[optind++];
Packit 664db3
	  return 1;
Packit 664db3
	}
Packit 664db3

Packit 664db3
      /* We have found another option-ARGV-element.
Packit 664db3
	 Skip the initial punctuation.  */
Packit 664db3

Packit 664db3
      nextchar = (argv[optind] + 1
Packit 664db3
		  + (longopts != NULL && argv[optind][1] == '-'));
Packit 664db3
    }
Packit 664db3

Packit 664db3
  /* Decode the current option-ARGV-element.  */
Packit 664db3

Packit 664db3
  /* Check whether the ARGV-element is a long option.
Packit 664db3

Packit 664db3
     If long_only and the ARGV-element has the form "-f", where f is
Packit 664db3
     a valid short option, don't consider it an abbreviated form of
Packit 664db3
     a long option that starts with f.  Otherwise there would be no
Packit 664db3
     way to give the -f short option.
Packit 664db3

Packit 664db3
     On the other hand, if there's a long option "fubar" and
Packit 664db3
     the ARGV-element is "-fu", do consider that an abbreviation of
Packit 664db3
     the long option, just like "--fu", and not "-f" with arg "u".
Packit 664db3

Packit 664db3
     This distinction seems to be the most useful approach.  */
Packit 664db3

Packit 664db3
  if (longopts != NULL
Packit 664db3
      && (argv[optind][1] == '-'
Packit 664db3
	  || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
Packit 664db3
    {
Packit 664db3
      char *nameend;
Packit 664db3
      const struct option *p;
Packit 664db3
      const struct option *pfound = NULL;
Packit 664db3
      int exact = 0;
Packit 664db3
      int ambig = 0;
Packit 664db3
      int indfound = -1;
Packit 664db3
      int option_index;
Packit 664db3

Packit 664db3
      for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
Packit 664db3
	/* Do nothing.  */ ;
Packit 664db3

Packit 664db3
      /* Test all long options for either exact match
Packit 664db3
	 or abbreviated matches.  */
Packit 664db3
      for (p = longopts, option_index = 0; p->name; p++, option_index++)
Packit 664db3
	if (!strncmp (p->name, nextchar, nameend - nextchar))
Packit 664db3
	  {
Packit 664db3
	    if ((unsigned int) (nameend - nextchar)
Packit 664db3
		== (unsigned int) strlen (p->name))
Packit 664db3
	      {
Packit 664db3
		/* Exact match found.  */
Packit 664db3
		pfound = p;
Packit 664db3
		indfound = option_index;
Packit 664db3
		exact = 1;
Packit 664db3
		break;
Packit 664db3
	      }
Packit 664db3
	    else if (pfound == NULL)
Packit 664db3
	      {
Packit 664db3
		/* First nonexact match found.  */
Packit 664db3
		pfound = p;
Packit 664db3
		indfound = option_index;
Packit 664db3
	      }
Packit 664db3
	    else
Packit 664db3
	      /* Second or later nonexact match found.  */
Packit 664db3
	      ambig = 1;
Packit 664db3
	  }
Packit 664db3

Packit 664db3
      if (ambig && !exact)
Packit 664db3
	{
Packit 664db3
	  if (opterr)
Packit 664db3
	    fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
Packit 664db3
		     argv[0], argv[optind]);
Packit 664db3
	  nextchar += strlen (nextchar);
Packit 664db3
	  optind++;
Packit 664db3
	  optopt = 0;
Packit 664db3
	  return '?';
Packit 664db3
	}
Packit 664db3

Packit 664db3
      if (pfound != NULL)
Packit 664db3
	{
Packit 664db3
	  option_index = indfound;
Packit 664db3
	  optind++;
Packit 664db3
	  if (*nameend)
Packit 664db3
	    {
Packit 664db3
	      /* Don't test has_arg with >, because some C compilers don't
Packit 664db3
		 allow it to be used on enums.  */
Packit 664db3
	      if (pfound->has_arg)
Packit 664db3
		optarg = nameend + 1;
Packit 664db3
	      else
Packit 664db3
		{
Packit 664db3
		  if (opterr)
Packit 664db3
		    {
Packit 664db3
		      if (argv[optind - 1][1] == '-')
Packit 664db3
			/* --option */
Packit 664db3
			fprintf (stderr,
Packit 664db3
				 _("%s: option `--%s' doesn't allow an argument\n"),
Packit 664db3
				 argv[0], pfound->name);
Packit 664db3
		      else
Packit 664db3
			/* +option or -option */
Packit 664db3
			fprintf (stderr,
Packit 664db3
				 _("%s: option `%c%s' doesn't allow an argument\n"),
Packit 664db3
				 argv[0], argv[optind - 1][0], pfound->name);
Packit 664db3
		    }
Packit 664db3

Packit 664db3
		  nextchar += strlen (nextchar);
Packit 664db3

Packit 664db3
		  optopt = pfound->val;
Packit 664db3
		  return '?';
Packit 664db3
		}
Packit 664db3
	    }
Packit 664db3
	  else if (pfound->has_arg == 1)
Packit 664db3
	    {
Packit 664db3
	      if (optind < argc)
Packit 664db3
		optarg = argv[optind++];
Packit 664db3
	      else
Packit 664db3
		{
Packit 664db3
		  if (opterr)
Packit 664db3
		    fprintf (stderr,
Packit 664db3
			   _("%s: option `%s' requires an argument\n"),
Packit 664db3
			   argv[0], argv[optind - 1]);
Packit 664db3
		  nextchar += strlen (nextchar);
Packit 664db3
		  optopt = pfound->val;
Packit 664db3
		  return optstring[0] == ':' ? ':' : '?';
Packit 664db3
		}
Packit 664db3
	    }
Packit 664db3
	  nextchar += strlen (nextchar);
Packit 664db3
	  if (longind != NULL)
Packit 664db3
	    *longind = option_index;
Packit 664db3
	  if (pfound->flag)
Packit 664db3
	    {
Packit 664db3
	      *(pfound->flag) = pfound->val;
Packit 664db3
	      return 0;
Packit 664db3
	    }
Packit 664db3
	  return pfound->val;
Packit 664db3
	}
Packit 664db3

Packit 664db3
      /* Can't find it as a long option.  If this is not getopt_long_only,
Packit 664db3
	 or the option starts with '--' or is not a valid short
Packit 664db3
	 option, then it's an error.
Packit 664db3
	 Otherwise interpret it as a short option.  */
Packit 664db3
      if (!long_only || argv[optind][1] == '-'
Packit 664db3
	  || my_index (optstring, *nextchar) == NULL)
Packit 664db3
	{
Packit 664db3
	  if (opterr)
Packit 664db3
	    {
Packit 664db3
	      if (argv[optind][1] == '-')
Packit 664db3
		/* --option */
Packit 664db3
		fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
Packit 664db3
			 argv[0], nextchar);
Packit 664db3
	      else
Packit 664db3
		/* +option or -option */
Packit 664db3
		fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
Packit 664db3
			 argv[0], argv[optind][0], nextchar);
Packit 664db3
	    }
Packit 664db3
	  nextchar = (char *) "";
Packit 664db3
	  optind++;
Packit 664db3
	  optopt = 0;
Packit 664db3
	  return '?';
Packit 664db3
	}
Packit 664db3
    }
Packit 664db3

Packit 664db3
  /* Look at and handle the next short option-character.  */
Packit 664db3

Packit 664db3
  {
Packit 664db3
    char c = *nextchar++;
Packit 664db3
    char *temp = my_index (optstring, c);
Packit 664db3

Packit 664db3
    /* Increment `optind' when we start to process its last character.  */
Packit 664db3
    if (*nextchar == '\0')
Packit 664db3
      ++optind;
Packit 664db3

Packit 664db3
    if (temp == NULL || c == ':')
Packit 664db3
      {
Packit 664db3
	if (opterr)
Packit 664db3
	  {
Packit 664db3
	    if (posixly_correct)
Packit 664db3
	      /* 1003.2 specifies the format of this message.  */
Packit 664db3
	      fprintf (stderr, _("%s: illegal option -- %c\n"),
Packit 664db3
		       argv[0], c);
Packit 664db3
	    else
Packit 664db3
	      fprintf (stderr, _("%s: invalid option -- %c\n"),
Packit 664db3
		       argv[0], c);
Packit 664db3
	  }
Packit 664db3
	optopt = c;
Packit 664db3
	return '?';
Packit 664db3
      }
Packit 664db3
    /* Convenience. Treat POSIX -W foo same as long option --foo */
Packit 664db3
    if (temp[0] == 'W' && temp[1] == ';')
Packit 664db3
      {
Packit 664db3
	char *nameend;
Packit 664db3
	const struct option *p;
Packit 664db3
	const struct option *pfound = NULL;
Packit 664db3
	int exact = 0;
Packit 664db3
	int ambig = 0;
Packit 664db3
	int indfound = 0;
Packit 664db3
	int option_index;
Packit 664db3

Packit 664db3
	/* This is an option that requires an argument.  */
Packit 664db3
	if (*nextchar != '\0')
Packit 664db3
	  {
Packit 664db3
	    optarg = nextchar;
Packit 664db3
	    /* If we end this ARGV-element by taking the rest as an arg,
Packit 664db3
	       we must advance to the next element now.  */
Packit 664db3
	    optind++;
Packit 664db3
	  }
Packit 664db3
	else if (optind == argc)
Packit 664db3
	  {
Packit 664db3
	    if (opterr)
Packit 664db3
	      {
Packit 664db3
		/* 1003.2 specifies the format of this message.  */
Packit 664db3
		fprintf (stderr, _("%s: option requires an argument -- %c\n"),
Packit 664db3
			 argv[0], c);
Packit 664db3
	      }
Packit 664db3
	    optopt = c;
Packit 664db3
	    if (optstring[0] == ':')
Packit 664db3
	      c = ':';
Packit 664db3
	    else
Packit 664db3
	      c = '?';
Packit 664db3
	    return c;
Packit 664db3
	  }
Packit 664db3
	else
Packit 664db3
	  /* We already incremented `optind' once;
Packit 664db3
	     increment it again when taking next ARGV-elt as argument.  */
Packit 664db3
	  optarg = argv[optind++];
Packit 664db3

Packit 664db3
	/* optarg is now the argument, see if it's in the
Packit 664db3
	   table of longopts.  */
Packit 664db3

Packit 664db3
	for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
Packit 664db3
	  /* Do nothing.  */ ;
Packit 664db3

Packit 664db3
	/* Test all long options for either exact match
Packit 664db3
	   or abbreviated matches.  */
Packit 664db3
	for (p = longopts, option_index = 0; p->name; p++, option_index++)
Packit 664db3
	  if (!strncmp (p->name, nextchar, nameend - nextchar))
Packit 664db3
	    {
Packit 664db3
	      if ((unsigned int) (nameend - nextchar) == strlen (p->name))
Packit 664db3
		{
Packit 664db3
		  /* Exact match found.  */
Packit 664db3
		  pfound = p;
Packit 664db3
		  indfound = option_index;
Packit 664db3
		  exact = 1;
Packit 664db3
		  break;
Packit 664db3
		}
Packit 664db3
	      else if (pfound == NULL)
Packit 664db3
		{
Packit 664db3
		  /* First nonexact match found.  */
Packit 664db3
		  pfound = p;
Packit 664db3
		  indfound = option_index;
Packit 664db3
		}
Packit 664db3
	      else
Packit 664db3
		/* Second or later nonexact match found.  */
Packit 664db3
		ambig = 1;
Packit 664db3
	    }
Packit 664db3
	if (ambig && !exact)
Packit 664db3
	  {
Packit 664db3
	    if (opterr)
Packit 664db3
	      fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
Packit 664db3
		       argv[0], argv[optind]);
Packit 664db3
	    nextchar += strlen (nextchar);
Packit 664db3
	    optind++;
Packit 664db3
	    return '?';
Packit 664db3
	  }
Packit 664db3
	if (pfound != NULL)
Packit 664db3
	  {
Packit 664db3
	    option_index = indfound;
Packit 664db3
	    if (*nameend)
Packit 664db3
	      {
Packit 664db3
		/* Don't test has_arg with >, because some C compilers don't
Packit 664db3
		   allow it to be used on enums.  */
Packit 664db3
		if (pfound->has_arg)
Packit 664db3
		  optarg = nameend + 1;
Packit 664db3
		else
Packit 664db3
		  {
Packit 664db3
		    if (opterr)
Packit 664db3
		      fprintf (stderr, _("\
Packit 664db3
%s: option `-W %s' doesn't allow an argument\n"),
Packit 664db3
			       argv[0], pfound->name);
Packit 664db3

Packit 664db3
		    nextchar += strlen (nextchar);
Packit 664db3
		    return '?';
Packit 664db3
		  }
Packit 664db3
	      }
Packit 664db3
	    else if (pfound->has_arg == 1)
Packit 664db3
	      {
Packit 664db3
		if (optind < argc)
Packit 664db3
		  optarg = argv[optind++];
Packit 664db3
		else
Packit 664db3
		  {
Packit 664db3
		    if (opterr)
Packit 664db3
		      fprintf (stderr,
Packit 664db3
			       _("%s: option `%s' requires an argument\n"),
Packit 664db3
			       argv[0], argv[optind - 1]);
Packit 664db3
		    nextchar += strlen (nextchar);
Packit 664db3
		    return optstring[0] == ':' ? ':' : '?';
Packit 664db3
		  }
Packit 664db3
	      }
Packit 664db3
	    nextchar += strlen (nextchar);
Packit 664db3
	    if (longind != NULL)
Packit 664db3
	      *longind = option_index;
Packit 664db3
	    if (pfound->flag)
Packit 664db3
	      {
Packit 664db3
		*(pfound->flag) = pfound->val;
Packit 664db3
		return 0;
Packit 664db3
	      }
Packit 664db3
	    return pfound->val;
Packit 664db3
	  }
Packit 664db3
	  nextchar = NULL;
Packit 664db3
	  return 'W';	/* Let the application handle it.   */
Packit 664db3
      }
Packit 664db3
    if (temp[1] == ':')
Packit 664db3
      {
Packit 664db3
	if (temp[2] == ':')
Packit 664db3
	  {
Packit 664db3
	    /* This is an option that accepts an argument optionally.  */
Packit 664db3
	    if (*nextchar != '\0')
Packit 664db3
	      {
Packit 664db3
		optarg = nextchar;
Packit 664db3
		optind++;
Packit 664db3
	      }
Packit 664db3
	    else
Packit 664db3
	      optarg = NULL;
Packit 664db3
	    nextchar = NULL;
Packit 664db3
	  }
Packit 664db3
	else
Packit 664db3
	  {
Packit 664db3
	    /* This is an option that requires an argument.  */
Packit 664db3
	    if (*nextchar != '\0')
Packit 664db3
	      {
Packit 664db3
		optarg = nextchar;
Packit 664db3
		/* If we end this ARGV-element by taking the rest as an arg,
Packit 664db3
		   we must advance to the next element now.  */
Packit 664db3
		optind++;
Packit 664db3
	      }
Packit 664db3
	    else if (optind == argc)
Packit 664db3
	      {
Packit 664db3
		if (opterr)
Packit 664db3
		  {
Packit 664db3
		    /* 1003.2 specifies the format of this message.  */
Packit 664db3
		    fprintf (stderr,
Packit 664db3
			   _("%s: option requires an argument -- %c\n"),
Packit 664db3
			   argv[0], c);
Packit 664db3
		  }
Packit 664db3
		optopt = c;
Packit 664db3
		if (optstring[0] == ':')
Packit 664db3
		  c = ':';
Packit 664db3
		else
Packit 664db3
		  c = '?';
Packit 664db3
	      }
Packit 664db3
	    else
Packit 664db3
	      /* We already incremented `optind' once;
Packit 664db3
		 increment it again when taking next ARGV-elt as argument.  */
Packit 664db3
	      optarg = argv[optind++];
Packit 664db3
	    nextchar = NULL;
Packit 664db3
	  }
Packit 664db3
      }
Packit 664db3
    return c;
Packit 664db3
  }
Packit 664db3
}
Packit 664db3

Packit 664db3
int
Packit 664db3
getopt (argc, argv, optstring)
Packit 664db3
     int argc;
Packit 664db3
     char *const *argv;
Packit 664db3
     const char *optstring;
Packit 664db3
{
Packit 664db3
  return _getopt_internal (argc, argv, optstring,
Packit 664db3
			   (const struct option *) 0,
Packit 664db3
			   (int *) 0,
Packit 664db3
			   0);
Packit 664db3
}
Packit 664db3

Packit 664db3
#endif	/* Not ELIDE_CODE.  */
Packit 664db3

Packit 664db3
#ifdef TEST
Packit 664db3

Packit 664db3
/* Compile with -DTEST to make an executable for use in testing
Packit 664db3
   the above definition of `getopt'.  */
Packit 664db3

Packit 664db3
int
Packit 664db3
main (argc, argv)
Packit 664db3
     int argc;
Packit 664db3
     char **argv;
Packit 664db3
{
Packit 664db3
  int c;
Packit 664db3
  int digit_optind = 0;
Packit 664db3

Packit 664db3
  while (1)
Packit 664db3
    {
Packit 664db3
      int this_option_optind = optind ? optind : 1;
Packit 664db3

Packit 664db3
      c = getopt (argc, argv, "abc:d:0123456789");
Packit 664db3
      if (c == -1)
Packit 664db3
	break;
Packit 664db3

Packit 664db3
      switch (c)
Packit 664db3
	{
Packit 664db3
	case '0':
Packit 664db3
	case '1':
Packit 664db3
	case '2':
Packit 664db3
	case '3':
Packit 664db3
	case '4':
Packit 664db3
	case '5':
Packit 664db3
	case '6':
Packit 664db3
	case '7':
Packit 664db3
	case '8':
Packit 664db3
	case '9':
Packit 664db3
	  if (digit_optind != 0 && digit_optind != this_option_optind)
Packit 664db3
	    printf ("digits occur in two different argv-elements.\n");
Packit 664db3
	  digit_optind = this_option_optind;
Packit 664db3
	  printf ("option %c\n", c);
Packit 664db3
	  break;
Packit 664db3

Packit 664db3
	case 'a':
Packit 664db3
	  printf ("option a\n");
Packit 664db3
	  break;
Packit 664db3

Packit 664db3
	case 'b':
Packit 664db3
	  printf ("option b\n");
Packit 664db3
	  break;
Packit 664db3

Packit 664db3
	case 'c':
Packit 664db3
	  printf ("option c with value `%s'\n", optarg);
Packit 664db3
	  break;
Packit 664db3

Packit 664db3
	case '?':
Packit 664db3
	  break;
Packit 664db3

Packit 664db3
	default:
Packit 664db3
	  printf ("?? getopt returned character code 0%o ??\n", c);
Packit 664db3
	}
Packit 664db3
    }
Packit 664db3

Packit 664db3
  if (optind < argc)
Packit 664db3
    {
Packit 664db3
      printf ("non-option ARGV-elements: ");
Packit 664db3
      while (optind < argc)
Packit 664db3
	printf ("%s ", argv[optind++]);
Packit 664db3
      printf ("\n");
Packit 664db3
    }
Packit 664db3

Packit 664db3
  exit (0);
Packit 664db3
}
Packit 664db3

Packit 664db3
#endif /* TEST */