198b9484cSchristos /* Getopt for GNU. 298b9484cSchristos NOTE: getopt is now part of the C library, so if you don't know what 398b9484cSchristos "Keep this file name-space clean" means, talk to drepper@gnu.org 498b9484cSchristos before changing it! 598b9484cSchristos 6*5173eb0aSchristos Copyright (C) 1987-2024 Free Software Foundation, Inc. 798b9484cSchristos 898b9484cSchristos NOTE: This source is derived from an old version taken from the GNU C 998b9484cSchristos Library (glibc). 1098b9484cSchristos 1198b9484cSchristos This program is free software; you can redistribute it and/or modify it 1298b9484cSchristos under the terms of the GNU General Public License as published by the 1398b9484cSchristos Free Software Foundation; either version 2, or (at your option) any 1498b9484cSchristos later version. 1598b9484cSchristos 1698b9484cSchristos This program is distributed in the hope that it will be useful, 1798b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 1898b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1998b9484cSchristos GNU General Public License for more details. 2098b9484cSchristos 2198b9484cSchristos You should have received a copy of the GNU General Public License 2298b9484cSchristos along with this program; if not, write to the Free Software 2398b9484cSchristos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 2498b9484cSchristos USA. */ 2598b9484cSchristos 2698b9484cSchristos /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. 2798b9484cSchristos Ditto for AIX 3.2 and <stdlib.h>. */ 2898b9484cSchristos #ifndef _NO_PROTO 2998b9484cSchristos # define _NO_PROTO 3098b9484cSchristos #endif 3198b9484cSchristos 3298b9484cSchristos #ifdef HAVE_CONFIG_H 3398b9484cSchristos # include <config.h> 3498b9484cSchristos #endif 3598b9484cSchristos 3698b9484cSchristos #if !defined __STDC__ || !__STDC__ 3798b9484cSchristos /* This is a separate conditional since some stdc systems 3898b9484cSchristos reject `defined (const)'. */ 3998b9484cSchristos # ifndef const 4098b9484cSchristos # define const 4198b9484cSchristos # endif 4298b9484cSchristos #endif 4398b9484cSchristos 4498b9484cSchristos #include "ansidecl.h" 4598b9484cSchristos #include <stdio.h> 4698b9484cSchristos 4798b9484cSchristos /* Comment out all this code if we are using the GNU C Library, and are not 4898b9484cSchristos actually compiling the library itself. This code is part of the GNU C 4998b9484cSchristos Library, but also included in many other GNU distributions. Compiling 5098b9484cSchristos and linking in this code is a waste when using the GNU C library 5198b9484cSchristos (especially if it is a shared library). Rather than having every GNU 5298b9484cSchristos program understand `configure --with-gnu-libc' and omit the object files, 5398b9484cSchristos it is simpler to just do this in the source for each such file. */ 5498b9484cSchristos 5598b9484cSchristos #define GETOPT_INTERFACE_VERSION 2 5698b9484cSchristos #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 5798b9484cSchristos # include <gnu-versions.h> 5898b9484cSchristos # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION 5998b9484cSchristos # define ELIDE_CODE 6098b9484cSchristos # endif 6198b9484cSchristos #endif 6298b9484cSchristos 6398b9484cSchristos #ifndef ELIDE_CODE 6498b9484cSchristos 6598b9484cSchristos 6698b9484cSchristos /* This needs to come after some library #include 6798b9484cSchristos to get __GNU_LIBRARY__ defined. */ 6898b9484cSchristos #ifdef __GNU_LIBRARY__ 6998b9484cSchristos /* Don't include stdlib.h for non-GNU C libraries because some of them 7098b9484cSchristos contain conflicting prototypes for getopt. */ 7198b9484cSchristos # include <stdlib.h> 7298b9484cSchristos # include <unistd.h> 7398b9484cSchristos #endif /* GNU C library. */ 7498b9484cSchristos 7598b9484cSchristos #ifdef VMS 7698b9484cSchristos # include <unixlib.h> 7798b9484cSchristos # if HAVE_STRING_H - 0 7898b9484cSchristos # include <string.h> 7998b9484cSchristos # endif 8098b9484cSchristos #endif 8198b9484cSchristos 8298b9484cSchristos #ifndef _ 8398b9484cSchristos /* This is for other GNU distributions with internationalized messages. 8498b9484cSchristos When compiling libc, the _ macro is predefined. */ 8598b9484cSchristos # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC 8698b9484cSchristos # include <libintl.h> 8798b9484cSchristos # define _(msgid) gettext (msgid) 8898b9484cSchristos # else 8998b9484cSchristos # define _(msgid) (msgid) 9098b9484cSchristos # endif 9198b9484cSchristos #endif 9298b9484cSchristos 9398b9484cSchristos /* This version of `getopt' appears to the caller like standard Unix `getopt' 9498b9484cSchristos but it behaves differently for the user, since it allows the user 9598b9484cSchristos to intersperse the options with the other arguments. 9698b9484cSchristos 9798b9484cSchristos As `getopt' works, it permutes the elements of ARGV so that, 9898b9484cSchristos when it is done, all the options precede everything else. Thus 9998b9484cSchristos all application programs are extended to handle flexible argument order. 10098b9484cSchristos 10198b9484cSchristos Setting the environment variable POSIXLY_CORRECT disables permutation. 10298b9484cSchristos Then the behavior is completely standard. 10398b9484cSchristos 10498b9484cSchristos GNU application programs can use a third alternative mode in which 10598b9484cSchristos they can distinguish the relative order of options and other arguments. */ 10698b9484cSchristos 10798b9484cSchristos #include "getopt.h" 10898b9484cSchristos 10998b9484cSchristos /* For communication from `getopt' to the caller. 11098b9484cSchristos When `getopt' finds an option that takes an argument, 11198b9484cSchristos the argument value is returned here. 11298b9484cSchristos Also, when `ordering' is RETURN_IN_ORDER, 11398b9484cSchristos each non-option ARGV-element is returned here. */ 11498b9484cSchristos 11598b9484cSchristos char *optarg = NULL; 11698b9484cSchristos 11798b9484cSchristos /* Index in ARGV of the next element to be scanned. 11898b9484cSchristos This is used for communication to and from the caller 11998b9484cSchristos and for communication between successive calls to `getopt'. 12098b9484cSchristos 12198b9484cSchristos On entry to `getopt', zero means this is the first call; initialize. 12298b9484cSchristos 12398b9484cSchristos When `getopt' returns -1, this is the index of the first of the 12498b9484cSchristos non-option elements that the caller should itself scan. 12598b9484cSchristos 12698b9484cSchristos Otherwise, `optind' communicates from one call to the next 12798b9484cSchristos how much of ARGV has been scanned so far. */ 12898b9484cSchristos 12998b9484cSchristos /* 1003.2 says this must be 1 before any call. */ 13098b9484cSchristos int optind = 1; 13198b9484cSchristos 13298b9484cSchristos /* Formerly, initialization of getopt depended on optind==0, which 13398b9484cSchristos causes problems with re-calling getopt as programs generally don't 13498b9484cSchristos know that. */ 13598b9484cSchristos 13698b9484cSchristos int __getopt_initialized = 0; 13798b9484cSchristos 13898b9484cSchristos /* The next char to be scanned in the option-element 13998b9484cSchristos in which the last option character we returned was found. 14098b9484cSchristos This allows us to pick up the scan where we left off. 14198b9484cSchristos 14298b9484cSchristos If this is zero, or a null string, it means resume the scan 14398b9484cSchristos by advancing to the next ARGV-element. */ 14498b9484cSchristos 14598b9484cSchristos static char *nextchar; 14698b9484cSchristos 14798b9484cSchristos /* Callers store zero here to inhibit the error message 14898b9484cSchristos for unrecognized options. */ 14998b9484cSchristos 15098b9484cSchristos int opterr = 1; 15198b9484cSchristos 15298b9484cSchristos /* Set to an option character which was unrecognized. 15398b9484cSchristos This must be initialized on some systems to avoid linking in the 15498b9484cSchristos system's own getopt implementation. */ 15598b9484cSchristos 15698b9484cSchristos int optopt = '?'; 15798b9484cSchristos 15898b9484cSchristos /* Describe how to deal with options that follow non-option ARGV-elements. 15998b9484cSchristos 16098b9484cSchristos If the caller did not specify anything, 16198b9484cSchristos the default is REQUIRE_ORDER if the environment variable 16298b9484cSchristos POSIXLY_CORRECT is defined, PERMUTE otherwise. 16398b9484cSchristos 16498b9484cSchristos REQUIRE_ORDER means don't recognize them as options; 16598b9484cSchristos stop option processing when the first non-option is seen. 16698b9484cSchristos This is what Unix does. 16798b9484cSchristos This mode of operation is selected by either setting the environment 16898b9484cSchristos variable POSIXLY_CORRECT, or using `+' as the first character 16998b9484cSchristos of the list of option characters. 17098b9484cSchristos 17198b9484cSchristos PERMUTE is the default. We permute the contents of ARGV as we scan, 17298b9484cSchristos so that eventually all the non-options are at the end. This allows options 17398b9484cSchristos to be given in any order, even with programs that were not written to 17498b9484cSchristos expect this. 17598b9484cSchristos 17698b9484cSchristos RETURN_IN_ORDER is an option available to programs that were written 17798b9484cSchristos to expect options and other ARGV-elements in any order and that care about 17898b9484cSchristos the ordering of the two. We describe each non-option ARGV-element 17998b9484cSchristos as if it were the argument of an option with character code 1. 18098b9484cSchristos Using `-' as the first character of the list of option characters 18198b9484cSchristos selects this mode of operation. 18298b9484cSchristos 18398b9484cSchristos The special argument `--' forces an end of option-scanning regardless 18498b9484cSchristos of the value of `ordering'. In the case of RETURN_IN_ORDER, only 18598b9484cSchristos `--' can cause `getopt' to return -1 with `optind' != ARGC. */ 18698b9484cSchristos 18798b9484cSchristos static enum 18898b9484cSchristos { 18998b9484cSchristos REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER 19098b9484cSchristos } ordering; 19198b9484cSchristos 19298b9484cSchristos /* Value of POSIXLY_CORRECT environment variable. */ 19398b9484cSchristos static char *posixly_correct; 19498b9484cSchristos 19598b9484cSchristos #ifdef __GNU_LIBRARY__ 19698b9484cSchristos /* We want to avoid inclusion of string.h with non-GNU libraries 19798b9484cSchristos because there are many ways it can cause trouble. 19898b9484cSchristos On some systems, it contains special magic macros that don't work 19998b9484cSchristos in GCC. */ 20098b9484cSchristos # include <string.h> 20198b9484cSchristos # define my_index strchr 20298b9484cSchristos #else 20398b9484cSchristos 20498b9484cSchristos # if HAVE_STRING_H 20598b9484cSchristos # include <string.h> 20698b9484cSchristos # else 20798b9484cSchristos # if HAVE_STRINGS_H 20898b9484cSchristos # include <strings.h> 20998b9484cSchristos # endif 21098b9484cSchristos # endif 21198b9484cSchristos 21298b9484cSchristos /* Avoid depending on library functions or files 21398b9484cSchristos whose names are inconsistent. */ 21498b9484cSchristos 21598b9484cSchristos #if HAVE_STDLIB_H && HAVE_DECL_GETENV 21698b9484cSchristos # include <stdlib.h> 21798b9484cSchristos #elif !defined(getenv) 21898b9484cSchristos # ifdef __cplusplus 21998b9484cSchristos extern "C" { 22098b9484cSchristos # endif /* __cplusplus */ 22198b9484cSchristos extern char *getenv (const char *); 22298b9484cSchristos # ifdef __cplusplus 22398b9484cSchristos } 22498b9484cSchristos # endif /* __cplusplus */ 22598b9484cSchristos #endif 22698b9484cSchristos 22798b9484cSchristos static char * 22898b9484cSchristos my_index (const char *str, int chr) 22998b9484cSchristos { 23098b9484cSchristos while (*str) 23198b9484cSchristos { 23298b9484cSchristos if (*str == chr) 23398b9484cSchristos return (char *) str; 23498b9484cSchristos str++; 23598b9484cSchristos } 23698b9484cSchristos return 0; 23798b9484cSchristos } 23898b9484cSchristos 23998b9484cSchristos /* If using GCC, we can safely declare strlen this way. 24098b9484cSchristos If not using GCC, it is ok not to declare it. */ 24198b9484cSchristos #ifdef __GNUC__ 24298b9484cSchristos /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. 24398b9484cSchristos That was relevant to code that was here before. */ 24498b9484cSchristos # if (!defined __STDC__ || !__STDC__) && !defined strlen 24598b9484cSchristos /* gcc with -traditional declares the built-in strlen to return int, 24698b9484cSchristos and has done so at least since version 2.4.5. -- rms. */ 24798b9484cSchristos extern int strlen (const char *); 24898b9484cSchristos # endif /* not __STDC__ */ 24998b9484cSchristos #endif /* __GNUC__ */ 25098b9484cSchristos 25198b9484cSchristos #endif /* not __GNU_LIBRARY__ */ 25298b9484cSchristos 25398b9484cSchristos /* Handle permutation of arguments. */ 25498b9484cSchristos 25598b9484cSchristos /* Describe the part of ARGV that contains non-options that have 25698b9484cSchristos been skipped. `first_nonopt' is the index in ARGV of the first of them; 25798b9484cSchristos `last_nonopt' is the index after the last of them. */ 25898b9484cSchristos 25998b9484cSchristos static int first_nonopt; 26098b9484cSchristos static int last_nonopt; 26198b9484cSchristos 26298b9484cSchristos #ifdef _LIBC 26398b9484cSchristos /* Bash 2.0 gives us an environment variable containing flags 26498b9484cSchristos indicating ARGV elements that should not be considered arguments. */ 26598b9484cSchristos 26698b9484cSchristos /* Defined in getopt_init.c */ 26798b9484cSchristos extern char *__getopt_nonoption_flags; 26898b9484cSchristos 26998b9484cSchristos static int nonoption_flags_max_len; 27098b9484cSchristos static int nonoption_flags_len; 27198b9484cSchristos 27298b9484cSchristos static int original_argc; 27398b9484cSchristos static char *const *original_argv; 27498b9484cSchristos 27598b9484cSchristos /* Make sure the environment variable bash 2.0 puts in the environment 27698b9484cSchristos is valid for the getopt call we must make sure that the ARGV passed 27798b9484cSchristos to getopt is that one passed to the process. */ 27898b9484cSchristos static void 27998b9484cSchristos __attribute__ ((unused)) 28098b9484cSchristos store_args_and_env (int argc, char *const *argv) 28198b9484cSchristos { 28298b9484cSchristos /* XXX This is no good solution. We should rather copy the args so 28398b9484cSchristos that we can compare them later. But we must not use malloc(3). */ 28498b9484cSchristos original_argc = argc; 28598b9484cSchristos original_argv = argv; 28698b9484cSchristos } 28798b9484cSchristos # ifdef text_set_element 28898b9484cSchristos text_set_element (__libc_subinit, store_args_and_env); 28998b9484cSchristos # endif /* text_set_element */ 29098b9484cSchristos 29198b9484cSchristos # define SWAP_FLAGS(ch1, ch2) \ 29298b9484cSchristos if (nonoption_flags_len > 0) \ 29398b9484cSchristos { \ 29498b9484cSchristos char __tmp = __getopt_nonoption_flags[ch1]; \ 29598b9484cSchristos __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ 29698b9484cSchristos __getopt_nonoption_flags[ch2] = __tmp; \ 29798b9484cSchristos } 29898b9484cSchristos #else /* !_LIBC */ 29998b9484cSchristos # define SWAP_FLAGS(ch1, ch2) 30098b9484cSchristos #endif /* _LIBC */ 30198b9484cSchristos 30298b9484cSchristos /* Exchange two adjacent subsequences of ARGV. 30398b9484cSchristos One subsequence is elements [first_nonopt,last_nonopt) 30498b9484cSchristos which contains all the non-options that have been skipped so far. 30598b9484cSchristos The other is elements [last_nonopt,optind), which contains all 30698b9484cSchristos the options processed since those non-options were skipped. 30798b9484cSchristos 30898b9484cSchristos `first_nonopt' and `last_nonopt' are relocated so that they describe 30998b9484cSchristos the new indices of the non-options in ARGV after they are moved. */ 31098b9484cSchristos 31198b9484cSchristos #if defined __STDC__ && __STDC__ 31298b9484cSchristos static void exchange (char **); 31398b9484cSchristos #endif 31498b9484cSchristos 31598b9484cSchristos static void 31698b9484cSchristos exchange (char **argv) 31798b9484cSchristos { 31898b9484cSchristos int bottom = first_nonopt; 31998b9484cSchristos int middle = last_nonopt; 32098b9484cSchristos int top = optind; 32198b9484cSchristos char *tem; 32298b9484cSchristos 32398b9484cSchristos /* Exchange the shorter segment with the far end of the longer segment. 32498b9484cSchristos That puts the shorter segment into the right place. 32598b9484cSchristos It leaves the longer segment in the right place overall, 32698b9484cSchristos but it consists of two parts that need to be swapped next. */ 32798b9484cSchristos 32898b9484cSchristos #ifdef _LIBC 32998b9484cSchristos /* First make sure the handling of the `__getopt_nonoption_flags' 33098b9484cSchristos string can work normally. Our top argument must be in the range 33198b9484cSchristos of the string. */ 33298b9484cSchristos if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) 33398b9484cSchristos { 33498b9484cSchristos /* We must extend the array. The user plays games with us and 33598b9484cSchristos presents new arguments. */ 33698b9484cSchristos char *new_str = (char *) malloc (top + 1); 33798b9484cSchristos if (new_str == NULL) 33898b9484cSchristos nonoption_flags_len = nonoption_flags_max_len = 0; 33998b9484cSchristos else 34098b9484cSchristos { 34198b9484cSchristos memset (mempcpy (new_str, __getopt_nonoption_flags, 34298b9484cSchristos nonoption_flags_max_len), 34398b9484cSchristos '\0', top + 1 - nonoption_flags_max_len); 34498b9484cSchristos nonoption_flags_max_len = top + 1; 34598b9484cSchristos __getopt_nonoption_flags = new_str; 34698b9484cSchristos } 34798b9484cSchristos } 34898b9484cSchristos #endif 34998b9484cSchristos 35098b9484cSchristos while (top > middle && middle > bottom) 35198b9484cSchristos { 35298b9484cSchristos if (top - middle > middle - bottom) 35398b9484cSchristos { 35498b9484cSchristos /* Bottom segment is the short one. */ 35598b9484cSchristos int len = middle - bottom; 35698b9484cSchristos register int i; 35798b9484cSchristos 35898b9484cSchristos /* Swap it with the top part of the top segment. */ 35998b9484cSchristos for (i = 0; i < len; i++) 36098b9484cSchristos { 36198b9484cSchristos tem = argv[bottom + i]; 36298b9484cSchristos argv[bottom + i] = argv[top - (middle - bottom) + i]; 36398b9484cSchristos argv[top - (middle - bottom) + i] = tem; 36498b9484cSchristos SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); 36598b9484cSchristos } 36698b9484cSchristos /* Exclude the moved bottom segment from further swapping. */ 36798b9484cSchristos top -= len; 36898b9484cSchristos } 36998b9484cSchristos else 37098b9484cSchristos { 37198b9484cSchristos /* Top segment is the short one. */ 37298b9484cSchristos int len = top - middle; 37398b9484cSchristos register int i; 37498b9484cSchristos 37598b9484cSchristos /* Swap it with the bottom part of the bottom segment. */ 37698b9484cSchristos for (i = 0; i < len; i++) 37798b9484cSchristos { 37898b9484cSchristos tem = argv[bottom + i]; 37998b9484cSchristos argv[bottom + i] = argv[middle + i]; 38098b9484cSchristos argv[middle + i] = tem; 38198b9484cSchristos SWAP_FLAGS (bottom + i, middle + i); 38298b9484cSchristos } 38398b9484cSchristos /* Exclude the moved top segment from further swapping. */ 38498b9484cSchristos bottom += len; 38598b9484cSchristos } 38698b9484cSchristos } 38798b9484cSchristos 38898b9484cSchristos /* Update records for the slots the non-options now occupy. */ 38998b9484cSchristos 39098b9484cSchristos first_nonopt += (optind - last_nonopt); 39198b9484cSchristos last_nonopt = optind; 39298b9484cSchristos } 39398b9484cSchristos 39498b9484cSchristos /* Initialize the internal data when the first call is made. */ 39598b9484cSchristos 39698b9484cSchristos #if defined __STDC__ && __STDC__ 39798b9484cSchristos static const char *_getopt_initialize (int, char *const *, const char *); 39898b9484cSchristos #endif 39998b9484cSchristos static const char * 40098b9484cSchristos _getopt_initialize (int argc ATTRIBUTE_UNUSED, 40198b9484cSchristos char *const *argv ATTRIBUTE_UNUSED, 40298b9484cSchristos const char *optstring) 40398b9484cSchristos { 40498b9484cSchristos /* Start processing options with ARGV-element 1 (since ARGV-element 0 40598b9484cSchristos is the program name); the sequence of previously skipped 40698b9484cSchristos non-option ARGV-elements is empty. */ 40798b9484cSchristos 40898b9484cSchristos first_nonopt = last_nonopt = optind; 40998b9484cSchristos 41098b9484cSchristos nextchar = NULL; 41198b9484cSchristos 41298b9484cSchristos posixly_correct = getenv ("POSIXLY_CORRECT"); 41398b9484cSchristos 41498b9484cSchristos /* Determine how to handle the ordering of options and nonoptions. */ 41598b9484cSchristos 41698b9484cSchristos if (optstring[0] == '-') 41798b9484cSchristos { 41898b9484cSchristos ordering = RETURN_IN_ORDER; 41998b9484cSchristos ++optstring; 42098b9484cSchristos } 42198b9484cSchristos else if (optstring[0] == '+') 42298b9484cSchristos { 42398b9484cSchristos ordering = REQUIRE_ORDER; 42498b9484cSchristos ++optstring; 42598b9484cSchristos } 42698b9484cSchristos else if (posixly_correct != NULL) 42798b9484cSchristos ordering = REQUIRE_ORDER; 42898b9484cSchristos else 42998b9484cSchristos ordering = PERMUTE; 43098b9484cSchristos 43198b9484cSchristos #ifdef _LIBC 43298b9484cSchristos if (posixly_correct == NULL 43398b9484cSchristos && argc == original_argc && argv == original_argv) 43498b9484cSchristos { 43598b9484cSchristos if (nonoption_flags_max_len == 0) 43698b9484cSchristos { 43798b9484cSchristos if (__getopt_nonoption_flags == NULL 43898b9484cSchristos || __getopt_nonoption_flags[0] == '\0') 43998b9484cSchristos nonoption_flags_max_len = -1; 44098b9484cSchristos else 44198b9484cSchristos { 44298b9484cSchristos const char *orig_str = __getopt_nonoption_flags; 44398b9484cSchristos int len = nonoption_flags_max_len = strlen (orig_str); 44498b9484cSchristos if (nonoption_flags_max_len < argc) 44598b9484cSchristos nonoption_flags_max_len = argc; 44698b9484cSchristos __getopt_nonoption_flags = 44798b9484cSchristos (char *) malloc (nonoption_flags_max_len); 44898b9484cSchristos if (__getopt_nonoption_flags == NULL) 44998b9484cSchristos nonoption_flags_max_len = -1; 45098b9484cSchristos else 45198b9484cSchristos memset (mempcpy (__getopt_nonoption_flags, orig_str, len), 45298b9484cSchristos '\0', nonoption_flags_max_len - len); 45398b9484cSchristos } 45498b9484cSchristos } 45598b9484cSchristos nonoption_flags_len = nonoption_flags_max_len; 45698b9484cSchristos } 45798b9484cSchristos else 45898b9484cSchristos nonoption_flags_len = 0; 45998b9484cSchristos #endif 46098b9484cSchristos 46198b9484cSchristos return optstring; 46298b9484cSchristos } 46398b9484cSchristos 46498b9484cSchristos /* Scan elements of ARGV (whose length is ARGC) for option characters 46598b9484cSchristos given in OPTSTRING. 46698b9484cSchristos 46798b9484cSchristos If an element of ARGV starts with '-', and is not exactly "-" or "--", 46898b9484cSchristos then it is an option element. The characters of this element 46998b9484cSchristos (aside from the initial '-') are option characters. If `getopt' 47098b9484cSchristos is called repeatedly, it returns successively each of the option characters 47198b9484cSchristos from each of the option elements. 47298b9484cSchristos 47398b9484cSchristos If `getopt' finds another option character, it returns that character, 47498b9484cSchristos updating `optind' and `nextchar' so that the next call to `getopt' can 47598b9484cSchristos resume the scan with the following option character or ARGV-element. 47698b9484cSchristos 47798b9484cSchristos If there are no more option characters, `getopt' returns -1. 47898b9484cSchristos Then `optind' is the index in ARGV of the first ARGV-element 47998b9484cSchristos that is not an option. (The ARGV-elements have been permuted 48098b9484cSchristos so that those that are not options now come last.) 48198b9484cSchristos 48298b9484cSchristos OPTSTRING is a string containing the legitimate option characters. 48398b9484cSchristos If an option character is seen that is not listed in OPTSTRING, 48498b9484cSchristos return '?' after printing an error message. If you set `opterr' to 48598b9484cSchristos zero, the error message is suppressed but we still return '?'. 48698b9484cSchristos 48798b9484cSchristos If a char in OPTSTRING is followed by a colon, that means it wants an arg, 48898b9484cSchristos so the following text in the same ARGV-element, or the text of the following 48998b9484cSchristos ARGV-element, is returned in `optarg'. Two colons mean an option that 49098b9484cSchristos wants an optional arg; if there is text in the current ARGV-element, 49198b9484cSchristos it is returned in `optarg', otherwise `optarg' is set to zero. 49298b9484cSchristos 49398b9484cSchristos If OPTSTRING starts with `-' or `+', it requests different methods of 49498b9484cSchristos handling the non-option ARGV-elements. 49598b9484cSchristos See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. 49698b9484cSchristos 49798b9484cSchristos Long-named options begin with `--' instead of `-'. 49898b9484cSchristos Their names may be abbreviated as long as the abbreviation is unique 49998b9484cSchristos or is an exact match for some defined option. If they have an 50098b9484cSchristos argument, it follows the option name in the same ARGV-element, separated 50198b9484cSchristos from the option name by a `=', or else the in next ARGV-element. 50298b9484cSchristos When `getopt' finds a long-named option, it returns 0 if that option's 50398b9484cSchristos `flag' field is nonzero, the value of the option's `val' field 50498b9484cSchristos if the `flag' field is zero. 50598b9484cSchristos 50698b9484cSchristos The elements of ARGV aren't really const, because we permute them. 50798b9484cSchristos But we pretend they're const in the prototype to be compatible 50898b9484cSchristos with other systems. 50998b9484cSchristos 51098b9484cSchristos LONGOPTS is a vector of `struct option' terminated by an 51198b9484cSchristos element containing a name which is zero. 51298b9484cSchristos 51398b9484cSchristos LONGIND returns the index in LONGOPT of the long-named option found. 51498b9484cSchristos It is only valid when a long-named option has been found by the most 51598b9484cSchristos recent call. 51698b9484cSchristos 51798b9484cSchristos If LONG_ONLY is nonzero, '-' as well as '--' can introduce 51898b9484cSchristos long-named options. */ 51998b9484cSchristos 52098b9484cSchristos int 52198b9484cSchristos _getopt_internal (int argc, char *const *argv, const char *optstring, 52298b9484cSchristos const struct option *longopts, 52398b9484cSchristos int *longind, int long_only) 52498b9484cSchristos { 52598b9484cSchristos optarg = NULL; 52698b9484cSchristos 52798b9484cSchristos if (optind == 0 || !__getopt_initialized) 52898b9484cSchristos { 52998b9484cSchristos if (optind == 0) 53098b9484cSchristos optind = 1; /* Don't scan ARGV[0], the program name. */ 53198b9484cSchristos optstring = _getopt_initialize (argc, argv, optstring); 53298b9484cSchristos __getopt_initialized = 1; 53398b9484cSchristos } 53498b9484cSchristos 53598b9484cSchristos /* Test whether ARGV[optind] points to a non-option argument. 53698b9484cSchristos Either it does not have option syntax, or there is an environment flag 53798b9484cSchristos from the shell indicating it is not an option. The later information 53898b9484cSchristos is only used when the used in the GNU libc. */ 53998b9484cSchristos #ifdef _LIBC 54098b9484cSchristos # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ 54198b9484cSchristos || (optind < nonoption_flags_len \ 54298b9484cSchristos && __getopt_nonoption_flags[optind] == '1')) 54398b9484cSchristos #else 54498b9484cSchristos # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') 54598b9484cSchristos #endif 54698b9484cSchristos 54798b9484cSchristos if (nextchar == NULL || *nextchar == '\0') 54898b9484cSchristos { 54998b9484cSchristos /* Advance to the next ARGV-element. */ 55098b9484cSchristos 55198b9484cSchristos /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been 55298b9484cSchristos moved back by the user (who may also have changed the arguments). */ 55398b9484cSchristos if (last_nonopt > optind) 55498b9484cSchristos last_nonopt = optind; 55598b9484cSchristos if (first_nonopt > optind) 55698b9484cSchristos first_nonopt = optind; 55798b9484cSchristos 55898b9484cSchristos if (ordering == PERMUTE) 55998b9484cSchristos { 56098b9484cSchristos /* If we have just processed some options following some non-options, 56198b9484cSchristos exchange them so that the options come first. */ 56298b9484cSchristos 56398b9484cSchristos if (first_nonopt != last_nonopt && last_nonopt != optind) 56498b9484cSchristos exchange ((char **) argv); 56598b9484cSchristos else if (last_nonopt != optind) 56698b9484cSchristos first_nonopt = optind; 56798b9484cSchristos 56898b9484cSchristos /* Skip any additional non-options 56998b9484cSchristos and extend the range of non-options previously skipped. */ 57098b9484cSchristos 57198b9484cSchristos while (optind < argc && NONOPTION_P) 57298b9484cSchristos optind++; 57398b9484cSchristos last_nonopt = optind; 57498b9484cSchristos } 57598b9484cSchristos 57698b9484cSchristos /* The special ARGV-element `--' means premature end of options. 57798b9484cSchristos Skip it like a null option, 57898b9484cSchristos then exchange with previous non-options as if it were an option, 57998b9484cSchristos then skip everything else like a non-option. */ 58098b9484cSchristos 58198b9484cSchristos if (optind != argc && !strcmp (argv[optind], "--")) 58298b9484cSchristos { 58398b9484cSchristos optind++; 58498b9484cSchristos 58598b9484cSchristos if (first_nonopt != last_nonopt && last_nonopt != optind) 58698b9484cSchristos exchange ((char **) argv); 58798b9484cSchristos else if (first_nonopt == last_nonopt) 58898b9484cSchristos first_nonopt = optind; 58998b9484cSchristos last_nonopt = argc; 59098b9484cSchristos 59198b9484cSchristos optind = argc; 59298b9484cSchristos } 59398b9484cSchristos 59498b9484cSchristos /* If we have done all the ARGV-elements, stop the scan 59598b9484cSchristos and back over any non-options that we skipped and permuted. */ 59698b9484cSchristos 59798b9484cSchristos if (optind == argc) 59898b9484cSchristos { 59998b9484cSchristos /* Set the next-arg-index to point at the non-options 60098b9484cSchristos that we previously skipped, so the caller will digest them. */ 60198b9484cSchristos if (first_nonopt != last_nonopt) 60298b9484cSchristos optind = first_nonopt; 60398b9484cSchristos return -1; 60498b9484cSchristos } 60598b9484cSchristos 60698b9484cSchristos /* If we have come to a non-option and did not permute it, 60798b9484cSchristos either stop the scan or describe it to the caller and pass it by. */ 60898b9484cSchristos 60998b9484cSchristos if (NONOPTION_P) 61098b9484cSchristos { 61198b9484cSchristos if (ordering == REQUIRE_ORDER) 61298b9484cSchristos return -1; 61398b9484cSchristos optarg = argv[optind++]; 61498b9484cSchristos return 1; 61598b9484cSchristos } 61698b9484cSchristos 61798b9484cSchristos /* We have found another option-ARGV-element. 61898b9484cSchristos Skip the initial punctuation. */ 61998b9484cSchristos 62098b9484cSchristos nextchar = (argv[optind] + 1 62198b9484cSchristos + (longopts != NULL && argv[optind][1] == '-')); 62298b9484cSchristos } 62398b9484cSchristos 62498b9484cSchristos /* Decode the current option-ARGV-element. */ 62598b9484cSchristos 62698b9484cSchristos /* Check whether the ARGV-element is a long option. 62798b9484cSchristos 62898b9484cSchristos If long_only and the ARGV-element has the form "-f", where f is 62998b9484cSchristos a valid short option, don't consider it an abbreviated form of 63098b9484cSchristos a long option that starts with f. Otherwise there would be no 63198b9484cSchristos way to give the -f short option. 63298b9484cSchristos 63398b9484cSchristos On the other hand, if there's a long option "fubar" and 63498b9484cSchristos the ARGV-element is "-fu", do consider that an abbreviation of 63598b9484cSchristos the long option, just like "--fu", and not "-f" with arg "u". 63698b9484cSchristos 63798b9484cSchristos This distinction seems to be the most useful approach. */ 63898b9484cSchristos 63998b9484cSchristos if (longopts != NULL 64098b9484cSchristos && (argv[optind][1] == '-' 64198b9484cSchristos || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) 64298b9484cSchristos { 64398b9484cSchristos char *nameend; 64498b9484cSchristos const struct option *p; 64598b9484cSchristos const struct option *pfound = NULL; 64698b9484cSchristos int exact = 0; 64798b9484cSchristos int ambig = 0; 64898b9484cSchristos int indfound = -1; 64998b9484cSchristos int option_index; 65098b9484cSchristos 65198b9484cSchristos for (nameend = nextchar; *nameend && *nameend != '='; nameend++) 65298b9484cSchristos /* Do nothing. */ ; 65398b9484cSchristos 65498b9484cSchristos /* Test all long options for either exact match 65598b9484cSchristos or abbreviated matches. */ 65698b9484cSchristos for (p = longopts, option_index = 0; p->name; p++, option_index++) 65798b9484cSchristos if (!strncmp (p->name, nextchar, nameend - nextchar)) 65898b9484cSchristos { 65998b9484cSchristos if ((unsigned int) (nameend - nextchar) 66098b9484cSchristos == (unsigned int) strlen (p->name)) 66198b9484cSchristos { 66298b9484cSchristos /* Exact match found. */ 66398b9484cSchristos pfound = p; 66498b9484cSchristos indfound = option_index; 66598b9484cSchristos exact = 1; 66698b9484cSchristos break; 66798b9484cSchristos } 66898b9484cSchristos else if (pfound == NULL) 66998b9484cSchristos { 67098b9484cSchristos /* First nonexact match found. */ 67198b9484cSchristos pfound = p; 67298b9484cSchristos indfound = option_index; 67398b9484cSchristos } 67498b9484cSchristos else 67598b9484cSchristos /* Second or later nonexact match found. */ 67698b9484cSchristos ambig = 1; 67798b9484cSchristos } 67898b9484cSchristos 67998b9484cSchristos if (ambig && !exact) 68098b9484cSchristos { 68198b9484cSchristos if (opterr) 68298b9484cSchristos fprintf (stderr, _("%s: option `%s' is ambiguous\n"), 68398b9484cSchristos argv[0], argv[optind]); 68498b9484cSchristos nextchar += strlen (nextchar); 68598b9484cSchristos optind++; 68698b9484cSchristos optopt = 0; 68798b9484cSchristos return '?'; 68898b9484cSchristos } 68998b9484cSchristos 69098b9484cSchristos if (pfound != NULL) 69198b9484cSchristos { 69298b9484cSchristos option_index = indfound; 69398b9484cSchristos optind++; 69498b9484cSchristos if (*nameend) 69598b9484cSchristos { 69698b9484cSchristos /* Don't test has_arg with >, because some C compilers don't 69798b9484cSchristos allow it to be used on enums. */ 69898b9484cSchristos if (pfound->has_arg) 69998b9484cSchristos optarg = nameend + 1; 70098b9484cSchristos else 70198b9484cSchristos { 70298b9484cSchristos if (opterr) 70398b9484cSchristos { 70498b9484cSchristos if (argv[optind - 1][1] == '-') 70598b9484cSchristos /* --option */ 70698b9484cSchristos fprintf (stderr, 70798b9484cSchristos _("%s: option `--%s' doesn't allow an argument\n"), 70898b9484cSchristos argv[0], pfound->name); 70998b9484cSchristos else 71098b9484cSchristos /* +option or -option */ 71198b9484cSchristos fprintf (stderr, 71298b9484cSchristos _("%s: option `%c%s' doesn't allow an argument\n"), 71398b9484cSchristos argv[0], argv[optind - 1][0], pfound->name); 71498b9484cSchristos 71598b9484cSchristos nextchar += strlen (nextchar); 71698b9484cSchristos 71798b9484cSchristos optopt = pfound->val; 71898b9484cSchristos return '?'; 71998b9484cSchristos } 72098b9484cSchristos } 72198b9484cSchristos } 72298b9484cSchristos else if (pfound->has_arg == 1) 72398b9484cSchristos { 72498b9484cSchristos if (optind < argc) 72598b9484cSchristos optarg = argv[optind++]; 72698b9484cSchristos else 72798b9484cSchristos { 72898b9484cSchristos if (opterr) 72998b9484cSchristos fprintf (stderr, 73098b9484cSchristos _("%s: option `%s' requires an argument\n"), 73198b9484cSchristos argv[0], argv[optind - 1]); 73298b9484cSchristos nextchar += strlen (nextchar); 73398b9484cSchristos optopt = pfound->val; 73498b9484cSchristos return optstring[0] == ':' ? ':' : '?'; 73598b9484cSchristos } 73698b9484cSchristos } 73798b9484cSchristos nextchar += strlen (nextchar); 73898b9484cSchristos if (longind != NULL) 73998b9484cSchristos *longind = option_index; 74098b9484cSchristos if (pfound->flag) 74198b9484cSchristos { 74298b9484cSchristos *(pfound->flag) = pfound->val; 74398b9484cSchristos return 0; 74498b9484cSchristos } 74598b9484cSchristos return pfound->val; 74698b9484cSchristos } 74798b9484cSchristos 74898b9484cSchristos /* Can't find it as a long option. If this is not getopt_long_only, 74998b9484cSchristos or the option starts with '--' or is not a valid short 75098b9484cSchristos option, then it's an error. 75198b9484cSchristos Otherwise interpret it as a short option. */ 75298b9484cSchristos if (!long_only || argv[optind][1] == '-' 75398b9484cSchristos || my_index (optstring, *nextchar) == NULL) 75498b9484cSchristos { 75598b9484cSchristos if (opterr) 75698b9484cSchristos { 75798b9484cSchristos if (argv[optind][1] == '-') 75898b9484cSchristos /* --option */ 75998b9484cSchristos fprintf (stderr, _("%s: unrecognized option `--%s'\n"), 76098b9484cSchristos argv[0], nextchar); 76198b9484cSchristos else 76298b9484cSchristos /* +option or -option */ 76398b9484cSchristos fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), 76498b9484cSchristos argv[0], argv[optind][0], nextchar); 76598b9484cSchristos } 76698b9484cSchristos nextchar = (char *) ""; 76798b9484cSchristos optind++; 76898b9484cSchristos optopt = 0; 76998b9484cSchristos return '?'; 77098b9484cSchristos } 77198b9484cSchristos } 77298b9484cSchristos 77398b9484cSchristos /* Look at and handle the next short option-character. */ 77498b9484cSchristos 77598b9484cSchristos { 77698b9484cSchristos char c = *nextchar++; 77798b9484cSchristos char *temp = my_index (optstring, c); 77898b9484cSchristos 77998b9484cSchristos /* Increment `optind' when we start to process its last character. */ 78098b9484cSchristos if (*nextchar == '\0') 78198b9484cSchristos ++optind; 78298b9484cSchristos 78398b9484cSchristos if (temp == NULL || c == ':') 78498b9484cSchristos { 78598b9484cSchristos if (opterr) 78698b9484cSchristos { 78798b9484cSchristos if (posixly_correct) 78898b9484cSchristos /* 1003.2 specifies the format of this message. */ 78998b9484cSchristos fprintf (stderr, _("%s: illegal option -- %c\n"), 79098b9484cSchristos argv[0], c); 79198b9484cSchristos else 79298b9484cSchristos fprintf (stderr, _("%s: invalid option -- %c\n"), 79398b9484cSchristos argv[0], c); 79498b9484cSchristos } 79598b9484cSchristos optopt = c; 79698b9484cSchristos return '?'; 79798b9484cSchristos } 79898b9484cSchristos /* Convenience. Treat POSIX -W foo same as long option --foo */ 79998b9484cSchristos if (temp[0] == 'W' && temp[1] == ';') 80098b9484cSchristos { 80198b9484cSchristos char *nameend; 80298b9484cSchristos const struct option *p; 80398b9484cSchristos const struct option *pfound = NULL; 80498b9484cSchristos int exact = 0; 80598b9484cSchristos int ambig = 0; 80698b9484cSchristos int indfound = 0; 80798b9484cSchristos int option_index; 80898b9484cSchristos 80998b9484cSchristos /* This is an option that requires an argument. */ 81098b9484cSchristos if (*nextchar != '\0') 81198b9484cSchristos { 81298b9484cSchristos optarg = nextchar; 81398b9484cSchristos /* If we end this ARGV-element by taking the rest as an arg, 81498b9484cSchristos we must advance to the next element now. */ 81598b9484cSchristos optind++; 81698b9484cSchristos } 81798b9484cSchristos else if (optind == argc) 81898b9484cSchristos { 81998b9484cSchristos if (opterr) 82098b9484cSchristos { 82198b9484cSchristos /* 1003.2 specifies the format of this message. */ 82298b9484cSchristos fprintf (stderr, _("%s: option requires an argument -- %c\n"), 82398b9484cSchristos argv[0], c); 82498b9484cSchristos } 82598b9484cSchristos optopt = c; 82698b9484cSchristos if (optstring[0] == ':') 82798b9484cSchristos c = ':'; 82898b9484cSchristos else 82998b9484cSchristos c = '?'; 83098b9484cSchristos return c; 83198b9484cSchristos } 83298b9484cSchristos else 83398b9484cSchristos /* We already incremented `optind' once; 83498b9484cSchristos increment it again when taking next ARGV-elt as argument. */ 83598b9484cSchristos optarg = argv[optind++]; 83698b9484cSchristos 83798b9484cSchristos /* optarg is now the argument, see if it's in the 83898b9484cSchristos table of longopts. */ 83998b9484cSchristos 84098b9484cSchristos for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) 84198b9484cSchristos /* Do nothing. */ ; 84298b9484cSchristos 84398b9484cSchristos /* Test all long options for either exact match 84498b9484cSchristos or abbreviated matches. */ 84598b9484cSchristos for (p = longopts, option_index = 0; p->name; p++, option_index++) 84698b9484cSchristos if (!strncmp (p->name, nextchar, nameend - nextchar)) 84798b9484cSchristos { 84898b9484cSchristos if ((unsigned int) (nameend - nextchar) == strlen (p->name)) 84998b9484cSchristos { 85098b9484cSchristos /* Exact match found. */ 85198b9484cSchristos pfound = p; 85298b9484cSchristos indfound = option_index; 85398b9484cSchristos exact = 1; 85498b9484cSchristos break; 85598b9484cSchristos } 85698b9484cSchristos else if (pfound == NULL) 85798b9484cSchristos { 85898b9484cSchristos /* First nonexact match found. */ 85998b9484cSchristos pfound = p; 86098b9484cSchristos indfound = option_index; 86198b9484cSchristos } 86298b9484cSchristos else 86398b9484cSchristos /* Second or later nonexact match found. */ 86498b9484cSchristos ambig = 1; 86598b9484cSchristos } 86698b9484cSchristos if (ambig && !exact) 86798b9484cSchristos { 86898b9484cSchristos if (opterr) 86998b9484cSchristos fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), 87098b9484cSchristos argv[0], argv[optind]); 87198b9484cSchristos nextchar += strlen (nextchar); 87298b9484cSchristos optind++; 87398b9484cSchristos return '?'; 87498b9484cSchristos } 87598b9484cSchristos if (pfound != NULL) 87698b9484cSchristos { 87798b9484cSchristos option_index = indfound; 87898b9484cSchristos if (*nameend) 87998b9484cSchristos { 88098b9484cSchristos /* Don't test has_arg with >, because some C compilers don't 88198b9484cSchristos allow it to be used on enums. */ 88298b9484cSchristos if (pfound->has_arg) 88398b9484cSchristos optarg = nameend + 1; 88498b9484cSchristos else 88598b9484cSchristos { 88698b9484cSchristos if (opterr) 88798b9484cSchristos fprintf (stderr, _("\ 88898b9484cSchristos %s: option `-W %s' doesn't allow an argument\n"), 88998b9484cSchristos argv[0], pfound->name); 89098b9484cSchristos 89198b9484cSchristos nextchar += strlen (nextchar); 89298b9484cSchristos return '?'; 89398b9484cSchristos } 89498b9484cSchristos } 89598b9484cSchristos else if (pfound->has_arg == 1) 89698b9484cSchristos { 89798b9484cSchristos if (optind < argc) 89898b9484cSchristos optarg = argv[optind++]; 89998b9484cSchristos else 90098b9484cSchristos { 90198b9484cSchristos if (opterr) 90298b9484cSchristos fprintf (stderr, 90398b9484cSchristos _("%s: option `%s' requires an argument\n"), 90498b9484cSchristos argv[0], argv[optind - 1]); 90598b9484cSchristos nextchar += strlen (nextchar); 90698b9484cSchristos return optstring[0] == ':' ? ':' : '?'; 90798b9484cSchristos } 90898b9484cSchristos } 90998b9484cSchristos nextchar += strlen (nextchar); 91098b9484cSchristos if (longind != NULL) 91198b9484cSchristos *longind = option_index; 91298b9484cSchristos if (pfound->flag) 91398b9484cSchristos { 91498b9484cSchristos *(pfound->flag) = pfound->val; 91598b9484cSchristos return 0; 91698b9484cSchristos } 91798b9484cSchristos return pfound->val; 91898b9484cSchristos } 91998b9484cSchristos nextchar = NULL; 92098b9484cSchristos return 'W'; /* Let the application handle it. */ 92198b9484cSchristos } 92298b9484cSchristos if (temp[1] == ':') 92398b9484cSchristos { 92498b9484cSchristos if (temp[2] == ':') 92598b9484cSchristos { 92698b9484cSchristos /* This is an option that accepts an argument optionally. */ 92798b9484cSchristos if (*nextchar != '\0') 92898b9484cSchristos { 92998b9484cSchristos optarg = nextchar; 93098b9484cSchristos optind++; 93198b9484cSchristos } 93298b9484cSchristos else 93398b9484cSchristos optarg = NULL; 93498b9484cSchristos nextchar = NULL; 93598b9484cSchristos } 93698b9484cSchristos else 93798b9484cSchristos { 93898b9484cSchristos /* This is an option that requires an argument. */ 93998b9484cSchristos if (*nextchar != '\0') 94098b9484cSchristos { 94198b9484cSchristos optarg = nextchar; 94298b9484cSchristos /* If we end this ARGV-element by taking the rest as an arg, 94398b9484cSchristos we must advance to the next element now. */ 94498b9484cSchristos optind++; 94598b9484cSchristos } 94698b9484cSchristos else if (optind == argc) 94798b9484cSchristos { 94898b9484cSchristos if (opterr) 94998b9484cSchristos { 95098b9484cSchristos /* 1003.2 specifies the format of this message. */ 95198b9484cSchristos fprintf (stderr, 95298b9484cSchristos _("%s: option requires an argument -- %c\n"), 95398b9484cSchristos argv[0], c); 95498b9484cSchristos } 95598b9484cSchristos optopt = c; 95698b9484cSchristos if (optstring[0] == ':') 95798b9484cSchristos c = ':'; 95898b9484cSchristos else 95998b9484cSchristos c = '?'; 96098b9484cSchristos } 96198b9484cSchristos else 96298b9484cSchristos /* We already incremented `optind' once; 96398b9484cSchristos increment it again when taking next ARGV-elt as argument. */ 96498b9484cSchristos optarg = argv[optind++]; 96598b9484cSchristos nextchar = NULL; 96698b9484cSchristos } 96798b9484cSchristos } 96898b9484cSchristos return c; 96998b9484cSchristos } 97098b9484cSchristos } 97198b9484cSchristos 97298b9484cSchristos int 97398b9484cSchristos getopt (int argc, char *const *argv, const char *optstring) 97498b9484cSchristos { 97598b9484cSchristos return _getopt_internal (argc, argv, optstring, 97698b9484cSchristos (const struct option *) 0, 97798b9484cSchristos (int *) 0, 97898b9484cSchristos 0); 97998b9484cSchristos } 98098b9484cSchristos 98198b9484cSchristos #endif /* Not ELIDE_CODE. */ 98298b9484cSchristos 98398b9484cSchristos #ifdef TEST 98498b9484cSchristos 98598b9484cSchristos /* Compile with -DTEST to make an executable for use in testing 98698b9484cSchristos the above definition of `getopt'. */ 98798b9484cSchristos 98898b9484cSchristos int 98998b9484cSchristos main (int argc, char **argv) 99098b9484cSchristos { 99198b9484cSchristos int c; 99298b9484cSchristos int digit_optind = 0; 99398b9484cSchristos 99498b9484cSchristos while (1) 99598b9484cSchristos { 99698b9484cSchristos int this_option_optind = optind ? optind : 1; 99798b9484cSchristos 99898b9484cSchristos c = getopt (argc, argv, "abc:d:0123456789"); 99998b9484cSchristos if (c == -1) 100098b9484cSchristos break; 100198b9484cSchristos 100298b9484cSchristos switch (c) 100398b9484cSchristos { 100498b9484cSchristos case '0': 100598b9484cSchristos case '1': 100698b9484cSchristos case '2': 100798b9484cSchristos case '3': 100898b9484cSchristos case '4': 100998b9484cSchristos case '5': 101098b9484cSchristos case '6': 101198b9484cSchristos case '7': 101298b9484cSchristos case '8': 101398b9484cSchristos case '9': 101498b9484cSchristos if (digit_optind != 0 && digit_optind != this_option_optind) 101598b9484cSchristos printf ("digits occur in two different argv-elements.\n"); 101698b9484cSchristos digit_optind = this_option_optind; 101798b9484cSchristos printf ("option %c\n", c); 101898b9484cSchristos break; 101998b9484cSchristos 102098b9484cSchristos case 'a': 102198b9484cSchristos printf ("option a\n"); 102298b9484cSchristos break; 102398b9484cSchristos 102498b9484cSchristos case 'b': 102598b9484cSchristos printf ("option b\n"); 102698b9484cSchristos break; 102798b9484cSchristos 102898b9484cSchristos case 'c': 102998b9484cSchristos printf ("option c with value `%s'\n", optarg); 103098b9484cSchristos break; 103198b9484cSchristos 103298b9484cSchristos case '?': 103398b9484cSchristos break; 103498b9484cSchristos 103598b9484cSchristos default: 103698b9484cSchristos printf ("?? getopt returned character code 0%o ??\n", c); 103798b9484cSchristos } 103898b9484cSchristos } 103998b9484cSchristos 104098b9484cSchristos if (optind < argc) 104198b9484cSchristos { 104298b9484cSchristos printf ("non-option ARGV-elements: "); 104398b9484cSchristos while (optind < argc) 104498b9484cSchristos printf ("%s ", argv[optind++]); 104598b9484cSchristos printf ("\n"); 104698b9484cSchristos } 104798b9484cSchristos 104898b9484cSchristos exit (0); 104998b9484cSchristos } 105098b9484cSchristos 105198b9484cSchristos #endif /* TEST */ 1052