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