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