blob: 4342a34104c392c253d80fab6af534e3640e0855 [file] [log] [blame]
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001/* Getopt for GNU.
Ying Wang05436632013-04-05 16:01:00 -07002 NOTE: getopt is part of the C library, so if you don't know what
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08003 "Keep this file name-space clean" means, talk to drepper@gnu.org
4 before changing it!
Ying Wang05436632013-04-05 16:01:00 -07005 Copyright (C) 1987-1996, 1998-2004, 2006, 2008-2012 Free Software
6 Foundation, Inc.
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08007 This file is part of the GNU C Library.
8
Ying Wang05436632013-04-05 16:01:00 -07009 This program is free software: you can redistribute it and/or modify
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080010 it under the terms of the GNU General Public License as published by
Ying Wang05436632013-04-05 16:01:00 -070011 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080013
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
Ying Wang05436632013-04-05 16:01:00 -070019 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080021
Ying Wang05436632013-04-05 16:01:00 -070022#ifndef _LIBC
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080023# include <config.h>
24#endif
25
26#include "getopt.h"
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080033#ifdef _LIBC
34# include <libintl.h>
35#else
36# include "gettext.h"
37# define _(msgid) gettext (msgid)
38#endif
39
40#if defined _LIBC && defined USE_IN_LIBIO
41# include <wchar.h>
42#endif
43
Ying Wang05436632013-04-05 16:01:00 -070044/* This version of 'getopt' appears to the caller like standard Unix 'getopt'
45 but it behaves differently for the user, since it allows the user
46 to intersperse the options with the other arguments.
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080047
Ying Wang05436632013-04-05 16:01:00 -070048 As 'getopt_long' works, it permutes the elements of ARGV so that,
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080049 when it is done, all the options precede everything else. Thus
50 all application programs are extended to handle flexible argument order.
51
Ying Wang05436632013-04-05 16:01:00 -070052 Using 'getopt' or setting the environment variable POSIXLY_CORRECT
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080053 disables permutation.
Ying Wang05436632013-04-05 16:01:00 -070054 Then the behavior is completely standard.
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080055
56 GNU application programs can use a third alternative mode in which
57 they can distinguish the relative order of options and other arguments. */
58
59#include "getopt_int.h"
60
Ying Wang05436632013-04-05 16:01:00 -070061/* For communication from 'getopt' to the caller.
62 When 'getopt' finds an option that takes an argument,
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080063 the argument value is returned here.
Ying Wang05436632013-04-05 16:01:00 -070064 Also, when 'ordering' is RETURN_IN_ORDER,
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080065 each non-option ARGV-element is returned here. */
66
67char *optarg;
68
69/* Index in ARGV of the next element to be scanned.
70 This is used for communication to and from the caller
Ying Wang05436632013-04-05 16:01:00 -070071 and for communication between successive calls to 'getopt'.
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080072
Ying Wang05436632013-04-05 16:01:00 -070073 On entry to 'getopt', zero means this is the first call; initialize.
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080074
Ying Wang05436632013-04-05 16:01:00 -070075 When 'getopt' returns -1, this is the index of the first of the
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080076 non-option elements that the caller should itself scan.
77
Ying Wang05436632013-04-05 16:01:00 -070078 Otherwise, 'optind' communicates from one call to the next
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080079 how much of ARGV has been scanned so far. */
80
81/* 1003.2 says this must be 1 before any call. */
82int optind = 1;
83
84/* Callers store zero here to inhibit the error message
85 for unrecognized options. */
86
87int opterr = 1;
88
89/* Set to an option character which was unrecognized.
90 This must be initialized on some systems to avoid linking in the
91 system's own getopt implementation. */
92
93int optopt = '?';
94
95/* Keep a global copy of all internal members of getopt_data. */
96
97static struct _getopt_data getopt_data;
98
99
100#if defined HAVE_DECL_GETENV && !HAVE_DECL_GETENV
101extern char *getenv ();
102#endif
103
104#ifdef _LIBC
105/* Stored original parameters.
106 XXX This is no good solution. We should rather copy the args so
107 that we can compare them later. But we must not use malloc(3). */
108extern int __libc_argc;
109extern char **__libc_argv;
110
111/* Bash 2.0 gives us an environment variable containing flags
112 indicating ARGV elements that should not be considered arguments. */
113
114# ifdef USE_NONOPTION_FLAGS
115/* Defined in getopt_init.c */
116extern char *__getopt_nonoption_flags;
117# endif
118
119# ifdef USE_NONOPTION_FLAGS
120# define SWAP_FLAGS(ch1, ch2) \
Ying Wang05436632013-04-05 16:01:00 -0700121 if (d->__nonoption_flags_len > 0) \
122 { \
123 char __tmp = __getopt_nonoption_flags[ch1]; \
124 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
125 __getopt_nonoption_flags[ch2] = __tmp; \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800126 }
127# else
128# define SWAP_FLAGS(ch1, ch2)
129# endif
Ying Wang05436632013-04-05 16:01:00 -0700130#else /* !_LIBC */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800131# define SWAP_FLAGS(ch1, ch2)
Ying Wang05436632013-04-05 16:01:00 -0700132#endif /* _LIBC */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800133
134/* Exchange two adjacent subsequences of ARGV.
135 One subsequence is elements [first_nonopt,last_nonopt)
136 which contains all the non-options that have been skipped so far.
137 The other is elements [last_nonopt,optind), which contains all
138 the options processed since those non-options were skipped.
139
Ying Wang05436632013-04-05 16:01:00 -0700140 'first_nonopt' and 'last_nonopt' are relocated so that they describe
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800141 the new indices of the non-options in ARGV after they are moved. */
142
143static void
144exchange (char **argv, struct _getopt_data *d)
145{
146 int bottom = d->__first_nonopt;
147 int middle = d->__last_nonopt;
148 int top = d->optind;
149 char *tem;
150
151 /* Exchange the shorter segment with the far end of the longer segment.
152 That puts the shorter segment into the right place.
153 It leaves the longer segment in the right place overall,
154 but it consists of two parts that need to be swapped next. */
155
156#if defined _LIBC && defined USE_NONOPTION_FLAGS
Ying Wang05436632013-04-05 16:01:00 -0700157 /* First make sure the handling of the '__getopt_nonoption_flags'
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800158 string can work normally. Our top argument must be in the range
159 of the string. */
160 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
161 {
162 /* We must extend the array. The user plays games with us and
Ying Wang05436632013-04-05 16:01:00 -0700163 presents new arguments. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800164 char *new_str = malloc (top + 1);
165 if (new_str == NULL)
Ying Wang05436632013-04-05 16:01:00 -0700166 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800167 else
Ying Wang05436632013-04-05 16:01:00 -0700168 {
169 memset (__mempcpy (new_str, __getopt_nonoption_flags,
170 d->__nonoption_flags_max_len),
171 '\0', top + 1 - d->__nonoption_flags_max_len);
172 d->__nonoption_flags_max_len = top + 1;
173 __getopt_nonoption_flags = new_str;
174 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800175 }
176#endif
177
178 while (top > middle && middle > bottom)
179 {
180 if (top - middle > middle - bottom)
Ying Wang05436632013-04-05 16:01:00 -0700181 {
182 /* Bottom segment is the short one. */
183 int len = middle - bottom;
184 register int i;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800185
Ying Wang05436632013-04-05 16:01:00 -0700186 /* Swap it with the top part of the top segment. */
187 for (i = 0; i < len; i++)
188 {
189 tem = argv[bottom + i];
190 argv[bottom + i] = argv[top - (middle - bottom) + i];
191 argv[top - (middle - bottom) + i] = tem;
192 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
193 }
194 /* Exclude the moved bottom segment from further swapping. */
195 top -= len;
196 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800197 else
Ying Wang05436632013-04-05 16:01:00 -0700198 {
199 /* Top segment is the short one. */
200 int len = top - middle;
201 register int i;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800202
Ying Wang05436632013-04-05 16:01:00 -0700203 /* Swap it with the bottom part of the bottom segment. */
204 for (i = 0; i < len; i++)
205 {
206 tem = argv[bottom + i];
207 argv[bottom + i] = argv[middle + i];
208 argv[middle + i] = tem;
209 SWAP_FLAGS (bottom + i, middle + i);
210 }
211 /* Exclude the moved top segment from further swapping. */
212 bottom += len;
213 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800214 }
215
216 /* Update records for the slots the non-options now occupy. */
217
218 d->__first_nonopt += (d->optind - d->__last_nonopt);
219 d->__last_nonopt = d->optind;
220}
221
222/* Initialize the internal data when the first call is made. */
223
224static const char *
Ying Wang05436632013-04-05 16:01:00 -0700225_getopt_initialize (int argc _GL_UNUSED,
226 char **argv _GL_UNUSED, const char *optstring,
227 struct _getopt_data *d, int posixly_correct)
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800228{
229 /* Start processing options with ARGV-element 1 (since ARGV-element 0
230 is the program name); the sequence of previously skipped
231 non-option ARGV-elements is empty. */
232
233 d->__first_nonopt = d->__last_nonopt = d->optind;
234
235 d->__nextchar = NULL;
236
237 d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
238
239 /* Determine how to handle the ordering of options and nonoptions. */
240
241 if (optstring[0] == '-')
242 {
243 d->__ordering = RETURN_IN_ORDER;
244 ++optstring;
245 }
246 else if (optstring[0] == '+')
247 {
248 d->__ordering = REQUIRE_ORDER;
249 ++optstring;
250 }
251 else if (d->__posixly_correct)
252 d->__ordering = REQUIRE_ORDER;
253 else
254 d->__ordering = PERMUTE;
255
256#if defined _LIBC && defined USE_NONOPTION_FLAGS
257 if (!d->__posixly_correct
258 && argc == __libc_argc && argv == __libc_argv)
259 {
260 if (d->__nonoption_flags_max_len == 0)
Ying Wang05436632013-04-05 16:01:00 -0700261 {
262 if (__getopt_nonoption_flags == NULL
263 || __getopt_nonoption_flags[0] == '\0')
264 d->__nonoption_flags_max_len = -1;
265 else
266 {
267 const char *orig_str = __getopt_nonoption_flags;
268 int len = d->__nonoption_flags_max_len = strlen (orig_str);
269 if (d->__nonoption_flags_max_len < argc)
270 d->__nonoption_flags_max_len = argc;
271 __getopt_nonoption_flags =
272 (char *) malloc (d->__nonoption_flags_max_len);
273 if (__getopt_nonoption_flags == NULL)
274 d->__nonoption_flags_max_len = -1;
275 else
276 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
277 '\0', d->__nonoption_flags_max_len - len);
278 }
279 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800280 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
281 }
282 else
283 d->__nonoption_flags_len = 0;
284#endif
285
286 return optstring;
287}
288
289/* Scan elements of ARGV (whose length is ARGC) for option characters
290 given in OPTSTRING.
291
292 If an element of ARGV starts with '-', and is not exactly "-" or "--",
293 then it is an option element. The characters of this element
Ying Wang05436632013-04-05 16:01:00 -0700294 (aside from the initial '-') are option characters. If 'getopt'
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800295 is called repeatedly, it returns successively each of the option characters
296 from each of the option elements.
297
Ying Wang05436632013-04-05 16:01:00 -0700298 If 'getopt' finds another option character, it returns that character,
299 updating 'optind' and 'nextchar' so that the next call to 'getopt' can
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800300 resume the scan with the following option character or ARGV-element.
301
Ying Wang05436632013-04-05 16:01:00 -0700302 If there are no more option characters, 'getopt' returns -1.
303 Then 'optind' is the index in ARGV of the first ARGV-element
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800304 that is not an option. (The ARGV-elements have been permuted
305 so that those that are not options now come last.)
306
307 OPTSTRING is a string containing the legitimate option characters.
308 If an option character is seen that is not listed in OPTSTRING,
Ying Wang05436632013-04-05 16:01:00 -0700309 return '?' after printing an error message. If you set 'opterr' to
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800310 zero, the error message is suppressed but we still return '?'.
311
312 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
313 so the following text in the same ARGV-element, or the text of the following
Ying Wang05436632013-04-05 16:01:00 -0700314 ARGV-element, is returned in 'optarg'. Two colons mean an option that
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800315 wants an optional arg; if there is text in the current ARGV-element,
Ying Wang05436632013-04-05 16:01:00 -0700316 it is returned in 'optarg', otherwise 'optarg' is set to zero.
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800317
Ying Wang05436632013-04-05 16:01:00 -0700318 If OPTSTRING starts with '-' or '+', it requests different methods of
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800319 handling the non-option ARGV-elements.
320 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
321
Ying Wang05436632013-04-05 16:01:00 -0700322 Long-named options begin with '--' instead of '-'.
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800323 Their names may be abbreviated as long as the abbreviation is unique
324 or is an exact match for some defined option. If they have an
325 argument, it follows the option name in the same ARGV-element, separated
Ying Wang05436632013-04-05 16:01:00 -0700326 from the option name by a '=', or else the in next ARGV-element.
327 When 'getopt' finds a long-named option, it returns 0 if that option's
328 'flag' field is nonzero, the value of the option's 'val' field
329 if the 'flag' field is zero.
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800330
Ying Wang05436632013-04-05 16:01:00 -0700331 The elements of ARGV aren't really const, because we permute them.
332 But we pretend they're const in the prototype to be compatible
333 with other systems.
334
335 LONGOPTS is a vector of 'struct option' terminated by an
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800336 element containing a name which is zero.
337
338 LONGIND returns the index in LONGOPT of the long-named option found.
339 It is only valid when a long-named option has been found by the most
340 recent call.
341
342 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
Ying Wang05436632013-04-05 16:01:00 -0700343 long-named options. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800344
345int
346_getopt_internal_r (int argc, char **argv, const char *optstring,
Ying Wang05436632013-04-05 16:01:00 -0700347 const struct option *longopts, int *longind,
348 int long_only, struct _getopt_data *d, int posixly_correct)
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800349{
350 int print_errors = d->opterr;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800351
352 if (argc < 1)
353 return -1;
354
355 d->optarg = NULL;
356
357 if (d->optind == 0 || !d->__initialized)
358 {
359 if (d->optind == 0)
Ying Wang05436632013-04-05 16:01:00 -0700360 d->optind = 1; /* Don't scan ARGV[0], the program name. */
361 optstring = _getopt_initialize (argc, argv, optstring, d,
362 posixly_correct);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800363 d->__initialized = 1;
364 }
Ying Wang05436632013-04-05 16:01:00 -0700365 else if (optstring[0] == '-' || optstring[0] == '+')
366 optstring++;
367 if (optstring[0] == ':')
368 print_errors = 0;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800369
370 /* Test whether ARGV[optind] points to a non-option argument.
371 Either it does not have option syntax, or there is an environment flag
372 from the shell indicating it is not an option. The later information
373 is only used when the used in the GNU libc. */
374#if defined _LIBC && defined USE_NONOPTION_FLAGS
375# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
Ying Wang05436632013-04-05 16:01:00 -0700376 || (d->optind < d->__nonoption_flags_len \
377 && __getopt_nonoption_flags[d->optind] == '1'))
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800378#else
379# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
380#endif
381
382 if (d->__nextchar == NULL || *d->__nextchar == '\0')
383 {
384 /* Advance to the next ARGV-element. */
385
386 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
Ying Wang05436632013-04-05 16:01:00 -0700387 moved back by the user (who may also have changed the arguments). */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800388 if (d->__last_nonopt > d->optind)
Ying Wang05436632013-04-05 16:01:00 -0700389 d->__last_nonopt = d->optind;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800390 if (d->__first_nonopt > d->optind)
Ying Wang05436632013-04-05 16:01:00 -0700391 d->__first_nonopt = d->optind;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800392
393 if (d->__ordering == PERMUTE)
Ying Wang05436632013-04-05 16:01:00 -0700394 {
395 /* If we have just processed some options following some non-options,
396 exchange them so that the options come first. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800397
Ying Wang05436632013-04-05 16:01:00 -0700398 if (d->__first_nonopt != d->__last_nonopt
399 && d->__last_nonopt != d->optind)
400 exchange ((char **) argv, d);
401 else if (d->__last_nonopt != d->optind)
402 d->__first_nonopt = d->optind;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800403
Ying Wang05436632013-04-05 16:01:00 -0700404 /* Skip any additional non-options
405 and extend the range of non-options previously skipped. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800406
Ying Wang05436632013-04-05 16:01:00 -0700407 while (d->optind < argc && NONOPTION_P)
408 d->optind++;
409 d->__last_nonopt = d->optind;
410 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800411
Ying Wang05436632013-04-05 16:01:00 -0700412 /* The special ARGV-element '--' means premature end of options.
413 Skip it like a null option,
414 then exchange with previous non-options as if it were an option,
415 then skip everything else like a non-option. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800416
417 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
Ying Wang05436632013-04-05 16:01:00 -0700418 {
419 d->optind++;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800420
Ying Wang05436632013-04-05 16:01:00 -0700421 if (d->__first_nonopt != d->__last_nonopt
422 && d->__last_nonopt != d->optind)
423 exchange ((char **) argv, d);
424 else if (d->__first_nonopt == d->__last_nonopt)
425 d->__first_nonopt = d->optind;
426 d->__last_nonopt = argc;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800427
Ying Wang05436632013-04-05 16:01:00 -0700428 d->optind = argc;
429 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800430
431 /* If we have done all the ARGV-elements, stop the scan
Ying Wang05436632013-04-05 16:01:00 -0700432 and back over any non-options that we skipped and permuted. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800433
434 if (d->optind == argc)
Ying Wang05436632013-04-05 16:01:00 -0700435 {
436 /* Set the next-arg-index to point at the non-options
437 that we previously skipped, so the caller will digest them. */
438 if (d->__first_nonopt != d->__last_nonopt)
439 d->optind = d->__first_nonopt;
440 return -1;
441 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800442
443 /* If we have come to a non-option and did not permute it,
Ying Wang05436632013-04-05 16:01:00 -0700444 either stop the scan or describe it to the caller and pass it by. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800445
446 if (NONOPTION_P)
Ying Wang05436632013-04-05 16:01:00 -0700447 {
448 if (d->__ordering == REQUIRE_ORDER)
449 return -1;
450 d->optarg = argv[d->optind++];
451 return 1;
452 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800453
454 /* We have found another option-ARGV-element.
Ying Wang05436632013-04-05 16:01:00 -0700455 Skip the initial punctuation. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800456
457 d->__nextchar = (argv[d->optind] + 1
Ying Wang05436632013-04-05 16:01:00 -0700458 + (longopts != NULL && argv[d->optind][1] == '-'));
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800459 }
460
461 /* Decode the current option-ARGV-element. */
462
463 /* Check whether the ARGV-element is a long option.
464
465 If long_only and the ARGV-element has the form "-f", where f is
466 a valid short option, don't consider it an abbreviated form of
467 a long option that starts with f. Otherwise there would be no
468 way to give the -f short option.
469
470 On the other hand, if there's a long option "fubar" and
471 the ARGV-element is "-fu", do consider that an abbreviation of
472 the long option, just like "--fu", and not "-f" with arg "u".
473
474 This distinction seems to be the most useful approach. */
475
476 if (longopts != NULL
477 && (argv[d->optind][1] == '-'
Ying Wang05436632013-04-05 16:01:00 -0700478 || (long_only && (argv[d->optind][2]
479 || !strchr (optstring, argv[d->optind][1])))))
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800480 {
481 char *nameend;
Ying Wang05436632013-04-05 16:01:00 -0700482 unsigned int namelen;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800483 const struct option *p;
484 const struct option *pfound = NULL;
Ying Wang05436632013-04-05 16:01:00 -0700485 struct option_list
486 {
487 const struct option *p;
488 struct option_list *next;
489 } *ambig_list = NULL;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800490 int exact = 0;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800491 int indfound = -1;
492 int option_index;
493
494 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
Ying Wang05436632013-04-05 16:01:00 -0700495 /* Do nothing. */ ;
496 namelen = nameend - d->__nextchar;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800497
498 /* Test all long options for either exact match
Ying Wang05436632013-04-05 16:01:00 -0700499 or abbreviated matches. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800500 for (p = longopts, option_index = 0; p->name; p++, option_index++)
Ying Wang05436632013-04-05 16:01:00 -0700501 if (!strncmp (p->name, d->__nextchar, namelen))
502 {
503 if (namelen == (unsigned int) strlen (p->name))
504 {
505 /* Exact match found. */
506 pfound = p;
507 indfound = option_index;
508 exact = 1;
509 break;
510 }
511 else if (pfound == NULL)
512 {
513 /* First nonexact match found. */
514 pfound = p;
515 indfound = option_index;
516 }
517 else if (long_only
518 || pfound->has_arg != p->has_arg
519 || pfound->flag != p->flag
520 || pfound->val != p->val)
521 {
522 /* Second or later nonexact match found. */
523 struct option_list *newp = malloc (sizeof (*newp));
524 newp->p = p;
525 newp->next = ambig_list;
526 ambig_list = newp;
527 }
528 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800529
Ying Wang05436632013-04-05 16:01:00 -0700530 if (ambig_list != NULL && !exact)
531 {
532 if (print_errors)
533 {
534 struct option_list first;
535 first.p = pfound;
536 first.next = ambig_list;
537 ambig_list = &first;
538
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800539#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700540 char *buf = NULL;
541 size_t buflen = 0;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800542
Ying Wang05436632013-04-05 16:01:00 -0700543 FILE *fp = open_memstream (&buf, &buflen);
544 if (fp != NULL)
545 {
546 fprintf (fp,
547 _("%s: option '%s' is ambiguous; possibilities:"),
548 argv[0], argv[d->optind]);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800549
Ying Wang05436632013-04-05 16:01:00 -0700550 do
551 {
552 fprintf (fp, " '--%s'", ambig_list->p->name);
553 ambig_list = ambig_list->next;
554 }
555 while (ambig_list != NULL);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800556
Ying Wang05436632013-04-05 16:01:00 -0700557 fputc_unlocked ('\n', fp);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800558
Ying Wang05436632013-04-05 16:01:00 -0700559 if (__builtin_expect (fclose (fp) != EOF, 1))
560 {
561 _IO_flockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800562
Ying Wang05436632013-04-05 16:01:00 -0700563 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
564 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
565
566 __fxprintf (NULL, "%s", buf);
567
568 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
569 _IO_funlockfile (stderr);
570
571 free (buf);
572 }
573 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800574#else
Ying Wang05436632013-04-05 16:01:00 -0700575 fprintf (stderr,
576 _("%s: option '%s' is ambiguous; possibilities:"),
577 argv[0], argv[d->optind]);
578 do
579 {
580 fprintf (stderr, " '--%s'", ambig_list->p->name);
581 ambig_list = ambig_list->next;
582 }
583 while (ambig_list != NULL);
584
585 fputc ('\n', stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800586#endif
Ying Wang05436632013-04-05 16:01:00 -0700587 }
588 d->__nextchar += strlen (d->__nextchar);
589 d->optind++;
590 d->optopt = 0;
591 return '?';
592 }
593
594 while (ambig_list != NULL)
595 {
596 struct option_list *pn = ambig_list->next;
597 free (ambig_list);
598 ambig_list = pn;
599 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800600
601 if (pfound != NULL)
Ying Wang05436632013-04-05 16:01:00 -0700602 {
603 option_index = indfound;
604 d->optind++;
605 if (*nameend)
606 {
607 /* Don't test has_arg with >, because some C compilers don't
608 allow it to be used on enums. */
609 if (pfound->has_arg)
610 d->optarg = nameend + 1;
611 else
612 {
613 if (print_errors)
614 {
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800615#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700616 char *buf;
617 int n;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800618#endif
619
Ying Wang05436632013-04-05 16:01:00 -0700620 if (argv[d->optind - 1][1] == '-')
621 {
622 /* --option */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800623#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700624 n = __asprintf (&buf, _("\
625%s: option '--%s' doesn't allow an argument\n"),
626 argv[0], pfound->name);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800627#else
Ying Wang05436632013-04-05 16:01:00 -0700628 fprintf (stderr, _("\
629%s: option '--%s' doesn't allow an argument\n"),
630 argv[0], pfound->name);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800631#endif
Ying Wang05436632013-04-05 16:01:00 -0700632 }
633 else
634 {
635 /* +option or -option */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800636#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700637 n = __asprintf (&buf, _("\
638%s: option '%c%s' doesn't allow an argument\n"),
639 argv[0], argv[d->optind - 1][0],
640 pfound->name);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800641#else
Ying Wang05436632013-04-05 16:01:00 -0700642 fprintf (stderr, _("\
643%s: option '%c%s' doesn't allow an argument\n"),
644 argv[0], argv[d->optind - 1][0],
645 pfound->name);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800646#endif
Ying Wang05436632013-04-05 16:01:00 -0700647 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800648
649#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700650 if (n >= 0)
651 {
652 _IO_flockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800653
Ying Wang05436632013-04-05 16:01:00 -0700654 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
655 ((_IO_FILE *) stderr)->_flags2
656 |= _IO_FLAGS2_NOTCANCEL;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800657
Ying Wang05436632013-04-05 16:01:00 -0700658 __fxprintf (NULL, "%s", buf);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800659
Ying Wang05436632013-04-05 16:01:00 -0700660 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
661 _IO_funlockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800662
Ying Wang05436632013-04-05 16:01:00 -0700663 free (buf);
664 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800665#endif
Ying Wang05436632013-04-05 16:01:00 -0700666 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800667
Ying Wang05436632013-04-05 16:01:00 -0700668 d->__nextchar += strlen (d->__nextchar);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800669
Ying Wang05436632013-04-05 16:01:00 -0700670 d->optopt = pfound->val;
671 return '?';
672 }
673 }
674 else if (pfound->has_arg == 1)
675 {
676 if (d->optind < argc)
677 d->optarg = argv[d->optind++];
678 else
679 {
680 if (print_errors)
681 {
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800682#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700683 char *buf;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800684
Ying Wang05436632013-04-05 16:01:00 -0700685 if (__asprintf (&buf, _("\
686%s: option '--%s' requires an argument\n"),
687 argv[0], pfound->name) >= 0)
688 {
689 _IO_flockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800690
Ying Wang05436632013-04-05 16:01:00 -0700691 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
692 ((_IO_FILE *) stderr)->_flags2
693 |= _IO_FLAGS2_NOTCANCEL;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800694
Ying Wang05436632013-04-05 16:01:00 -0700695 __fxprintf (NULL, "%s", buf);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800696
Ying Wang05436632013-04-05 16:01:00 -0700697 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
698 _IO_funlockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800699
Ying Wang05436632013-04-05 16:01:00 -0700700 free (buf);
701 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800702#else
Ying Wang05436632013-04-05 16:01:00 -0700703 fprintf (stderr,
704 _("%s: option '--%s' requires an argument\n"),
705 argv[0], pfound->name);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800706#endif
Ying Wang05436632013-04-05 16:01:00 -0700707 }
708 d->__nextchar += strlen (d->__nextchar);
709 d->optopt = pfound->val;
710 return optstring[0] == ':' ? ':' : '?';
711 }
712 }
713 d->__nextchar += strlen (d->__nextchar);
714 if (longind != NULL)
715 *longind = option_index;
716 if (pfound->flag)
717 {
718 *(pfound->flag) = pfound->val;
719 return 0;
720 }
721 return pfound->val;
722 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800723
724 /* Can't find it as a long option. If this is not getopt_long_only,
Ying Wang05436632013-04-05 16:01:00 -0700725 or the option starts with '--' or is not a valid short
726 option, then it's an error.
727 Otherwise interpret it as a short option. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800728 if (!long_only || argv[d->optind][1] == '-'
Ying Wang05436632013-04-05 16:01:00 -0700729 || strchr (optstring, *d->__nextchar) == NULL)
730 {
731 if (print_errors)
732 {
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800733#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700734 char *buf;
735 int n;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800736#endif
737
Ying Wang05436632013-04-05 16:01:00 -0700738 if (argv[d->optind][1] == '-')
739 {
740 /* --option */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800741#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700742 n = __asprintf (&buf, _("%s: unrecognized option '--%s'\n"),
743 argv[0], d->__nextchar);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800744#else
Ying Wang05436632013-04-05 16:01:00 -0700745 fprintf (stderr, _("%s: unrecognized option '--%s'\n"),
746 argv[0], d->__nextchar);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800747#endif
Ying Wang05436632013-04-05 16:01:00 -0700748 }
749 else
750 {
751 /* +option or -option */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800752#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700753 n = __asprintf (&buf, _("%s: unrecognized option '%c%s'\n"),
754 argv[0], argv[d->optind][0], d->__nextchar);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800755#else
Ying Wang05436632013-04-05 16:01:00 -0700756 fprintf (stderr, _("%s: unrecognized option '%c%s'\n"),
757 argv[0], argv[d->optind][0], d->__nextchar);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800758#endif
Ying Wang05436632013-04-05 16:01:00 -0700759 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800760
761#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700762 if (n >= 0)
763 {
764 _IO_flockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800765
Ying Wang05436632013-04-05 16:01:00 -0700766 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
767 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800768
Ying Wang05436632013-04-05 16:01:00 -0700769 __fxprintf (NULL, "%s", buf);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800770
Ying Wang05436632013-04-05 16:01:00 -0700771 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
772 _IO_funlockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800773
Ying Wang05436632013-04-05 16:01:00 -0700774 free (buf);
775 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800776#endif
Ying Wang05436632013-04-05 16:01:00 -0700777 }
778 d->__nextchar = (char *) "";
779 d->optind++;
780 d->optopt = 0;
781 return '?';
782 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800783 }
784
785 /* Look at and handle the next short option-character. */
786
787 {
788 char c = *d->__nextchar++;
Ying Wang05436632013-04-05 16:01:00 -0700789 const char *temp = strchr (optstring, c);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800790
Ying Wang05436632013-04-05 16:01:00 -0700791 /* Increment 'optind' when we start to process its last character. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800792 if (*d->__nextchar == '\0')
793 ++d->optind;
794
Ying Wang05436632013-04-05 16:01:00 -0700795 if (temp == NULL || c == ':' || c == ';')
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800796 {
Ying Wang05436632013-04-05 16:01:00 -0700797 if (print_errors)
798 {
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800799#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700800 char *buf;
801 int n;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800802#endif
803
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800804#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700805 n = __asprintf (&buf, _("%s: invalid option -- '%c'\n"),
806 argv[0], c);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800807#else
Ying Wang05436632013-04-05 16:01:00 -0700808 fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800809#endif
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800810
811#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700812 if (n >= 0)
813 {
814 _IO_flockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800815
Ying Wang05436632013-04-05 16:01:00 -0700816 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
817 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800818
Ying Wang05436632013-04-05 16:01:00 -0700819 __fxprintf (NULL, "%s", buf);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800820
Ying Wang05436632013-04-05 16:01:00 -0700821 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
822 _IO_funlockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800823
Ying Wang05436632013-04-05 16:01:00 -0700824 free (buf);
825 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800826#endif
Ying Wang05436632013-04-05 16:01:00 -0700827 }
828 d->optopt = c;
829 return '?';
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800830 }
831 /* Convenience. Treat POSIX -W foo same as long option --foo */
832 if (temp[0] == 'W' && temp[1] == ';')
833 {
Ying Wang05436632013-04-05 16:01:00 -0700834 char *nameend;
835 const struct option *p;
836 const struct option *pfound = NULL;
837 int exact = 0;
838 int ambig = 0;
839 int indfound = 0;
840 int option_index;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800841
Ying Wang05436632013-04-05 16:01:00 -0700842 if (longopts == NULL)
843 goto no_longs;
844
845 /* This is an option that requires an argument. */
846 if (*d->__nextchar != '\0')
847 {
848 d->optarg = d->__nextchar;
849 /* If we end this ARGV-element by taking the rest as an arg,
850 we must advance to the next element now. */
851 d->optind++;
852 }
853 else if (d->optind == argc)
854 {
855 if (print_errors)
856 {
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800857#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700858 char *buf;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800859
Ying Wang05436632013-04-05 16:01:00 -0700860 if (__asprintf (&buf,
861 _("%s: option requires an argument -- '%c'\n"),
862 argv[0], c) >= 0)
863 {
864 _IO_flockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800865
Ying Wang05436632013-04-05 16:01:00 -0700866 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
867 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800868
Ying Wang05436632013-04-05 16:01:00 -0700869 __fxprintf (NULL, "%s", buf);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800870
Ying Wang05436632013-04-05 16:01:00 -0700871 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
872 _IO_funlockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800873
Ying Wang05436632013-04-05 16:01:00 -0700874 free (buf);
875 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800876#else
Ying Wang05436632013-04-05 16:01:00 -0700877 fprintf (stderr,
878 _("%s: option requires an argument -- '%c'\n"),
879 argv[0], c);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800880#endif
Ying Wang05436632013-04-05 16:01:00 -0700881 }
882 d->optopt = c;
883 if (optstring[0] == ':')
884 c = ':';
885 else
886 c = '?';
887 return c;
888 }
889 else
890 /* We already incremented 'd->optind' once;
891 increment it again when taking next ARGV-elt as argument. */
892 d->optarg = argv[d->optind++];
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800893
Ying Wang05436632013-04-05 16:01:00 -0700894 /* optarg is now the argument, see if it's in the
895 table of longopts. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800896
Ying Wang05436632013-04-05 16:01:00 -0700897 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
898 nameend++)
899 /* Do nothing. */ ;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800900
Ying Wang05436632013-04-05 16:01:00 -0700901 /* Test all long options for either exact match
902 or abbreviated matches. */
903 for (p = longopts, option_index = 0; p->name; p++, option_index++)
904 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
905 {
906 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
907 {
908 /* Exact match found. */
909 pfound = p;
910 indfound = option_index;
911 exact = 1;
912 break;
913 }
914 else if (pfound == NULL)
915 {
916 /* First nonexact match found. */
917 pfound = p;
918 indfound = option_index;
919 }
920 else if (long_only
921 || pfound->has_arg != p->has_arg
922 || pfound->flag != p->flag
923 || pfound->val != p->val)
924 /* Second or later nonexact match found. */
925 ambig = 1;
926 }
927 if (ambig && !exact)
928 {
929 if (print_errors)
930 {
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800931#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700932 char *buf;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800933
Ying Wang05436632013-04-05 16:01:00 -0700934 if (__asprintf (&buf, _("%s: option '-W %s' is ambiguous\n"),
935 argv[0], d->optarg) >= 0)
936 {
937 _IO_flockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800938
Ying Wang05436632013-04-05 16:01:00 -0700939 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
940 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800941
Ying Wang05436632013-04-05 16:01:00 -0700942 __fxprintf (NULL, "%s", buf);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800943
Ying Wang05436632013-04-05 16:01:00 -0700944 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
945 _IO_funlockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800946
Ying Wang05436632013-04-05 16:01:00 -0700947 free (buf);
948 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800949#else
Ying Wang05436632013-04-05 16:01:00 -0700950 fprintf (stderr, _("%s: option '-W %s' is ambiguous\n"),
951 argv[0], d->optarg);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800952#endif
Ying Wang05436632013-04-05 16:01:00 -0700953 }
954 d->__nextchar += strlen (d->__nextchar);
955 d->optind++;
956 return '?';
957 }
958 if (pfound != NULL)
959 {
960 option_index = indfound;
961 if (*nameend)
962 {
963 /* Don't test has_arg with >, because some C compilers don't
964 allow it to be used on enums. */
965 if (pfound->has_arg)
966 d->optarg = nameend + 1;
967 else
968 {
969 if (print_errors)
970 {
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800971#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -0700972 char *buf;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800973
Ying Wang05436632013-04-05 16:01:00 -0700974 if (__asprintf (&buf, _("\
975%s: option '-W %s' doesn't allow an argument\n"),
976 argv[0], pfound->name) >= 0)
977 {
978 _IO_flockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800979
Ying Wang05436632013-04-05 16:01:00 -0700980 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
981 ((_IO_FILE *) stderr)->_flags2
982 |= _IO_FLAGS2_NOTCANCEL;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800983
Ying Wang05436632013-04-05 16:01:00 -0700984 __fxprintf (NULL, "%s", buf);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800985
Ying Wang05436632013-04-05 16:01:00 -0700986 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
987 _IO_funlockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800988
Ying Wang05436632013-04-05 16:01:00 -0700989 free (buf);
990 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800991#else
Ying Wang05436632013-04-05 16:01:00 -0700992 fprintf (stderr, _("\
993%s: option '-W %s' doesn't allow an argument\n"),
994 argv[0], pfound->name);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800995#endif
Ying Wang05436632013-04-05 16:01:00 -0700996 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800997
Ying Wang05436632013-04-05 16:01:00 -0700998 d->__nextchar += strlen (d->__nextchar);
999 return '?';
1000 }
1001 }
1002 else if (pfound->has_arg == 1)
1003 {
1004 if (d->optind < argc)
1005 d->optarg = argv[d->optind++];
1006 else
1007 {
1008 if (print_errors)
1009 {
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001010#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -07001011 char *buf;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001012
Ying Wang05436632013-04-05 16:01:00 -07001013 if (__asprintf (&buf, _("\
1014%s: option '-W %s' requires an argument\n"),
1015 argv[0], pfound->name) >= 0)
1016 {
1017 _IO_flockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001018
Ying Wang05436632013-04-05 16:01:00 -07001019 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1020 ((_IO_FILE *) stderr)->_flags2
1021 |= _IO_FLAGS2_NOTCANCEL;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001022
Ying Wang05436632013-04-05 16:01:00 -07001023 __fxprintf (NULL, "%s", buf);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001024
Ying Wang05436632013-04-05 16:01:00 -07001025 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1026 _IO_funlockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001027
Ying Wang05436632013-04-05 16:01:00 -07001028 free (buf);
1029 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001030#else
Ying Wang05436632013-04-05 16:01:00 -07001031 fprintf (stderr, _("\
1032%s: option '-W %s' requires an argument\n"),
1033 argv[0], pfound->name);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001034#endif
Ying Wang05436632013-04-05 16:01:00 -07001035 }
1036 d->__nextchar += strlen (d->__nextchar);
1037 return optstring[0] == ':' ? ':' : '?';
1038 }
1039 }
1040 else
1041 d->optarg = NULL;
1042 d->__nextchar += strlen (d->__nextchar);
1043 if (longind != NULL)
1044 *longind = option_index;
1045 if (pfound->flag)
1046 {
1047 *(pfound->flag) = pfound->val;
1048 return 0;
1049 }
1050 return pfound->val;
1051 }
1052
1053 no_longs:
1054 d->__nextchar = NULL;
1055 return 'W'; /* Let the application handle it. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001056 }
1057 if (temp[1] == ':')
1058 {
Ying Wang05436632013-04-05 16:01:00 -07001059 if (temp[2] == ':')
1060 {
1061 /* This is an option that accepts an argument optionally. */
1062 if (*d->__nextchar != '\0')
1063 {
1064 d->optarg = d->__nextchar;
1065 d->optind++;
1066 }
1067 else
1068 d->optarg = NULL;
1069 d->__nextchar = NULL;
1070 }
1071 else
1072 {
1073 /* This is an option that requires an argument. */
1074 if (*d->__nextchar != '\0')
1075 {
1076 d->optarg = d->__nextchar;
1077 /* If we end this ARGV-element by taking the rest as an arg,
1078 we must advance to the next element now. */
1079 d->optind++;
1080 }
1081 else if (d->optind == argc)
1082 {
1083 if (print_errors)
1084 {
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001085#if defined _LIBC && defined USE_IN_LIBIO
Ying Wang05436632013-04-05 16:01:00 -07001086 char *buf;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001087
Ying Wang05436632013-04-05 16:01:00 -07001088 if (__asprintf (&buf, _("\
1089%s: option requires an argument -- '%c'\n"),
1090 argv[0], c) >= 0)
1091 {
1092 _IO_flockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001093
Ying Wang05436632013-04-05 16:01:00 -07001094 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1095 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001096
Ying Wang05436632013-04-05 16:01:00 -07001097 __fxprintf (NULL, "%s", buf);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001098
Ying Wang05436632013-04-05 16:01:00 -07001099 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1100 _IO_funlockfile (stderr);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001101
Ying Wang05436632013-04-05 16:01:00 -07001102 free (buf);
1103 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001104#else
Ying Wang05436632013-04-05 16:01:00 -07001105 fprintf (stderr,
1106 _("%s: option requires an argument -- '%c'\n"),
1107 argv[0], c);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001108#endif
Ying Wang05436632013-04-05 16:01:00 -07001109 }
1110 d->optopt = c;
1111 if (optstring[0] == ':')
1112 c = ':';
1113 else
1114 c = '?';
1115 }
1116 else
1117 /* We already incremented 'optind' once;
1118 increment it again when taking next ARGV-elt as argument. */
1119 d->optarg = argv[d->optind++];
1120 d->__nextchar = NULL;
1121 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001122 }
1123 return c;
1124 }
1125}
1126
1127int
1128_getopt_internal (int argc, char **argv, const char *optstring,
Ying Wang05436632013-04-05 16:01:00 -07001129 const struct option *longopts, int *longind, int long_only,
1130 int posixly_correct)
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001131{
1132 int result;
1133
1134 getopt_data.optind = optind;
1135 getopt_data.opterr = opterr;
1136
Ying Wang05436632013-04-05 16:01:00 -07001137 result = _getopt_internal_r (argc, argv, optstring, longopts,
1138 longind, long_only, &getopt_data,
1139 posixly_correct);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001140
1141 optind = getopt_data.optind;
1142 optarg = getopt_data.optarg;
1143 optopt = getopt_data.optopt;
1144
1145 return result;
1146}
1147
1148/* glibc gets a LSB-compliant getopt.
1149 Standalone applications get a POSIX-compliant getopt. */
1150#if _LIBC
1151enum { POSIXLY_CORRECT = 0 };
1152#else
1153enum { POSIXLY_CORRECT = 1 };
1154#endif
1155
1156int
1157getopt (int argc, char *const *argv, const char *optstring)
1158{
Ying Wang05436632013-04-05 16:01:00 -07001159 return _getopt_internal (argc, (char **) argv, optstring,
1160 (const struct option *) 0,
1161 (int *) 0,
1162 0, POSIXLY_CORRECT);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001163}
1164
Ying Wang05436632013-04-05 16:01:00 -07001165#ifdef _LIBC
1166int
1167__posix_getopt (int argc, char *const *argv, const char *optstring)
1168{
1169 return _getopt_internal (argc, argv, optstring,
1170 (const struct option *) 0,
1171 (int *) 0,
1172 0, 1);
1173}
1174#endif
1175
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001176
1177#ifdef TEST
1178
1179/* Compile with -DTEST to make an executable for use in testing
Ying Wang05436632013-04-05 16:01:00 -07001180 the above definition of 'getopt'. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001181
1182int
1183main (int argc, char **argv)
1184{
1185 int c;
1186 int digit_optind = 0;
1187
1188 while (1)
1189 {
1190 int this_option_optind = optind ? optind : 1;
1191
1192 c = getopt (argc, argv, "abc:d:0123456789");
1193 if (c == -1)
Ying Wang05436632013-04-05 16:01:00 -07001194 break;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001195
1196 switch (c)
Ying Wang05436632013-04-05 16:01:00 -07001197 {
1198 case '0':
1199 case '1':
1200 case '2':
1201 case '3':
1202 case '4':
1203 case '5':
1204 case '6':
1205 case '7':
1206 case '8':
1207 case '9':
1208 if (digit_optind != 0 && digit_optind != this_option_optind)
1209 printf ("digits occur in two different argv-elements.\n");
1210 digit_optind = this_option_optind;
1211 printf ("option %c\n", c);
1212 break;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001213
Ying Wang05436632013-04-05 16:01:00 -07001214 case 'a':
1215 printf ("option a\n");
1216 break;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001217
Ying Wang05436632013-04-05 16:01:00 -07001218 case 'b':
1219 printf ("option b\n");
1220 break;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001221
Ying Wang05436632013-04-05 16:01:00 -07001222 case 'c':
1223 printf ("option c with value '%s'\n", optarg);
1224 break;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001225
Ying Wang05436632013-04-05 16:01:00 -07001226 case '?':
1227 break;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001228
Ying Wang05436632013-04-05 16:01:00 -07001229 default:
1230 printf ("?? getopt returned character code 0%o ??\n", c);
1231 }
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001232 }
1233
1234 if (optind < argc)
1235 {
1236 printf ("non-option ARGV-elements: ");
1237 while (optind < argc)
Ying Wang05436632013-04-05 16:01:00 -07001238 printf ("%s ", argv[optind++]);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001239 printf ("\n");
1240 }
1241
1242 exit (0);
1243}
1244
1245#endif /* TEST */