Blame getopt.c

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