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