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