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