/* Getopt for GNU.1NOTE: getopt is now part of the C library, so if you don't know what2"Keep this file name-space clean" means, talk to [email protected]3before changing it!4Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,20015Free Software Foundation, Inc.6This file is part of the GNU C Library.78The GNU C Library is free software; you can redistribute it and/or9modify it under the terms of the GNU Lesser General Public10License as published by the Free Software Foundation; either11version 2.1 of the License, or (at your option) any later version.1213The GNU C Library is distributed in the hope that it will be useful,14but WITHOUT ANY WARRANTY; without even the implied warranty of15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU16Lesser General Public License for more details.1718You should have received a copy of the GNU Lesser General Public19License along with the GNU C Library; if not, write to the Free20Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA2102111-1307 USA. */2223/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.24Ditto for AIX 3.2 and <stdlib.h>. */25#ifndef _NO_PROTO26# define _NO_PROTO27#endif2829#ifdef HAVE_CONFIG_H30# include <config.h>31#endif3233#if !defined __STDC__ || !__STDC__34/* This is a separate conditional since some stdc systems35reject `defined (const)'. */36# ifndef const37# define const38# endif39#endif4041#include <stdio.h>4243/* Comment out all this code if we are using the GNU C Library, and are not44actually compiling the library itself. This code is part of the GNU C45Library, but also included in many other GNU distributions. Compiling46and linking in this code is a waste when using the GNU C library47(especially if it is a shared library). Rather than having every GNU48program understand `configure --with-gnu-libc' and omit the object files,49it is simpler to just do this in the source for each such file. */5051#define GETOPT_INTERFACE_VERSION 252#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 253# include <gnu-versions.h>54# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION55# define ELIDE_CODE56# endif57#endif5859#ifndef ELIDE_CODE606162/* This needs to come after some library #include63to get __GNU_LIBRARY__ defined. */64#ifdef __GNU_LIBRARY__65/* Don't include stdlib.h for non-GNU C libraries because some of them66contain conflicting prototypes for getopt. */67# include <stdlib.h>68# include <unistd.h>69#endif /* GNU C library. */7071#ifdef VMS72# include <unixlib.h>73# if HAVE_STRING_H - 074# include <string.h>75# endif76#endif7778#ifndef _79/* This is for other GNU distributions with internationalized messages. */80# if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC81# include <libintl.h>82# ifndef _83# define _(msgid) gettext (msgid)84# endif85# else86# define _(msgid) (msgid)87# endif88#endif8990/* This version of `getopt' appears to the caller like standard Unix `getopt'91but it behaves differently for the user, since it allows the user92to intersperse the options with the other arguments.9394As `getopt' works, it permutes the elements of ARGV so that,95when it is done, all the options precede everything else. Thus96all application programs are extended to handle flexible argument order.9798Setting the environment variable POSIXLY_CORRECT disables permutation.99Then the behavior is completely standard.100101GNU application programs can use a third alternative mode in which102they can distinguish the relative order of options and other arguments. */103104#include "getopt.h"105106/* For communication from `getopt' to the caller.107When `getopt' finds an option that takes an argument,108the argument value is returned here.109Also, when `ordering' is RETURN_IN_ORDER,110each non-option ARGV-element is returned here. */111112char *optarg;113114/* Index in ARGV of the next element to be scanned.115This is used for communication to and from the caller116and for communication between successive calls to `getopt'.117118On entry to `getopt', zero means this is the first call; initialize.119120When `getopt' returns -1, this is the index of the first of the121non-option elements that the caller should itself scan.122123Otherwise, `optind' communicates from one call to the next124how much of ARGV has been scanned so far. */125126/* 1003.2 says this must be 1 before any call. */127int optind = 1;128129/* Formerly, initialization of getopt depended on optind==0, which130causes problems with re-calling getopt as programs generally don't131know that. */132133int __getopt_initialized;134135/* The next char to be scanned in the option-element136in which the last option character we returned was found.137This allows us to pick up the scan where we left off.138139If this is zero, or a null string, it means resume the scan140by advancing to the next ARGV-element. */141142static char *nextchar;143144/* Callers store zero here to inhibit the error message145for unrecognized options. */146147int opterr = 1;148149/* Set to an option character which was unrecognized.150This must be initialized on some systems to avoid linking in the151system's own getopt implementation. */152153int optopt = '?';154155/* Describe how to deal with options that follow non-option ARGV-elements.156157If the caller did not specify anything,158the default is REQUIRE_ORDER if the environment variable159POSIXLY_CORRECT is defined, PERMUTE otherwise.160161REQUIRE_ORDER means don't recognize them as options;162stop option processing when the first non-option is seen.163This is what Unix does.164This mode of operation is selected by either setting the environment165variable POSIXLY_CORRECT, or using `+' as the first character166of the list of option characters.167168PERMUTE is the default. We permute the contents of ARGV as we scan,169so that eventually all the non-options are at the end. This allows options170to be given in any order, even with programs that were not written to171expect this.172173RETURN_IN_ORDER is an option available to programs that were written174to expect options and other ARGV-elements in any order and that care about175the ordering of the two. We describe each non-option ARGV-element176as if it were the argument of an option with character code 1.177Using `-' as the first character of the list of option characters178selects this mode of operation.179180The special argument `--' forces an end of option-scanning regardless181of the value of `ordering'. In the case of RETURN_IN_ORDER, only182`--' can cause `getopt' to return -1 with `optind' != ARGC. */183184static enum185{186REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER187} ordering;188189/* Value of POSIXLY_CORRECT environment variable. */190static char *posixly_correct;191192#ifdef __GNU_LIBRARY__193/* We want to avoid inclusion of string.h with non-GNU libraries194because there are many ways it can cause trouble.195On some systems, it contains special magic macros that don't work196in GCC. */197# include <string.h>198# define my_index strchr199#else200201# if HAVE_STRING_H202# include <string.h>203# else204# include <strings.h>205# endif206207/* Avoid depending on library functions or files208whose names are inconsistent. */209210#ifndef getenv211extern char *getenv ();212#endif213214static char *215my_index (str, chr)216const char *str;217int chr;218{219while (*str)220{221if (*str == chr)222return (char *) str;223str++;224}225return 0;226}227228/* If using GCC, we can safely declare strlen this way.229If not using GCC, it is ok not to declare it. */230#ifdef __GNUC__231/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.232That was relevant to code that was here before. */233# if (!defined __STDC__ || !__STDC__) && !defined strlen234/* gcc with -traditional declares the built-in strlen to return int,235and has done so at least since version 2.4.5. -- rms. */236extern int strlen (const char *);237# endif /* not __STDC__ */238#endif /* __GNUC__ */239240#endif /* not __GNU_LIBRARY__ */241242/* Handle permutation of arguments. */243244/* Describe the part of ARGV that contains non-options that have245been skipped. `first_nonopt' is the index in ARGV of the first of them;246`last_nonopt' is the index after the last of them. */247248static int first_nonopt;249static int last_nonopt;250251#ifdef _LIBC252/* Stored original parameters.253XXX This is no good solution. We should rather copy the args so254that we can compare them later. But we must not use malloc(3). */255extern int __libc_argc;256extern char **__libc_argv;257258/* Bash 2.0 gives us an environment variable containing flags259indicating ARGV elements that should not be considered arguments. */260261# ifdef USE_NONOPTION_FLAGS262/* Defined in getopt_init.c */263extern char *__getopt_nonoption_flags;264265static int nonoption_flags_max_len;266static int nonoption_flags_len;267# endif268269# ifdef USE_NONOPTION_FLAGS270# define SWAP_FLAGS(ch1, ch2) \271if (nonoption_flags_len > 0) \272{ \273char __tmp = __getopt_nonoption_flags[ch1]; \274__getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \275__getopt_nonoption_flags[ch2] = __tmp; \276}277# else278# define SWAP_FLAGS(ch1, ch2)279# endif280#else /* !_LIBC */281# define SWAP_FLAGS(ch1, ch2)282#endif /* _LIBC */283284/* Exchange two adjacent subsequences of ARGV.285One subsequence is elements [first_nonopt,last_nonopt)286which contains all the non-options that have been skipped so far.287The other is elements [last_nonopt,optind), which contains all288the options processed since those non-options were skipped.289290`first_nonopt' and `last_nonopt' are relocated so that they describe291the new indices of the non-options in ARGV after they are moved. */292293#if defined __STDC__ && __STDC__294static void exchange (char **);295#endif296297static void298exchange (argv)299char **argv;300{301int bottom = first_nonopt;302int middle = last_nonopt;303int top = optind;304char *tem;305306/* Exchange the shorter segment with the far end of the longer segment.307That puts the shorter segment into the right place.308It leaves the longer segment in the right place overall,309but it consists of two parts that need to be swapped next. */310311#if defined _LIBC && defined USE_NONOPTION_FLAGS312/* First make sure the handling of the `__getopt_nonoption_flags'313string can work normally. Our top argument must be in the range314of the string. */315if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)316{317/* We must extend the array. The user plays games with us and318presents new arguments. */319char *new_str = malloc (top + 1);320if (new_str == NULL)321nonoption_flags_len = nonoption_flags_max_len = 0;322else323{324memset (__mempcpy (new_str, __getopt_nonoption_flags,325nonoption_flags_max_len),326'\0', top + 1 - nonoption_flags_max_len);327nonoption_flags_max_len = top + 1;328__getopt_nonoption_flags = new_str;329}330}331#endif332333while (top > middle && middle > bottom)334{335if (top - middle > middle - bottom)336{337/* Bottom segment is the short one. */338int len = middle - bottom;339register int i;340341/* Swap it with the top part of the top segment. */342for (i = 0; i < len; i++)343{344tem = argv[bottom + i];345argv[bottom + i] = argv[top - (middle - bottom) + i];346argv[top - (middle - bottom) + i] = tem;347SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);348}349/* Exclude the moved bottom segment from further swapping. */350top -= len;351}352else353{354/* Top segment is the short one. */355int len = top - middle;356register int i;357358/* Swap it with the bottom part of the bottom segment. */359for (i = 0; i < len; i++)360{361tem = argv[bottom + i];362argv[bottom + i] = argv[middle + i];363argv[middle + i] = tem;364SWAP_FLAGS (bottom + i, middle + i);365}366/* Exclude the moved top segment from further swapping. */367bottom += len;368}369}370371/* Update records for the slots the non-options now occupy. */372373first_nonopt += (optind - last_nonopt);374last_nonopt = optind;375}376377/* Initialize the internal data when the first call is made. */378379#if defined __STDC__ && __STDC__380static const char *_getopt_initialize (int, char *const *, const char *);381#endif382static const char *383_getopt_initialize (argc, argv, optstring)384int argc;385char *const *argv;386const char *optstring;387{388/* Start processing options with ARGV-element 1 (since ARGV-element 0389is the program name); the sequence of previously skipped390non-option ARGV-elements is empty. */391392first_nonopt = last_nonopt = optind;393394nextchar = NULL;395396posixly_correct = getenv ("POSIXLY_CORRECT");397398/* Determine how to handle the ordering of options and nonoptions. */399400if (optstring[0] == '-')401{402ordering = RETURN_IN_ORDER;403++optstring;404}405else if (optstring[0] == '+')406{407ordering = REQUIRE_ORDER;408++optstring;409}410else if (posixly_correct != NULL)411ordering = REQUIRE_ORDER;412else413ordering = PERMUTE;414415#if defined _LIBC && defined USE_NONOPTION_FLAGS416if (posixly_correct == NULL417&& argc == __libc_argc && argv == __libc_argv)418{419if (nonoption_flags_max_len == 0)420{421if (__getopt_nonoption_flags == NULL422|| __getopt_nonoption_flags[0] == '\0')423nonoption_flags_max_len = -1;424else425{426const char *orig_str = __getopt_nonoption_flags;427int len = nonoption_flags_max_len = strlen (orig_str);428if (nonoption_flags_max_len < argc)429nonoption_flags_max_len = argc;430__getopt_nonoption_flags =431(char *) malloc (nonoption_flags_max_len);432if (__getopt_nonoption_flags == NULL)433nonoption_flags_max_len = -1;434else435memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),436'\0', nonoption_flags_max_len - len);437}438}439nonoption_flags_len = nonoption_flags_max_len;440}441else442nonoption_flags_len = 0;443#endif444445return optstring;446}447448/* Scan elements of ARGV (whose length is ARGC) for option characters449given in OPTSTRING.450451If an element of ARGV starts with '-', and is not exactly "-" or "--",452then it is an option element. The characters of this element453(aside from the initial '-') are option characters. If `getopt'454is called repeatedly, it returns successively each of the option characters455from each of the option elements.456457If `getopt' finds another option character, it returns that character,458updating `optind' and `nextchar' so that the next call to `getopt' can459resume the scan with the following option character or ARGV-element.460461If there are no more option characters, `getopt' returns -1.462Then `optind' is the index in ARGV of the first ARGV-element463that is not an option. (The ARGV-elements have been permuted464so that those that are not options now come last.)465466OPTSTRING is a string containing the legitimate option characters.467If an option character is seen that is not listed in OPTSTRING,468return '?' after printing an error message. If you set `opterr' to469zero, the error message is suppressed but we still return '?'.470471If a char in OPTSTRING is followed by a colon, that means it wants an arg,472so the following text in the same ARGV-element, or the text of the following473ARGV-element, is returned in `optarg'. Two colons mean an option that474wants an optional arg; if there is text in the current ARGV-element,475it is returned in `optarg', otherwise `optarg' is set to zero.476477If OPTSTRING starts with `-' or `+', it requests different methods of478handling the non-option ARGV-elements.479See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.480481Long-named options begin with `--' instead of `-'.482Their names may be abbreviated as long as the abbreviation is unique483or is an exact match for some defined option. If they have an484argument, it follows the option name in the same ARGV-element, separated485from the option name by a `=', or else the in next ARGV-element.486When `getopt' finds a long-named option, it returns 0 if that option's487`flag' field is nonzero, the value of the option's `val' field488if the `flag' field is zero.489490The elements of ARGV aren't really const, because we permute them.491But we pretend they're const in the prototype to be compatible492with other systems.493494LONGOPTS is a vector of `struct option' terminated by an495element containing a name which is zero.496497LONGIND returns the index in LONGOPT of the long-named option found.498It is only valid when a long-named option has been found by the most499recent call.500501If LONG_ONLY is nonzero, '-' as well as '--' can introduce502long-named options. */503504int505_getopt_internal (argc, argv, optstring, longopts, longind, long_only)506int argc;507char *const *argv;508const char *optstring;509const struct option *longopts;510int *longind;511int long_only;512{513int print_errors = opterr;514if (optstring[0] == ':')515print_errors = 0;516517if (argc < 1)518return -1;519520optarg = NULL;521522if (optind == 0 || !__getopt_initialized)523{524if (optind == 0)525optind = 1; /* Don't scan ARGV[0], the program name. */526optstring = _getopt_initialize (argc, argv, optstring);527__getopt_initialized = 1;528}529530/* Test whether ARGV[optind] points to a non-option argument.531Either it does not have option syntax, or there is an environment flag532from the shell indicating it is not an option. The later information533is only used when the used in the GNU libc. */534#if defined _LIBC && defined USE_NONOPTION_FLAGS535# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \536|| (optind < nonoption_flags_len \537&& __getopt_nonoption_flags[optind] == '1'))538#else539# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')540#endif541542if (nextchar == NULL || *nextchar == '\0')543{544/* Advance to the next ARGV-element. */545546/* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been547moved back by the user (who may also have changed the arguments). */548if (last_nonopt > optind)549last_nonopt = optind;550if (first_nonopt > optind)551first_nonopt = optind;552553if (ordering == PERMUTE)554{555/* If we have just processed some options following some non-options,556exchange them so that the options come first. */557558if (first_nonopt != last_nonopt && last_nonopt != optind)559exchange ((char **) argv);560else if (last_nonopt != optind)561first_nonopt = optind;562563/* Skip any additional non-options564and extend the range of non-options previously skipped. */565566while (optind < argc && NONOPTION_P)567optind++;568last_nonopt = optind;569}570571/* The special ARGV-element `--' means premature end of options.572Skip it like a null option,573then exchange with previous non-options as if it were an option,574then skip everything else like a non-option. */575576if (optind != argc && !strcmp (argv[optind], "--"))577{578optind++;579580if (first_nonopt != last_nonopt && last_nonopt != optind)581exchange ((char **) argv);582else if (first_nonopt == last_nonopt)583first_nonopt = optind;584last_nonopt = argc;585586optind = argc;587}588589/* If we have done all the ARGV-elements, stop the scan590and back over any non-options that we skipped and permuted. */591592if (optind == argc)593{594/* Set the next-arg-index to point at the non-options595that we previously skipped, so the caller will digest them. */596if (first_nonopt != last_nonopt)597optind = first_nonopt;598return -1;599}600601/* If we have come to a non-option and did not permute it,602either stop the scan or describe it to the caller and pass it by. */603604if (NONOPTION_P)605{606if (ordering == REQUIRE_ORDER)607return -1;608optarg = argv[optind++];609return 1;610}611612/* We have found another option-ARGV-element.613Skip the initial punctuation. */614615nextchar = (argv[optind] + 1616+ (longopts != NULL && argv[optind][1] == '-'));617}618619/* Decode the current option-ARGV-element. */620621/* Check whether the ARGV-element is a long option.622623If long_only and the ARGV-element has the form "-f", where f is624a valid short option, don't consider it an abbreviated form of625a long option that starts with f. Otherwise there would be no626way to give the -f short option.627628On the other hand, if there's a long option "fubar" and629the ARGV-element is "-fu", do consider that an abbreviation of630the long option, just like "--fu", and not "-f" with arg "u".631632This distinction seems to be the most useful approach. */633634if (longopts != NULL635&& (argv[optind][1] == '-'636|| (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))637{638char *nameend;639const struct option *p;640const struct option *pfound = NULL;641int exact = 0;642int ambig = 0;643int indfound = -1;644int option_index;645646for (nameend = nextchar; *nameend && *nameend != '='; nameend++)647/* Do nothing. */ ;648649/* Test all long options for either exact match650or abbreviated matches. */651for (p = longopts, option_index = 0; p->name; p++, option_index++)652if (!strncmp (p->name, nextchar, nameend - nextchar))653{654if ((unsigned int) (nameend - nextchar)655== (unsigned int) strlen (p->name))656{657/* Exact match found. */658pfound = p;659indfound = option_index;660exact = 1;661break;662}663else if (pfound == NULL)664{665/* First nonexact match found. */666pfound = p;667indfound = option_index;668}669else if (long_only670|| pfound->has_arg != p->has_arg671|| pfound->flag != p->flag672|| pfound->val != p->val)673/* Second or later nonexact match found. */674ambig = 1;675}676677if (ambig && !exact)678{679if (print_errors)680fprintf (stderr, _("%s: option `%s' is ambiguous\n"),681argv[0], argv[optind]);682nextchar += strlen (nextchar);683optind++;684optopt = 0;685return '?';686}687688if (pfound != NULL)689{690option_index = indfound;691optind++;692if (*nameend)693{694/* Don't test has_arg with >, because some C compilers don't695allow it to be used on enums. */696if (pfound->has_arg)697optarg = nameend + 1;698else699{700if (print_errors)701{702if (argv[optind - 1][1] == '-')703/* --option */704fprintf (stderr,705_("%s: option `--%s' doesn't allow an argument\n"),706argv[0], pfound->name);707else708/* +option or -option */709fprintf (stderr,710_("%s: option `%c%s' doesn't allow an argument\n"),711argv[0], argv[optind - 1][0], pfound->name);712}713714nextchar += strlen (nextchar);715716optopt = pfound->val;717return '?';718}719}720else if (pfound->has_arg == 1)721{722if (optind < argc)723optarg = argv[optind++];724else725{726if (print_errors)727fprintf (stderr,728_("%s: option `%s' requires an argument\n"),729argv[0], argv[optind - 1]);730nextchar += strlen (nextchar);731optopt = pfound->val;732return optstring[0] == ':' ? ':' : '?';733}734}735nextchar += strlen (nextchar);736if (longind != NULL)737*longind = option_index;738if (pfound->flag)739{740*(pfound->flag) = pfound->val;741return 0;742}743return pfound->val;744}745746/* Can't find it as a long option. If this is not getopt_long_only,747or the option starts with '--' or is not a valid short748option, then it's an error.749Otherwise interpret it as a short option. */750if (!long_only || argv[optind][1] == '-'751|| my_index (optstring, *nextchar) == NULL)752{753if (print_errors)754{755if (argv[optind][1] == '-')756/* --option */757fprintf (stderr, _("%s: unrecognized option `--%s'\n"),758argv[0], nextchar);759else760/* +option or -option */761fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),762argv[0], argv[optind][0], nextchar);763}764nextchar = (char *) "";765optind++;766optopt = 0;767return '?';768}769}770771/* Look at and handle the next short option-character. */772773{774char c = *nextchar++;775char *temp = my_index (optstring, c);776777/* Increment `optind' when we start to process its last character. */778if (*nextchar == '\0')779++optind;780781if (temp == NULL || c == ':')782{783if (print_errors)784{785if (posixly_correct)786/* 1003.2 specifies the format of this message. */787fprintf (stderr, _("%s: illegal option -- %c\n"),788argv[0], c);789else790fprintf (stderr, _("%s: invalid option -- %c\n"),791argv[0], c);792}793optopt = c;794return '?';795}796/* Convenience. Treat POSIX -W foo same as long option --foo */797if (temp[0] == 'W' && temp[1] == ';')798{799char *nameend;800const struct option *p;801const struct option *pfound = NULL;802int exact = 0;803int ambig = 0;804int indfound = 0;805int option_index;806807/* This is an option that requires an argument. */808if (*nextchar != '\0')809{810optarg = nextchar;811/* If we end this ARGV-element by taking the rest as an arg,812we must advance to the next element now. */813optind++;814}815else if (optind == argc)816{817if (print_errors)818{819/* 1003.2 specifies the format of this message. */820fprintf (stderr, _("%s: option requires an argument -- %c\n"),821argv[0], c);822}823optopt = c;824if (optstring[0] == ':')825c = ':';826else827c = '?';828return c;829}830else831/* We already incremented `optind' once;832increment it again when taking next ARGV-elt as argument. */833optarg = argv[optind++];834835/* optarg is now the argument, see if it's in the836table of longopts. */837838for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)839/* Do nothing. */ ;840841/* Test all long options for either exact match842or abbreviated matches. */843for (p = longopts, option_index = 0; p->name; p++, option_index++)844if (!strncmp (p->name, nextchar, nameend - nextchar))845{846if ((unsigned int) (nameend - nextchar) == strlen (p->name))847{848/* Exact match found. */849pfound = p;850indfound = option_index;851exact = 1;852break;853}854else if (pfound == NULL)855{856/* First nonexact match found. */857pfound = p;858indfound = option_index;859}860else861/* Second or later nonexact match found. */862ambig = 1;863}864if (ambig && !exact)865{866if (print_errors)867fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),868argv[0], argv[optind]);869nextchar += strlen (nextchar);870optind++;871return '?';872}873if (pfound != NULL)874{875option_index = indfound;876if (*nameend)877{878/* Don't test has_arg with >, because some C compilers don't879allow it to be used on enums. */880if (pfound->has_arg)881optarg = nameend + 1;882else883{884if (print_errors)885fprintf (stderr, _("\886%s: option `-W %s' doesn't allow an argument\n"),887argv[0], pfound->name);888889nextchar += strlen (nextchar);890return '?';891}892}893else if (pfound->has_arg == 1)894{895if (optind < argc)896optarg = argv[optind++];897else898{899if (print_errors)900fprintf (stderr,901_("%s: option `%s' requires an argument\n"),902argv[0], argv[optind - 1]);903nextchar += strlen (nextchar);904return optstring[0] == ':' ? ':' : '?';905}906}907nextchar += strlen (nextchar);908if (longind != NULL)909*longind = option_index;910if (pfound->flag)911{912*(pfound->flag) = pfound->val;913return 0;914}915return pfound->val;916}917nextchar = NULL;918return 'W'; /* Let the application handle it. */919}920if (temp[1] == ':')921{922if (temp[2] == ':')923{924/* This is an option that accepts an argument optionally. */925if (*nextchar != '\0')926{927optarg = nextchar;928optind++;929}930else931optarg = NULL;932nextchar = NULL;933}934else935{936/* This is an option that requires an argument. */937if (*nextchar != '\0')938{939optarg = nextchar;940/* If we end this ARGV-element by taking the rest as an arg,941we must advance to the next element now. */942optind++;943}944else if (optind == argc)945{946if (print_errors)947{948/* 1003.2 specifies the format of this message. */949fprintf (stderr,950_("%s: option requires an argument -- %c\n"),951argv[0], c);952}953optopt = c;954if (optstring[0] == ':')955c = ':';956else957c = '?';958}959else960/* We already incremented `optind' once;961increment it again when taking next ARGV-elt as argument. */962optarg = argv[optind++];963nextchar = NULL;964}965}966return c;967}968}969970int971getopt (argc, argv, optstring)972int argc;973char *const *argv;974const char *optstring;975{976return _getopt_internal (argc, argv, optstring,977(const struct option *) 0,978(int *) 0,9790);980}981982int getopt_long (argc, argv, optstring, long_options, opt_index)983int argc;984char *const *argv;985const char *optstring;986const struct option *long_options;987int *opt_index;988{989return _getopt_internal (argc, argv, optstring, long_options, opt_index, 0);990}991992#endif /* Not ELIDE_CODE. */993994#ifdef TEST995996/* Compile with -DTEST to make an executable for use in testing997the above definition of `getopt'. */998999int1000main (argc, argv)1001int argc;1002char **argv;1003{1004int c;1005int digit_optind = 0;10061007while (1)1008{1009int this_option_optind = optind ? optind : 1;10101011c = getopt (argc, argv, "abc:d:0123456789");1012if (c == -1)1013break;10141015switch (c)1016{1017case '0':1018case '1':1019case '2':1020case '3':1021case '4':1022case '5':1023case '6':1024case '7':1025case '8':1026case '9':1027if (digit_optind != 0 && digit_optind != this_option_optind)1028printf ("digits occur in two different argv-elements.\n");1029digit_optind = this_option_optind;1030printf ("option %c\n", c);1031break;10321033case 'a':1034printf ("option a\n");1035break;10361037case 'b':1038printf ("option b\n");1039break;10401041case 'c':1042printf ("option c with value `%s'\n", optarg);1043break;10441045case '?':1046break;10471048default:1049printf ("?? getopt returned character code 0%o ??\n", c);1050}1051}10521053if (optind < argc)1054{1055printf ("non-option ARGV-elements: ");1056while (optind < argc)1057printf ("%s ", argv[optind++]);1058printf ("\n");1059}10601061exit (0);1062}10631064#endif /* TEST */106510661067