xref: /dflybsd-src/contrib/gcc-8.0/libiberty/getopt.c (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* Getopt for GNU.
2*38fd1498Szrj    NOTE: getopt is now part of the C library, so if you don't know what
3*38fd1498Szrj    "Keep this file name-space clean" means, talk to drepper@gnu.org
4*38fd1498Szrj    before changing it!
5*38fd1498Szrj 
6*38fd1498Szrj    Copyright (C) 1987-2018 Free Software Foundation, Inc.
7*38fd1498Szrj 
8*38fd1498Szrj    NOTE: This source is derived from an old version taken from the GNU C
9*38fd1498Szrj    Library (glibc).
10*38fd1498Szrj 
11*38fd1498Szrj    This program is free software; you can redistribute it and/or modify it
12*38fd1498Szrj    under the terms of the GNU General Public License as published by the
13*38fd1498Szrj    Free Software Foundation; either version 2, or (at your option) any
14*38fd1498Szrj    later version.
15*38fd1498Szrj 
16*38fd1498Szrj    This program is distributed in the hope that it will be useful,
17*38fd1498Szrj    but WITHOUT ANY WARRANTY; without even the implied warranty of
18*38fd1498Szrj    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19*38fd1498Szrj    GNU General Public License for more details.
20*38fd1498Szrj 
21*38fd1498Szrj    You should have received a copy of the GNU General Public License
22*38fd1498Szrj    along with this program; if not, write to the Free Software
23*38fd1498Szrj    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
24*38fd1498Szrj    USA.  */
25*38fd1498Szrj 
26*38fd1498Szrj /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
27*38fd1498Szrj    Ditto for AIX 3.2 and <stdlib.h>.  */
28*38fd1498Szrj #ifndef _NO_PROTO
29*38fd1498Szrj # define _NO_PROTO
30*38fd1498Szrj #endif
31*38fd1498Szrj 
32*38fd1498Szrj #ifdef HAVE_CONFIG_H
33*38fd1498Szrj # include <config.h>
34*38fd1498Szrj #endif
35*38fd1498Szrj 
36*38fd1498Szrj #if !defined __STDC__ || !__STDC__
37*38fd1498Szrj /* This is a separate conditional since some stdc systems
38*38fd1498Szrj    reject `defined (const)'.  */
39*38fd1498Szrj # ifndef const
40*38fd1498Szrj #  define const
41*38fd1498Szrj # endif
42*38fd1498Szrj #endif
43*38fd1498Szrj 
44*38fd1498Szrj #include "ansidecl.h"
45*38fd1498Szrj #include <stdio.h>
46*38fd1498Szrj 
47*38fd1498Szrj /* Comment out all this code if we are using the GNU C Library, and are not
48*38fd1498Szrj    actually compiling the library itself.  This code is part of the GNU C
49*38fd1498Szrj    Library, but also included in many other GNU distributions.  Compiling
50*38fd1498Szrj    and linking in this code is a waste when using the GNU C library
51*38fd1498Szrj    (especially if it is a shared library).  Rather than having every GNU
52*38fd1498Szrj    program understand `configure --with-gnu-libc' and omit the object files,
53*38fd1498Szrj    it is simpler to just do this in the source for each such file.  */
54*38fd1498Szrj 
55*38fd1498Szrj #define GETOPT_INTERFACE_VERSION 2
56*38fd1498Szrj #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
57*38fd1498Szrj # include <gnu-versions.h>
58*38fd1498Szrj # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
59*38fd1498Szrj #  define ELIDE_CODE
60*38fd1498Szrj # endif
61*38fd1498Szrj #endif
62*38fd1498Szrj 
63*38fd1498Szrj #ifndef ELIDE_CODE
64*38fd1498Szrj 
65*38fd1498Szrj 
66*38fd1498Szrj /* This needs to come after some library #include
67*38fd1498Szrj    to get __GNU_LIBRARY__ defined.  */
68*38fd1498Szrj #ifdef	__GNU_LIBRARY__
69*38fd1498Szrj /* Don't include stdlib.h for non-GNU C libraries because some of them
70*38fd1498Szrj    contain conflicting prototypes for getopt.  */
71*38fd1498Szrj # include <stdlib.h>
72*38fd1498Szrj # include <unistd.h>
73*38fd1498Szrj #endif	/* GNU C library.  */
74*38fd1498Szrj 
75*38fd1498Szrj #ifdef VMS
76*38fd1498Szrj # include <unixlib.h>
77*38fd1498Szrj # if HAVE_STRING_H - 0
78*38fd1498Szrj #  include <string.h>
79*38fd1498Szrj # endif
80*38fd1498Szrj #endif
81*38fd1498Szrj 
82*38fd1498Szrj #ifndef _
83*38fd1498Szrj /* This is for other GNU distributions with internationalized messages.
84*38fd1498Szrj    When compiling libc, the _ macro is predefined.  */
85*38fd1498Szrj # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
86*38fd1498Szrj #  include <libintl.h>
87*38fd1498Szrj #  define _(msgid)	gettext (msgid)
88*38fd1498Szrj # else
89*38fd1498Szrj #  define _(msgid)	(msgid)
90*38fd1498Szrj # endif
91*38fd1498Szrj #endif
92*38fd1498Szrj 
93*38fd1498Szrj /* This version of `getopt' appears to the caller like standard Unix `getopt'
94*38fd1498Szrj    but it behaves differently for the user, since it allows the user
95*38fd1498Szrj    to intersperse the options with the other arguments.
96*38fd1498Szrj 
97*38fd1498Szrj    As `getopt' works, it permutes the elements of ARGV so that,
98*38fd1498Szrj    when it is done, all the options precede everything else.  Thus
99*38fd1498Szrj    all application programs are extended to handle flexible argument order.
100*38fd1498Szrj 
101*38fd1498Szrj    Setting the environment variable POSIXLY_CORRECT disables permutation.
102*38fd1498Szrj    Then the behavior is completely standard.
103*38fd1498Szrj 
104*38fd1498Szrj    GNU application programs can use a third alternative mode in which
105*38fd1498Szrj    they can distinguish the relative order of options and other arguments.  */
106*38fd1498Szrj 
107*38fd1498Szrj #include "getopt.h"
108*38fd1498Szrj 
109*38fd1498Szrj /* For communication from `getopt' to the caller.
110*38fd1498Szrj    When `getopt' finds an option that takes an argument,
111*38fd1498Szrj    the argument value is returned here.
112*38fd1498Szrj    Also, when `ordering' is RETURN_IN_ORDER,
113*38fd1498Szrj    each non-option ARGV-element is returned here.  */
114*38fd1498Szrj 
115*38fd1498Szrj char *optarg = NULL;
116*38fd1498Szrj 
117*38fd1498Szrj /* Index in ARGV of the next element to be scanned.
118*38fd1498Szrj    This is used for communication to and from the caller
119*38fd1498Szrj    and for communication between successive calls to `getopt'.
120*38fd1498Szrj 
121*38fd1498Szrj    On entry to `getopt', zero means this is the first call; initialize.
122*38fd1498Szrj 
123*38fd1498Szrj    When `getopt' returns -1, this is the index of the first of the
124*38fd1498Szrj    non-option elements that the caller should itself scan.
125*38fd1498Szrj 
126*38fd1498Szrj    Otherwise, `optind' communicates from one call to the next
127*38fd1498Szrj    how much of ARGV has been scanned so far.  */
128*38fd1498Szrj 
129*38fd1498Szrj /* 1003.2 says this must be 1 before any call.  */
130*38fd1498Szrj int optind = 1;
131*38fd1498Szrj 
132*38fd1498Szrj /* Formerly, initialization of getopt depended on optind==0, which
133*38fd1498Szrj    causes problems with re-calling getopt as programs generally don't
134*38fd1498Szrj    know that. */
135*38fd1498Szrj 
136*38fd1498Szrj int __getopt_initialized = 0;
137*38fd1498Szrj 
138*38fd1498Szrj /* The next char to be scanned in the option-element
139*38fd1498Szrj    in which the last option character we returned was found.
140*38fd1498Szrj    This allows us to pick up the scan where we left off.
141*38fd1498Szrj 
142*38fd1498Szrj    If this is zero, or a null string, it means resume the scan
143*38fd1498Szrj    by advancing to the next ARGV-element.  */
144*38fd1498Szrj 
145*38fd1498Szrj static char *nextchar;
146*38fd1498Szrj 
147*38fd1498Szrj /* Callers store zero here to inhibit the error message
148*38fd1498Szrj    for unrecognized options.  */
149*38fd1498Szrj 
150*38fd1498Szrj int opterr = 1;
151*38fd1498Szrj 
152*38fd1498Szrj /* Set to an option character which was unrecognized.
153*38fd1498Szrj    This must be initialized on some systems to avoid linking in the
154*38fd1498Szrj    system's own getopt implementation.  */
155*38fd1498Szrj 
156*38fd1498Szrj int optopt = '?';
157*38fd1498Szrj 
158*38fd1498Szrj /* Describe how to deal with options that follow non-option ARGV-elements.
159*38fd1498Szrj 
160*38fd1498Szrj    If the caller did not specify anything,
161*38fd1498Szrj    the default is REQUIRE_ORDER if the environment variable
162*38fd1498Szrj    POSIXLY_CORRECT is defined, PERMUTE otherwise.
163*38fd1498Szrj 
164*38fd1498Szrj    REQUIRE_ORDER means don't recognize them as options;
165*38fd1498Szrj    stop option processing when the first non-option is seen.
166*38fd1498Szrj    This is what Unix does.
167*38fd1498Szrj    This mode of operation is selected by either setting the environment
168*38fd1498Szrj    variable POSIXLY_CORRECT, or using `+' as the first character
169*38fd1498Szrj    of the list of option characters.
170*38fd1498Szrj 
171*38fd1498Szrj    PERMUTE is the default.  We permute the contents of ARGV as we scan,
172*38fd1498Szrj    so that eventually all the non-options are at the end.  This allows options
173*38fd1498Szrj    to be given in any order, even with programs that were not written to
174*38fd1498Szrj    expect this.
175*38fd1498Szrj 
176*38fd1498Szrj    RETURN_IN_ORDER is an option available to programs that were written
177*38fd1498Szrj    to expect options and other ARGV-elements in any order and that care about
178*38fd1498Szrj    the ordering of the two.  We describe each non-option ARGV-element
179*38fd1498Szrj    as if it were the argument of an option with character code 1.
180*38fd1498Szrj    Using `-' as the first character of the list of option characters
181*38fd1498Szrj    selects this mode of operation.
182*38fd1498Szrj 
183*38fd1498Szrj    The special argument `--' forces an end of option-scanning regardless
184*38fd1498Szrj    of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
185*38fd1498Szrj    `--' can cause `getopt' to return -1 with `optind' != ARGC.  */
186*38fd1498Szrj 
187*38fd1498Szrj static enum
188*38fd1498Szrj {
189*38fd1498Szrj   REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
190*38fd1498Szrj } ordering;
191*38fd1498Szrj 
192*38fd1498Szrj /* Value of POSIXLY_CORRECT environment variable.  */
193*38fd1498Szrj static char *posixly_correct;
194*38fd1498Szrj 
195*38fd1498Szrj #ifdef	__GNU_LIBRARY__
196*38fd1498Szrj /* We want to avoid inclusion of string.h with non-GNU libraries
197*38fd1498Szrj    because there are many ways it can cause trouble.
198*38fd1498Szrj    On some systems, it contains special magic macros that don't work
199*38fd1498Szrj    in GCC.  */
200*38fd1498Szrj # include <string.h>
201*38fd1498Szrj # define my_index	strchr
202*38fd1498Szrj #else
203*38fd1498Szrj 
204*38fd1498Szrj # if HAVE_STRING_H
205*38fd1498Szrj #  include <string.h>
206*38fd1498Szrj # else
207*38fd1498Szrj #  if HAVE_STRINGS_H
208*38fd1498Szrj #   include <strings.h>
209*38fd1498Szrj #  endif
210*38fd1498Szrj # endif
211*38fd1498Szrj 
212*38fd1498Szrj /* Avoid depending on library functions or files
213*38fd1498Szrj    whose names are inconsistent.  */
214*38fd1498Szrj 
215*38fd1498Szrj #if HAVE_STDLIB_H && HAVE_DECL_GETENV
216*38fd1498Szrj #  include <stdlib.h>
217*38fd1498Szrj #elif !defined(getenv)
218*38fd1498Szrj #  ifdef __cplusplus
219*38fd1498Szrj extern "C" {
220*38fd1498Szrj #  endif /* __cplusplus */
221*38fd1498Szrj extern char *getenv (const char *);
222*38fd1498Szrj #  ifdef __cplusplus
223*38fd1498Szrj }
224*38fd1498Szrj #  endif /* __cplusplus */
225*38fd1498Szrj #endif
226*38fd1498Szrj 
227*38fd1498Szrj static char *
my_index(const char * str,int chr)228*38fd1498Szrj my_index (const char *str, int chr)
229*38fd1498Szrj {
230*38fd1498Szrj   while (*str)
231*38fd1498Szrj     {
232*38fd1498Szrj       if (*str == chr)
233*38fd1498Szrj 	return (char *) str;
234*38fd1498Szrj       str++;
235*38fd1498Szrj     }
236*38fd1498Szrj   return 0;
237*38fd1498Szrj }
238*38fd1498Szrj 
239*38fd1498Szrj /* If using GCC, we can safely declare strlen this way.
240*38fd1498Szrj    If not using GCC, it is ok not to declare it.  */
241*38fd1498Szrj #ifdef __GNUC__
242*38fd1498Szrj /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
243*38fd1498Szrj    That was relevant to code that was here before.  */
244*38fd1498Szrj # if (!defined __STDC__ || !__STDC__) && !defined strlen
245*38fd1498Szrj /* gcc with -traditional declares the built-in strlen to return int,
246*38fd1498Szrj    and has done so at least since version 2.4.5. -- rms.  */
247*38fd1498Szrj extern int strlen (const char *);
248*38fd1498Szrj # endif /* not __STDC__ */
249*38fd1498Szrj #endif /* __GNUC__ */
250*38fd1498Szrj 
251*38fd1498Szrj #endif /* not __GNU_LIBRARY__ */
252*38fd1498Szrj 
253*38fd1498Szrj /* Handle permutation of arguments.  */
254*38fd1498Szrj 
255*38fd1498Szrj /* Describe the part of ARGV that contains non-options that have
256*38fd1498Szrj    been skipped.  `first_nonopt' is the index in ARGV of the first of them;
257*38fd1498Szrj    `last_nonopt' is the index after the last of them.  */
258*38fd1498Szrj 
259*38fd1498Szrj static int first_nonopt;
260*38fd1498Szrj static int last_nonopt;
261*38fd1498Szrj 
262*38fd1498Szrj #ifdef _LIBC
263*38fd1498Szrj /* Bash 2.0 gives us an environment variable containing flags
264*38fd1498Szrj    indicating ARGV elements that should not be considered arguments.  */
265*38fd1498Szrj 
266*38fd1498Szrj /* Defined in getopt_init.c  */
267*38fd1498Szrj extern char *__getopt_nonoption_flags;
268*38fd1498Szrj 
269*38fd1498Szrj static int nonoption_flags_max_len;
270*38fd1498Szrj static int nonoption_flags_len;
271*38fd1498Szrj 
272*38fd1498Szrj static int original_argc;
273*38fd1498Szrj static char *const *original_argv;
274*38fd1498Szrj 
275*38fd1498Szrj /* Make sure the environment variable bash 2.0 puts in the environment
276*38fd1498Szrj    is valid for the getopt call we must make sure that the ARGV passed
277*38fd1498Szrj    to getopt is that one passed to the process.  */
278*38fd1498Szrj static void
279*38fd1498Szrj __attribute__ ((unused))
store_args_and_env(int argc,char * const * argv)280*38fd1498Szrj store_args_and_env (int argc, char *const *argv)
281*38fd1498Szrj {
282*38fd1498Szrj   /* XXX This is no good solution.  We should rather copy the args so
283*38fd1498Szrj      that we can compare them later.  But we must not use malloc(3).  */
284*38fd1498Szrj   original_argc = argc;
285*38fd1498Szrj   original_argv = argv;
286*38fd1498Szrj }
287*38fd1498Szrj # ifdef text_set_element
288*38fd1498Szrj text_set_element (__libc_subinit, store_args_and_env);
289*38fd1498Szrj # endif /* text_set_element */
290*38fd1498Szrj 
291*38fd1498Szrj # define SWAP_FLAGS(ch1, ch2) \
292*38fd1498Szrj   if (nonoption_flags_len > 0)						      \
293*38fd1498Szrj     {									      \
294*38fd1498Szrj       char __tmp = __getopt_nonoption_flags[ch1];			      \
295*38fd1498Szrj       __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];	      \
296*38fd1498Szrj       __getopt_nonoption_flags[ch2] = __tmp;				      \
297*38fd1498Szrj     }
298*38fd1498Szrj #else	/* !_LIBC */
299*38fd1498Szrj # define SWAP_FLAGS(ch1, ch2)
300*38fd1498Szrj #endif	/* _LIBC */
301*38fd1498Szrj 
302*38fd1498Szrj /* Exchange two adjacent subsequences of ARGV.
303*38fd1498Szrj    One subsequence is elements [first_nonopt,last_nonopt)
304*38fd1498Szrj    which contains all the non-options that have been skipped so far.
305*38fd1498Szrj    The other is elements [last_nonopt,optind), which contains all
306*38fd1498Szrj    the options processed since those non-options were skipped.
307*38fd1498Szrj 
308*38fd1498Szrj    `first_nonopt' and `last_nonopt' are relocated so that they describe
309*38fd1498Szrj    the new indices of the non-options in ARGV after they are moved.  */
310*38fd1498Szrj 
311*38fd1498Szrj #if defined __STDC__ && __STDC__
312*38fd1498Szrj static void exchange (char **);
313*38fd1498Szrj #endif
314*38fd1498Szrj 
315*38fd1498Szrj static void
exchange(char ** argv)316*38fd1498Szrj exchange (char **argv)
317*38fd1498Szrj {
318*38fd1498Szrj   int bottom = first_nonopt;
319*38fd1498Szrj   int middle = last_nonopt;
320*38fd1498Szrj   int top = optind;
321*38fd1498Szrj   char *tem;
322*38fd1498Szrj 
323*38fd1498Szrj   /* Exchange the shorter segment with the far end of the longer segment.
324*38fd1498Szrj      That puts the shorter segment into the right place.
325*38fd1498Szrj      It leaves the longer segment in the right place overall,
326*38fd1498Szrj      but it consists of two parts that need to be swapped next.  */
327*38fd1498Szrj 
328*38fd1498Szrj #ifdef _LIBC
329*38fd1498Szrj   /* First make sure the handling of the `__getopt_nonoption_flags'
330*38fd1498Szrj      string can work normally.  Our top argument must be in the range
331*38fd1498Szrj      of the string.  */
332*38fd1498Szrj   if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
333*38fd1498Szrj     {
334*38fd1498Szrj       /* We must extend the array.  The user plays games with us and
335*38fd1498Szrj 	 presents new arguments.  */
336*38fd1498Szrj       char *new_str = (char *) malloc (top + 1);
337*38fd1498Szrj       if (new_str == NULL)
338*38fd1498Szrj 	nonoption_flags_len = nonoption_flags_max_len = 0;
339*38fd1498Szrj       else
340*38fd1498Szrj 	{
341*38fd1498Szrj 	  memset (mempcpy (new_str, __getopt_nonoption_flags,
342*38fd1498Szrj 			   nonoption_flags_max_len),
343*38fd1498Szrj 		  '\0', top + 1 - nonoption_flags_max_len);
344*38fd1498Szrj 	  nonoption_flags_max_len = top + 1;
345*38fd1498Szrj 	  __getopt_nonoption_flags = new_str;
346*38fd1498Szrj 	}
347*38fd1498Szrj     }
348*38fd1498Szrj #endif
349*38fd1498Szrj 
350*38fd1498Szrj   while (top > middle && middle > bottom)
351*38fd1498Szrj     {
352*38fd1498Szrj       if (top - middle > middle - bottom)
353*38fd1498Szrj 	{
354*38fd1498Szrj 	  /* Bottom segment is the short one.  */
355*38fd1498Szrj 	  int len = middle - bottom;
356*38fd1498Szrj 	  register int i;
357*38fd1498Szrj 
358*38fd1498Szrj 	  /* Swap it with the top part of the top segment.  */
359*38fd1498Szrj 	  for (i = 0; i < len; i++)
360*38fd1498Szrj 	    {
361*38fd1498Szrj 	      tem = argv[bottom + i];
362*38fd1498Szrj 	      argv[bottom + i] = argv[top - (middle - bottom) + i];
363*38fd1498Szrj 	      argv[top - (middle - bottom) + i] = tem;
364*38fd1498Szrj 	      SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
365*38fd1498Szrj 	    }
366*38fd1498Szrj 	  /* Exclude the moved bottom segment from further swapping.  */
367*38fd1498Szrj 	  top -= len;
368*38fd1498Szrj 	}
369*38fd1498Szrj       else
370*38fd1498Szrj 	{
371*38fd1498Szrj 	  /* Top segment is the short one.  */
372*38fd1498Szrj 	  int len = top - middle;
373*38fd1498Szrj 	  register int i;
374*38fd1498Szrj 
375*38fd1498Szrj 	  /* Swap it with the bottom part of the bottom segment.  */
376*38fd1498Szrj 	  for (i = 0; i < len; i++)
377*38fd1498Szrj 	    {
378*38fd1498Szrj 	      tem = argv[bottom + i];
379*38fd1498Szrj 	      argv[bottom + i] = argv[middle + i];
380*38fd1498Szrj 	      argv[middle + i] = tem;
381*38fd1498Szrj 	      SWAP_FLAGS (bottom + i, middle + i);
382*38fd1498Szrj 	    }
383*38fd1498Szrj 	  /* Exclude the moved top segment from further swapping.  */
384*38fd1498Szrj 	  bottom += len;
385*38fd1498Szrj 	}
386*38fd1498Szrj     }
387*38fd1498Szrj 
388*38fd1498Szrj   /* Update records for the slots the non-options now occupy.  */
389*38fd1498Szrj 
390*38fd1498Szrj   first_nonopt += (optind - last_nonopt);
391*38fd1498Szrj   last_nonopt = optind;
392*38fd1498Szrj }
393*38fd1498Szrj 
394*38fd1498Szrj /* Initialize the internal data when the first call is made.  */
395*38fd1498Szrj 
396*38fd1498Szrj #if defined __STDC__ && __STDC__
397*38fd1498Szrj static const char *_getopt_initialize (int, char *const *, const char *);
398*38fd1498Szrj #endif
399*38fd1498Szrj static const char *
_getopt_initialize(int argc ATTRIBUTE_UNUSED,char * const * argv ATTRIBUTE_UNUSED,const char * optstring)400*38fd1498Szrj _getopt_initialize (int argc ATTRIBUTE_UNUSED,
401*38fd1498Szrj 		    char *const *argv ATTRIBUTE_UNUSED,
402*38fd1498Szrj 		    const char *optstring)
403*38fd1498Szrj {
404*38fd1498Szrj   /* Start processing options with ARGV-element 1 (since ARGV-element 0
405*38fd1498Szrj      is the program name); the sequence of previously skipped
406*38fd1498Szrj      non-option ARGV-elements is empty.  */
407*38fd1498Szrj 
408*38fd1498Szrj   first_nonopt = last_nonopt = optind;
409*38fd1498Szrj 
410*38fd1498Szrj   nextchar = NULL;
411*38fd1498Szrj 
412*38fd1498Szrj   posixly_correct = getenv ("POSIXLY_CORRECT");
413*38fd1498Szrj 
414*38fd1498Szrj   /* Determine how to handle the ordering of options and nonoptions.  */
415*38fd1498Szrj 
416*38fd1498Szrj   if (optstring[0] == '-')
417*38fd1498Szrj     {
418*38fd1498Szrj       ordering = RETURN_IN_ORDER;
419*38fd1498Szrj       ++optstring;
420*38fd1498Szrj     }
421*38fd1498Szrj   else if (optstring[0] == '+')
422*38fd1498Szrj     {
423*38fd1498Szrj       ordering = REQUIRE_ORDER;
424*38fd1498Szrj       ++optstring;
425*38fd1498Szrj     }
426*38fd1498Szrj   else if (posixly_correct != NULL)
427*38fd1498Szrj     ordering = REQUIRE_ORDER;
428*38fd1498Szrj   else
429*38fd1498Szrj     ordering = PERMUTE;
430*38fd1498Szrj 
431*38fd1498Szrj #ifdef _LIBC
432*38fd1498Szrj   if (posixly_correct == NULL
433*38fd1498Szrj       && argc == original_argc && argv == original_argv)
434*38fd1498Szrj     {
435*38fd1498Szrj       if (nonoption_flags_max_len == 0)
436*38fd1498Szrj 	{
437*38fd1498Szrj 	  if (__getopt_nonoption_flags == NULL
438*38fd1498Szrj 	      || __getopt_nonoption_flags[0] == '\0')
439*38fd1498Szrj 	    nonoption_flags_max_len = -1;
440*38fd1498Szrj 	  else
441*38fd1498Szrj 	    {
442*38fd1498Szrj 	      const char *orig_str = __getopt_nonoption_flags;
443*38fd1498Szrj 	      int len = nonoption_flags_max_len = strlen (orig_str);
444*38fd1498Szrj 	      if (nonoption_flags_max_len < argc)
445*38fd1498Szrj 		nonoption_flags_max_len = argc;
446*38fd1498Szrj 	      __getopt_nonoption_flags =
447*38fd1498Szrj 		(char *) malloc (nonoption_flags_max_len);
448*38fd1498Szrj 	      if (__getopt_nonoption_flags == NULL)
449*38fd1498Szrj 		nonoption_flags_max_len = -1;
450*38fd1498Szrj 	      else
451*38fd1498Szrj 		memset (mempcpy (__getopt_nonoption_flags, orig_str, len),
452*38fd1498Szrj 			'\0', nonoption_flags_max_len - len);
453*38fd1498Szrj 	    }
454*38fd1498Szrj 	}
455*38fd1498Szrj       nonoption_flags_len = nonoption_flags_max_len;
456*38fd1498Szrj     }
457*38fd1498Szrj   else
458*38fd1498Szrj     nonoption_flags_len = 0;
459*38fd1498Szrj #endif
460*38fd1498Szrj 
461*38fd1498Szrj   return optstring;
462*38fd1498Szrj }
463*38fd1498Szrj 
464*38fd1498Szrj /* Scan elements of ARGV (whose length is ARGC) for option characters
465*38fd1498Szrj    given in OPTSTRING.
466*38fd1498Szrj 
467*38fd1498Szrj    If an element of ARGV starts with '-', and is not exactly "-" or "--",
468*38fd1498Szrj    then it is an option element.  The characters of this element
469*38fd1498Szrj    (aside from the initial '-') are option characters.  If `getopt'
470*38fd1498Szrj    is called repeatedly, it returns successively each of the option characters
471*38fd1498Szrj    from each of the option elements.
472*38fd1498Szrj 
473*38fd1498Szrj    If `getopt' finds another option character, it returns that character,
474*38fd1498Szrj    updating `optind' and `nextchar' so that the next call to `getopt' can
475*38fd1498Szrj    resume the scan with the following option character or ARGV-element.
476*38fd1498Szrj 
477*38fd1498Szrj    If there are no more option characters, `getopt' returns -1.
478*38fd1498Szrj    Then `optind' is the index in ARGV of the first ARGV-element
479*38fd1498Szrj    that is not an option.  (The ARGV-elements have been permuted
480*38fd1498Szrj    so that those that are not options now come last.)
481*38fd1498Szrj 
482*38fd1498Szrj    OPTSTRING is a string containing the legitimate option characters.
483*38fd1498Szrj    If an option character is seen that is not listed in OPTSTRING,
484*38fd1498Szrj    return '?' after printing an error message.  If you set `opterr' to
485*38fd1498Szrj    zero, the error message is suppressed but we still return '?'.
486*38fd1498Szrj 
487*38fd1498Szrj    If a char in OPTSTRING is followed by a colon, that means it wants an arg,
488*38fd1498Szrj    so the following text in the same ARGV-element, or the text of the following
489*38fd1498Szrj    ARGV-element, is returned in `optarg'.  Two colons mean an option that
490*38fd1498Szrj    wants an optional arg; if there is text in the current ARGV-element,
491*38fd1498Szrj    it is returned in `optarg', otherwise `optarg' is set to zero.
492*38fd1498Szrj 
493*38fd1498Szrj    If OPTSTRING starts with `-' or `+', it requests different methods of
494*38fd1498Szrj    handling the non-option ARGV-elements.
495*38fd1498Szrj    See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
496*38fd1498Szrj 
497*38fd1498Szrj    Long-named options begin with `--' instead of `-'.
498*38fd1498Szrj    Their names may be abbreviated as long as the abbreviation is unique
499*38fd1498Szrj    or is an exact match for some defined option.  If they have an
500*38fd1498Szrj    argument, it follows the option name in the same ARGV-element, separated
501*38fd1498Szrj    from the option name by a `=', or else the in next ARGV-element.
502*38fd1498Szrj    When `getopt' finds a long-named option, it returns 0 if that option's
503*38fd1498Szrj    `flag' field is nonzero, the value of the option's `val' field
504*38fd1498Szrj    if the `flag' field is zero.
505*38fd1498Szrj 
506*38fd1498Szrj    The elements of ARGV aren't really const, because we permute them.
507*38fd1498Szrj    But we pretend they're const in the prototype to be compatible
508*38fd1498Szrj    with other systems.
509*38fd1498Szrj 
510*38fd1498Szrj    LONGOPTS is a vector of `struct option' terminated by an
511*38fd1498Szrj    element containing a name which is zero.
512*38fd1498Szrj 
513*38fd1498Szrj    LONGIND returns the index in LONGOPT of the long-named option found.
514*38fd1498Szrj    It is only valid when a long-named option has been found by the most
515*38fd1498Szrj    recent call.
516*38fd1498Szrj 
517*38fd1498Szrj    If LONG_ONLY is nonzero, '-' as well as '--' can introduce
518*38fd1498Szrj    long-named options.  */
519*38fd1498Szrj 
520*38fd1498Szrj int
_getopt_internal(int argc,char * const * argv,const char * optstring,const struct option * longopts,int * longind,int long_only)521*38fd1498Szrj _getopt_internal (int argc, char *const *argv, const char *optstring,
522*38fd1498Szrj                   const struct option *longopts,
523*38fd1498Szrj                   int *longind, int long_only)
524*38fd1498Szrj {
525*38fd1498Szrj   optarg = NULL;
526*38fd1498Szrj 
527*38fd1498Szrj   if (optind == 0 || !__getopt_initialized)
528*38fd1498Szrj     {
529*38fd1498Szrj       if (optind == 0)
530*38fd1498Szrj 	optind = 1;	/* Don't scan ARGV[0], the program name.  */
531*38fd1498Szrj       optstring = _getopt_initialize (argc, argv, optstring);
532*38fd1498Szrj       __getopt_initialized = 1;
533*38fd1498Szrj     }
534*38fd1498Szrj 
535*38fd1498Szrj   /* Test whether ARGV[optind] points to a non-option argument.
536*38fd1498Szrj      Either it does not have option syntax, or there is an environment flag
537*38fd1498Szrj      from the shell indicating it is not an option.  The later information
538*38fd1498Szrj      is only used when the used in the GNU libc.  */
539*38fd1498Szrj #ifdef _LIBC
540*38fd1498Szrj # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'	      \
541*38fd1498Szrj 		      || (optind < nonoption_flags_len			      \
542*38fd1498Szrj 			  && __getopt_nonoption_flags[optind] == '1'))
543*38fd1498Szrj #else
544*38fd1498Szrj # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
545*38fd1498Szrj #endif
546*38fd1498Szrj 
547*38fd1498Szrj   if (nextchar == NULL || *nextchar == '\0')
548*38fd1498Szrj     {
549*38fd1498Szrj       /* Advance to the next ARGV-element.  */
550*38fd1498Szrj 
551*38fd1498Szrj       /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
552*38fd1498Szrj 	 moved back by the user (who may also have changed the arguments).  */
553*38fd1498Szrj       if (last_nonopt > optind)
554*38fd1498Szrj 	last_nonopt = optind;
555*38fd1498Szrj       if (first_nonopt > optind)
556*38fd1498Szrj 	first_nonopt = optind;
557*38fd1498Szrj 
558*38fd1498Szrj       if (ordering == PERMUTE)
559*38fd1498Szrj 	{
560*38fd1498Szrj 	  /* If we have just processed some options following some non-options,
561*38fd1498Szrj 	     exchange them so that the options come first.  */
562*38fd1498Szrj 
563*38fd1498Szrj 	  if (first_nonopt != last_nonopt && last_nonopt != optind)
564*38fd1498Szrj 	    exchange ((char **) argv);
565*38fd1498Szrj 	  else if (last_nonopt != optind)
566*38fd1498Szrj 	    first_nonopt = optind;
567*38fd1498Szrj 
568*38fd1498Szrj 	  /* Skip any additional non-options
569*38fd1498Szrj 	     and extend the range of non-options previously skipped.  */
570*38fd1498Szrj 
571*38fd1498Szrj 	  while (optind < argc && NONOPTION_P)
572*38fd1498Szrj 	    optind++;
573*38fd1498Szrj 	  last_nonopt = optind;
574*38fd1498Szrj 	}
575*38fd1498Szrj 
576*38fd1498Szrj       /* The special ARGV-element `--' means premature end of options.
577*38fd1498Szrj 	 Skip it like a null option,
578*38fd1498Szrj 	 then exchange with previous non-options as if it were an option,
579*38fd1498Szrj 	 then skip everything else like a non-option.  */
580*38fd1498Szrj 
581*38fd1498Szrj       if (optind != argc && !strcmp (argv[optind], "--"))
582*38fd1498Szrj 	{
583*38fd1498Szrj 	  optind++;
584*38fd1498Szrj 
585*38fd1498Szrj 	  if (first_nonopt != last_nonopt && last_nonopt != optind)
586*38fd1498Szrj 	    exchange ((char **) argv);
587*38fd1498Szrj 	  else if (first_nonopt == last_nonopt)
588*38fd1498Szrj 	    first_nonopt = optind;
589*38fd1498Szrj 	  last_nonopt = argc;
590*38fd1498Szrj 
591*38fd1498Szrj 	  optind = argc;
592*38fd1498Szrj 	}
593*38fd1498Szrj 
594*38fd1498Szrj       /* If we have done all the ARGV-elements, stop the scan
595*38fd1498Szrj 	 and back over any non-options that we skipped and permuted.  */
596*38fd1498Szrj 
597*38fd1498Szrj       if (optind == argc)
598*38fd1498Szrj 	{
599*38fd1498Szrj 	  /* Set the next-arg-index to point at the non-options
600*38fd1498Szrj 	     that we previously skipped, so the caller will digest them.  */
601*38fd1498Szrj 	  if (first_nonopt != last_nonopt)
602*38fd1498Szrj 	    optind = first_nonopt;
603*38fd1498Szrj 	  return -1;
604*38fd1498Szrj 	}
605*38fd1498Szrj 
606*38fd1498Szrj       /* If we have come to a non-option and did not permute it,
607*38fd1498Szrj 	 either stop the scan or describe it to the caller and pass it by.  */
608*38fd1498Szrj 
609*38fd1498Szrj       if (NONOPTION_P)
610*38fd1498Szrj 	{
611*38fd1498Szrj 	  if (ordering == REQUIRE_ORDER)
612*38fd1498Szrj 	    return -1;
613*38fd1498Szrj 	  optarg = argv[optind++];
614*38fd1498Szrj 	  return 1;
615*38fd1498Szrj 	}
616*38fd1498Szrj 
617*38fd1498Szrj       /* We have found another option-ARGV-element.
618*38fd1498Szrj 	 Skip the initial punctuation.  */
619*38fd1498Szrj 
620*38fd1498Szrj       nextchar = (argv[optind] + 1
621*38fd1498Szrj 		  + (longopts != NULL && argv[optind][1] == '-'));
622*38fd1498Szrj     }
623*38fd1498Szrj 
624*38fd1498Szrj   /* Decode the current option-ARGV-element.  */
625*38fd1498Szrj 
626*38fd1498Szrj   /* Check whether the ARGV-element is a long option.
627*38fd1498Szrj 
628*38fd1498Szrj      If long_only and the ARGV-element has the form "-f", where f is
629*38fd1498Szrj      a valid short option, don't consider it an abbreviated form of
630*38fd1498Szrj      a long option that starts with f.  Otherwise there would be no
631*38fd1498Szrj      way to give the -f short option.
632*38fd1498Szrj 
633*38fd1498Szrj      On the other hand, if there's a long option "fubar" and
634*38fd1498Szrj      the ARGV-element is "-fu", do consider that an abbreviation of
635*38fd1498Szrj      the long option, just like "--fu", and not "-f" with arg "u".
636*38fd1498Szrj 
637*38fd1498Szrj      This distinction seems to be the most useful approach.  */
638*38fd1498Szrj 
639*38fd1498Szrj   if (longopts != NULL
640*38fd1498Szrj       && (argv[optind][1] == '-'
641*38fd1498Szrj 	  || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
642*38fd1498Szrj     {
643*38fd1498Szrj       char *nameend;
644*38fd1498Szrj       const struct option *p;
645*38fd1498Szrj       const struct option *pfound = NULL;
646*38fd1498Szrj       int exact = 0;
647*38fd1498Szrj       int ambig = 0;
648*38fd1498Szrj       int indfound = -1;
649*38fd1498Szrj       int option_index;
650*38fd1498Szrj 
651*38fd1498Szrj       for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
652*38fd1498Szrj 	/* Do nothing.  */ ;
653*38fd1498Szrj 
654*38fd1498Szrj       /* Test all long options for either exact match
655*38fd1498Szrj 	 or abbreviated matches.  */
656*38fd1498Szrj       for (p = longopts, option_index = 0; p->name; p++, option_index++)
657*38fd1498Szrj 	if (!strncmp (p->name, nextchar, nameend - nextchar))
658*38fd1498Szrj 	  {
659*38fd1498Szrj 	    if ((unsigned int) (nameend - nextchar)
660*38fd1498Szrj 		== (unsigned int) strlen (p->name))
661*38fd1498Szrj 	      {
662*38fd1498Szrj 		/* Exact match found.  */
663*38fd1498Szrj 		pfound = p;
664*38fd1498Szrj 		indfound = option_index;
665*38fd1498Szrj 		exact = 1;
666*38fd1498Szrj 		break;
667*38fd1498Szrj 	      }
668*38fd1498Szrj 	    else if (pfound == NULL)
669*38fd1498Szrj 	      {
670*38fd1498Szrj 		/* First nonexact match found.  */
671*38fd1498Szrj 		pfound = p;
672*38fd1498Szrj 		indfound = option_index;
673*38fd1498Szrj 	      }
674*38fd1498Szrj 	    else
675*38fd1498Szrj 	      /* Second or later nonexact match found.  */
676*38fd1498Szrj 	      ambig = 1;
677*38fd1498Szrj 	  }
678*38fd1498Szrj 
679*38fd1498Szrj       if (ambig && !exact)
680*38fd1498Szrj 	{
681*38fd1498Szrj 	  if (opterr)
682*38fd1498Szrj 	    fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
683*38fd1498Szrj 		     argv[0], argv[optind]);
684*38fd1498Szrj 	  nextchar += strlen (nextchar);
685*38fd1498Szrj 	  optind++;
686*38fd1498Szrj 	  optopt = 0;
687*38fd1498Szrj 	  return '?';
688*38fd1498Szrj 	}
689*38fd1498Szrj 
690*38fd1498Szrj       if (pfound != NULL)
691*38fd1498Szrj 	{
692*38fd1498Szrj 	  option_index = indfound;
693*38fd1498Szrj 	  optind++;
694*38fd1498Szrj 	  if (*nameend)
695*38fd1498Szrj 	    {
696*38fd1498Szrj 	      /* Don't test has_arg with >, because some C compilers don't
697*38fd1498Szrj 		 allow it to be used on enums.  */
698*38fd1498Szrj 	      if (pfound->has_arg)
699*38fd1498Szrj 		optarg = nameend + 1;
700*38fd1498Szrj 	      else
701*38fd1498Szrj 		{
702*38fd1498Szrj 		  if (opterr)
703*38fd1498Szrj 		    {
704*38fd1498Szrj 		      if (argv[optind - 1][1] == '-')
705*38fd1498Szrj 			/* --option */
706*38fd1498Szrj 			fprintf (stderr,
707*38fd1498Szrj 				 _("%s: option `--%s' doesn't allow an argument\n"),
708*38fd1498Szrj 				 argv[0], pfound->name);
709*38fd1498Szrj 		      else
710*38fd1498Szrj 			/* +option or -option */
711*38fd1498Szrj 			fprintf (stderr,
712*38fd1498Szrj 				 _("%s: option `%c%s' doesn't allow an argument\n"),
713*38fd1498Szrj 				 argv[0], argv[optind - 1][0], pfound->name);
714*38fd1498Szrj 
715*38fd1498Szrj 		      nextchar += strlen (nextchar);
716*38fd1498Szrj 
717*38fd1498Szrj 		      optopt = pfound->val;
718*38fd1498Szrj 		      return '?';
719*38fd1498Szrj 		    }
720*38fd1498Szrj 		}
721*38fd1498Szrj 	    }
722*38fd1498Szrj 	  else if (pfound->has_arg == 1)
723*38fd1498Szrj 	    {
724*38fd1498Szrj 	      if (optind < argc)
725*38fd1498Szrj 		optarg = argv[optind++];
726*38fd1498Szrj 	      else
727*38fd1498Szrj 		{
728*38fd1498Szrj 		  if (opterr)
729*38fd1498Szrj 		    fprintf (stderr,
730*38fd1498Szrj 			   _("%s: option `%s' requires an argument\n"),
731*38fd1498Szrj 			   argv[0], argv[optind - 1]);
732*38fd1498Szrj 		  nextchar += strlen (nextchar);
733*38fd1498Szrj 		  optopt = pfound->val;
734*38fd1498Szrj 		  return optstring[0] == ':' ? ':' : '?';
735*38fd1498Szrj 		}
736*38fd1498Szrj 	    }
737*38fd1498Szrj 	  nextchar += strlen (nextchar);
738*38fd1498Szrj 	  if (longind != NULL)
739*38fd1498Szrj 	    *longind = option_index;
740*38fd1498Szrj 	  if (pfound->flag)
741*38fd1498Szrj 	    {
742*38fd1498Szrj 	      *(pfound->flag) = pfound->val;
743*38fd1498Szrj 	      return 0;
744*38fd1498Szrj 	    }
745*38fd1498Szrj 	  return pfound->val;
746*38fd1498Szrj 	}
747*38fd1498Szrj 
748*38fd1498Szrj       /* Can't find it as a long option.  If this is not getopt_long_only,
749*38fd1498Szrj 	 or the option starts with '--' or is not a valid short
750*38fd1498Szrj 	 option, then it's an error.
751*38fd1498Szrj 	 Otherwise interpret it as a short option.  */
752*38fd1498Szrj       if (!long_only || argv[optind][1] == '-'
753*38fd1498Szrj 	  || my_index (optstring, *nextchar) == NULL)
754*38fd1498Szrj 	{
755*38fd1498Szrj 	  if (opterr)
756*38fd1498Szrj 	    {
757*38fd1498Szrj 	      if (argv[optind][1] == '-')
758*38fd1498Szrj 		/* --option */
759*38fd1498Szrj 		fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
760*38fd1498Szrj 			 argv[0], nextchar);
761*38fd1498Szrj 	      else
762*38fd1498Szrj 		/* +option or -option */
763*38fd1498Szrj 		fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
764*38fd1498Szrj 			 argv[0], argv[optind][0], nextchar);
765*38fd1498Szrj 	    }
766*38fd1498Szrj 	  nextchar = (char *) "";
767*38fd1498Szrj 	  optind++;
768*38fd1498Szrj 	  optopt = 0;
769*38fd1498Szrj 	  return '?';
770*38fd1498Szrj 	}
771*38fd1498Szrj     }
772*38fd1498Szrj 
773*38fd1498Szrj   /* Look at and handle the next short option-character.  */
774*38fd1498Szrj 
775*38fd1498Szrj   {
776*38fd1498Szrj     char c = *nextchar++;
777*38fd1498Szrj     char *temp = my_index (optstring, c);
778*38fd1498Szrj 
779*38fd1498Szrj     /* Increment `optind' when we start to process its last character.  */
780*38fd1498Szrj     if (*nextchar == '\0')
781*38fd1498Szrj       ++optind;
782*38fd1498Szrj 
783*38fd1498Szrj     if (temp == NULL || c == ':')
784*38fd1498Szrj       {
785*38fd1498Szrj 	if (opterr)
786*38fd1498Szrj 	  {
787*38fd1498Szrj 	    if (posixly_correct)
788*38fd1498Szrj 	      /* 1003.2 specifies the format of this message.  */
789*38fd1498Szrj 	      fprintf (stderr, _("%s: illegal option -- %c\n"),
790*38fd1498Szrj 		       argv[0], c);
791*38fd1498Szrj 	    else
792*38fd1498Szrj 	      fprintf (stderr, _("%s: invalid option -- %c\n"),
793*38fd1498Szrj 		       argv[0], c);
794*38fd1498Szrj 	  }
795*38fd1498Szrj 	optopt = c;
796*38fd1498Szrj 	return '?';
797*38fd1498Szrj       }
798*38fd1498Szrj     /* Convenience. Treat POSIX -W foo same as long option --foo */
799*38fd1498Szrj     if (temp[0] == 'W' && temp[1] == ';')
800*38fd1498Szrj       {
801*38fd1498Szrj 	char *nameend;
802*38fd1498Szrj 	const struct option *p;
803*38fd1498Szrj 	const struct option *pfound = NULL;
804*38fd1498Szrj 	int exact = 0;
805*38fd1498Szrj 	int ambig = 0;
806*38fd1498Szrj 	int indfound = 0;
807*38fd1498Szrj 	int option_index;
808*38fd1498Szrj 
809*38fd1498Szrj 	/* This is an option that requires an argument.  */
810*38fd1498Szrj 	if (*nextchar != '\0')
811*38fd1498Szrj 	  {
812*38fd1498Szrj 	    optarg = nextchar;
813*38fd1498Szrj 	    /* If we end this ARGV-element by taking the rest as an arg,
814*38fd1498Szrj 	       we must advance to the next element now.  */
815*38fd1498Szrj 	    optind++;
816*38fd1498Szrj 	  }
817*38fd1498Szrj 	else if (optind == argc)
818*38fd1498Szrj 	  {
819*38fd1498Szrj 	    if (opterr)
820*38fd1498Szrj 	      {
821*38fd1498Szrj 		/* 1003.2 specifies the format of this message.  */
822*38fd1498Szrj 		fprintf (stderr, _("%s: option requires an argument -- %c\n"),
823*38fd1498Szrj 			 argv[0], c);
824*38fd1498Szrj 	      }
825*38fd1498Szrj 	    optopt = c;
826*38fd1498Szrj 	    if (optstring[0] == ':')
827*38fd1498Szrj 	      c = ':';
828*38fd1498Szrj 	    else
829*38fd1498Szrj 	      c = '?';
830*38fd1498Szrj 	    return c;
831*38fd1498Szrj 	  }
832*38fd1498Szrj 	else
833*38fd1498Szrj 	  /* We already incremented `optind' once;
834*38fd1498Szrj 	     increment it again when taking next ARGV-elt as argument.  */
835*38fd1498Szrj 	  optarg = argv[optind++];
836*38fd1498Szrj 
837*38fd1498Szrj 	/* optarg is now the argument, see if it's in the
838*38fd1498Szrj 	   table of longopts.  */
839*38fd1498Szrj 
840*38fd1498Szrj 	for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
841*38fd1498Szrj 	  /* Do nothing.  */ ;
842*38fd1498Szrj 
843*38fd1498Szrj 	/* Test all long options for either exact match
844*38fd1498Szrj 	   or abbreviated matches.  */
845*38fd1498Szrj 	for (p = longopts, option_index = 0; p->name; p++, option_index++)
846*38fd1498Szrj 	  if (!strncmp (p->name, nextchar, nameend - nextchar))
847*38fd1498Szrj 	    {
848*38fd1498Szrj 	      if ((unsigned int) (nameend - nextchar) == strlen (p->name))
849*38fd1498Szrj 		{
850*38fd1498Szrj 		  /* Exact match found.  */
851*38fd1498Szrj 		  pfound = p;
852*38fd1498Szrj 		  indfound = option_index;
853*38fd1498Szrj 		  exact = 1;
854*38fd1498Szrj 		  break;
855*38fd1498Szrj 		}
856*38fd1498Szrj 	      else if (pfound == NULL)
857*38fd1498Szrj 		{
858*38fd1498Szrj 		  /* First nonexact match found.  */
859*38fd1498Szrj 		  pfound = p;
860*38fd1498Szrj 		  indfound = option_index;
861*38fd1498Szrj 		}
862*38fd1498Szrj 	      else
863*38fd1498Szrj 		/* Second or later nonexact match found.  */
864*38fd1498Szrj 		ambig = 1;
865*38fd1498Szrj 	    }
866*38fd1498Szrj 	if (ambig && !exact)
867*38fd1498Szrj 	  {
868*38fd1498Szrj 	    if (opterr)
869*38fd1498Szrj 	      fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
870*38fd1498Szrj 		       argv[0], argv[optind]);
871*38fd1498Szrj 	    nextchar += strlen (nextchar);
872*38fd1498Szrj 	    optind++;
873*38fd1498Szrj 	    return '?';
874*38fd1498Szrj 	  }
875*38fd1498Szrj 	if (pfound != NULL)
876*38fd1498Szrj 	  {
877*38fd1498Szrj 	    option_index = indfound;
878*38fd1498Szrj 	    if (*nameend)
879*38fd1498Szrj 	      {
880*38fd1498Szrj 		/* Don't test has_arg with >, because some C compilers don't
881*38fd1498Szrj 		   allow it to be used on enums.  */
882*38fd1498Szrj 		if (pfound->has_arg)
883*38fd1498Szrj 		  optarg = nameend + 1;
884*38fd1498Szrj 		else
885*38fd1498Szrj 		  {
886*38fd1498Szrj 		    if (opterr)
887*38fd1498Szrj 		      fprintf (stderr, _("\
888*38fd1498Szrj %s: option `-W %s' doesn't allow an argument\n"),
889*38fd1498Szrj 			       argv[0], pfound->name);
890*38fd1498Szrj 
891*38fd1498Szrj 		    nextchar += strlen (nextchar);
892*38fd1498Szrj 		    return '?';
893*38fd1498Szrj 		  }
894*38fd1498Szrj 	      }
895*38fd1498Szrj 	    else if (pfound->has_arg == 1)
896*38fd1498Szrj 	      {
897*38fd1498Szrj 		if (optind < argc)
898*38fd1498Szrj 		  optarg = argv[optind++];
899*38fd1498Szrj 		else
900*38fd1498Szrj 		  {
901*38fd1498Szrj 		    if (opterr)
902*38fd1498Szrj 		      fprintf (stderr,
903*38fd1498Szrj 			       _("%s: option `%s' requires an argument\n"),
904*38fd1498Szrj 			       argv[0], argv[optind - 1]);
905*38fd1498Szrj 		    nextchar += strlen (nextchar);
906*38fd1498Szrj 		    return optstring[0] == ':' ? ':' : '?';
907*38fd1498Szrj 		  }
908*38fd1498Szrj 	      }
909*38fd1498Szrj 	    nextchar += strlen (nextchar);
910*38fd1498Szrj 	    if (longind != NULL)
911*38fd1498Szrj 	      *longind = option_index;
912*38fd1498Szrj 	    if (pfound->flag)
913*38fd1498Szrj 	      {
914*38fd1498Szrj 		*(pfound->flag) = pfound->val;
915*38fd1498Szrj 		return 0;
916*38fd1498Szrj 	      }
917*38fd1498Szrj 	    return pfound->val;
918*38fd1498Szrj 	  }
919*38fd1498Szrj 	  nextchar = NULL;
920*38fd1498Szrj 	  return 'W';	/* Let the application handle it.   */
921*38fd1498Szrj       }
922*38fd1498Szrj     if (temp[1] == ':')
923*38fd1498Szrj       {
924*38fd1498Szrj 	if (temp[2] == ':')
925*38fd1498Szrj 	  {
926*38fd1498Szrj 	    /* This is an option that accepts an argument optionally.  */
927*38fd1498Szrj 	    if (*nextchar != '\0')
928*38fd1498Szrj 	      {
929*38fd1498Szrj 		optarg = nextchar;
930*38fd1498Szrj 		optind++;
931*38fd1498Szrj 	      }
932*38fd1498Szrj 	    else
933*38fd1498Szrj 	      optarg = NULL;
934*38fd1498Szrj 	    nextchar = NULL;
935*38fd1498Szrj 	  }
936*38fd1498Szrj 	else
937*38fd1498Szrj 	  {
938*38fd1498Szrj 	    /* This is an option that requires an argument.  */
939*38fd1498Szrj 	    if (*nextchar != '\0')
940*38fd1498Szrj 	      {
941*38fd1498Szrj 		optarg = nextchar;
942*38fd1498Szrj 		/* If we end this ARGV-element by taking the rest as an arg,
943*38fd1498Szrj 		   we must advance to the next element now.  */
944*38fd1498Szrj 		optind++;
945*38fd1498Szrj 	      }
946*38fd1498Szrj 	    else if (optind == argc)
947*38fd1498Szrj 	      {
948*38fd1498Szrj 		if (opterr)
949*38fd1498Szrj 		  {
950*38fd1498Szrj 		    /* 1003.2 specifies the format of this message.  */
951*38fd1498Szrj 		    fprintf (stderr,
952*38fd1498Szrj 			   _("%s: option requires an argument -- %c\n"),
953*38fd1498Szrj 			   argv[0], c);
954*38fd1498Szrj 		  }
955*38fd1498Szrj 		optopt = c;
956*38fd1498Szrj 		if (optstring[0] == ':')
957*38fd1498Szrj 		  c = ':';
958*38fd1498Szrj 		else
959*38fd1498Szrj 		  c = '?';
960*38fd1498Szrj 	      }
961*38fd1498Szrj 	    else
962*38fd1498Szrj 	      /* We already incremented `optind' once;
963*38fd1498Szrj 		 increment it again when taking next ARGV-elt as argument.  */
964*38fd1498Szrj 	      optarg = argv[optind++];
965*38fd1498Szrj 	    nextchar = NULL;
966*38fd1498Szrj 	  }
967*38fd1498Szrj       }
968*38fd1498Szrj     return c;
969*38fd1498Szrj   }
970*38fd1498Szrj }
971*38fd1498Szrj 
972*38fd1498Szrj int
getopt(int argc,char * const * argv,const char * optstring)973*38fd1498Szrj getopt (int argc, char *const *argv, const char *optstring)
974*38fd1498Szrj {
975*38fd1498Szrj   return _getopt_internal (argc, argv, optstring,
976*38fd1498Szrj 			   (const struct option *) 0,
977*38fd1498Szrj 			   (int *) 0,
978*38fd1498Szrj 			   0);
979*38fd1498Szrj }
980*38fd1498Szrj 
981*38fd1498Szrj #endif	/* Not ELIDE_CODE.  */
982*38fd1498Szrj 
983*38fd1498Szrj #ifdef TEST
984*38fd1498Szrj 
985*38fd1498Szrj /* Compile with -DTEST to make an executable for use in testing
986*38fd1498Szrj    the above definition of `getopt'.  */
987*38fd1498Szrj 
988*38fd1498Szrj int
main(int argc,char ** argv)989*38fd1498Szrj main (int argc, char **argv)
990*38fd1498Szrj {
991*38fd1498Szrj   int c;
992*38fd1498Szrj   int digit_optind = 0;
993*38fd1498Szrj 
994*38fd1498Szrj   while (1)
995*38fd1498Szrj     {
996*38fd1498Szrj       int this_option_optind = optind ? optind : 1;
997*38fd1498Szrj 
998*38fd1498Szrj       c = getopt (argc, argv, "abc:d:0123456789");
999*38fd1498Szrj       if (c == -1)
1000*38fd1498Szrj 	break;
1001*38fd1498Szrj 
1002*38fd1498Szrj       switch (c)
1003*38fd1498Szrj 	{
1004*38fd1498Szrj 	case '0':
1005*38fd1498Szrj 	case '1':
1006*38fd1498Szrj 	case '2':
1007*38fd1498Szrj 	case '3':
1008*38fd1498Szrj 	case '4':
1009*38fd1498Szrj 	case '5':
1010*38fd1498Szrj 	case '6':
1011*38fd1498Szrj 	case '7':
1012*38fd1498Szrj 	case '8':
1013*38fd1498Szrj 	case '9':
1014*38fd1498Szrj 	  if (digit_optind != 0 && digit_optind != this_option_optind)
1015*38fd1498Szrj 	    printf ("digits occur in two different argv-elements.\n");
1016*38fd1498Szrj 	  digit_optind = this_option_optind;
1017*38fd1498Szrj 	  printf ("option %c\n", c);
1018*38fd1498Szrj 	  break;
1019*38fd1498Szrj 
1020*38fd1498Szrj 	case 'a':
1021*38fd1498Szrj 	  printf ("option a\n");
1022*38fd1498Szrj 	  break;
1023*38fd1498Szrj 
1024*38fd1498Szrj 	case 'b':
1025*38fd1498Szrj 	  printf ("option b\n");
1026*38fd1498Szrj 	  break;
1027*38fd1498Szrj 
1028*38fd1498Szrj 	case 'c':
1029*38fd1498Szrj 	  printf ("option c with value `%s'\n", optarg);
1030*38fd1498Szrj 	  break;
1031*38fd1498Szrj 
1032*38fd1498Szrj 	case '?':
1033*38fd1498Szrj 	  break;
1034*38fd1498Szrj 
1035*38fd1498Szrj 	default:
1036*38fd1498Szrj 	  printf ("?? getopt returned character code 0%o ??\n", c);
1037*38fd1498Szrj 	}
1038*38fd1498Szrj     }
1039*38fd1498Szrj 
1040*38fd1498Szrj   if (optind < argc)
1041*38fd1498Szrj     {
1042*38fd1498Szrj       printf ("non-option ARGV-elements: ");
1043*38fd1498Szrj       while (optind < argc)
1044*38fd1498Szrj 	printf ("%s ", argv[optind++]);
1045*38fd1498Szrj       printf ("\n");
1046*38fd1498Szrj     }
1047*38fd1498Szrj 
1048*38fd1498Szrj   exit (0);
1049*38fd1498Szrj }
1050*38fd1498Szrj 
1051*38fd1498Szrj #endif /* TEST */
1052