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