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