summaryrefslogtreecommitdiffstats
path: root/lib/getopt.c
diff options
context:
space:
mode:
authorTon Voon <tonvoon@users.sourceforge.net>2006-05-18 22:05:43 (GMT)
committerTon Voon <tonvoon@users.sourceforge.net>2006-05-18 22:05:43 (GMT)
commita8cd7705e7898d77764aa12ddcc2e29de1860138 (patch)
tree970f609cbecbe515998907fd55a12f814ad2edcd /lib/getopt.c
parent08394ddb2d62ec8aab9121e45ab9c1a102e85ba4 (diff)
downloadmonitoring-plugins-a8cd7705e7898d77764aa12ddcc2e29de1860138.tar.gz
Synchronise with coreutils 2.95. Gettext now synced with coreutils,
so no longer development platform requirement git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1394 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib/getopt.c')
-rw-r--r--lib/getopt.c540
1 files changed, 286 insertions, 254 deletions
diff --git a/lib/getopt.c b/lib/getopt.c
index 6dcdbeb..bcb81c8 100644
--- a/lib/getopt.c
+++ b/lib/getopt.c
@@ -2,11 +2,8 @@
2 NOTE: getopt is now part of the C library, so if you don't know what 2 NOTE: getopt is now part of the C library, so if you don't know what
3 "Keep this file name-space clean" means, talk to drepper@gnu.org 3 "Keep this file name-space clean" means, talk to drepper@gnu.org
4 before changing it! 4 before changing it!
5 5 Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004
6 Copyright (C) 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 6 Free Software Foundation, Inc.
7 1996, 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation,
8 Inc.
9
10 This file is part of the GNU C Library. 7 This file is part of the GNU C Library.
11 8
12 This program is free software; you can redistribute it and/or modify 9 This program is free software; you can redistribute it and/or modify
@@ -21,7 +18,7 @@
21 18
22 You should have received a copy of the GNU General Public License along 19 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation, 20 with this program; if not, write to the Free Software Foundation,
24 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 21 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
25 22
26/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. 23/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
27 Ditto for AIX 3.2 and <stdlib.h>. */ 24 Ditto for AIX 3.2 and <stdlib.h>. */
@@ -35,25 +32,6 @@
35 32
36#include <stdio.h> 33#include <stdio.h>
37 34
38/* Comment out all this code if we are using the GNU C Library, and are not
39 actually compiling the library itself. This code is part of the GNU C
40 Library, but also included in many other GNU distributions. Compiling
41 and linking in this code is a waste when using the GNU C library
42 (especially if it is a shared library). Rather than having every GNU
43 program understand `configure --with-gnu-libc' and omit the object files,
44 it is simpler to just do this in the source for each such file. */
45
46#define GETOPT_INTERFACE_VERSION 2
47#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
48# include <gnu-versions.h>
49# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
50# define ELIDE_CODE
51# endif
52#endif
53
54#ifndef ELIDE_CODE
55
56
57/* This needs to come after some library #include 35/* This needs to come after some library #include
58 to get __GNU_LIBRARY__ defined. */ 36 to get __GNU_LIBRARY__ defined. */
59#ifdef __GNU_LIBRARY__ 37#ifdef __GNU_LIBRARY__
@@ -72,10 +50,9 @@
72#ifdef _LIBC 50#ifdef _LIBC
73# include <libintl.h> 51# include <libintl.h>
74#else 52#else
75/* This is for other GNU distributions with internationalized messages. */
76# include "gettext.h" 53# include "gettext.h"
54# define _(msgid) gettext (msgid)
77#endif 55#endif
78#define _(msgid) gettext (msgid)
79 56
80#if defined _LIBC && defined USE_IN_LIBIO 57#if defined _LIBC && defined USE_IN_LIBIO
81# include <wchar.h> 58# include <wchar.h>
@@ -85,21 +62,22 @@
85# define attribute_hidden 62# define attribute_hidden
86#endif 63#endif
87 64
88/* This version of `getopt' appears to the caller like standard Unix `getopt' 65/* Unlike standard Unix `getopt', functions like `getopt_long'
89 but it behaves differently for the user, since it allows the user 66 let the user intersperse the options with the other arguments.
90 to intersperse the options with the other arguments.
91 67
92 As `getopt' works, it permutes the elements of ARGV so that, 68 As `getopt_long' works, it permutes the elements of ARGV so that,
93 when it is done, all the options precede everything else. Thus 69 when it is done, all the options precede everything else. Thus
94 all application programs are extended to handle flexible argument order. 70 all application programs are extended to handle flexible argument order.
95 71
96 Setting the environment variable POSIXLY_CORRECT disables permutation. 72 Using `getopt' or setting the environment variable POSIXLY_CORRECT
97 Then the behavior is completely standard. 73 disables permutation.
74 Then the application's behavior is completely standard.
98 75
99 GNU application programs can use a third alternative mode in which 76 GNU application programs can use a third alternative mode in which
100 they can distinguish the relative order of options and other arguments. */ 77 they can distinguish the relative order of options and other arguments. */
101 78
102#include "getopt.h" 79#include "getopt.h"
80#include "getopt_int.h"
103 81
104/* For communication from `getopt' to the caller. 82/* For communication from `getopt' to the caller.
105 When `getopt' finds an option that takes an argument, 83 When `getopt' finds an option that takes an argument,
@@ -124,21 +102,6 @@ char *optarg;
124/* 1003.2 says this must be 1 before any call. */ 102/* 1003.2 says this must be 1 before any call. */
125int optind = 1; 103int optind = 1;
126 104
127/* Formerly, initialization of getopt depended on optind==0, which
128 causes problems with re-calling getopt as programs generally don't
129 know that. */
130
131int __getopt_initialized attribute_hidden;
132
133/* The next char to be scanned in the option-element
134 in which the last option character we returned was found.
135 This allows us to pick up the scan where we left off.
136
137 If this is zero, or a null string, it means resume the scan
138 by advancing to the next ARGV-element. */
139
140static char *nextchar;
141
142/* Callers store zero here to inhibit the error message 105/* Callers store zero here to inhibit the error message
143 for unrecognized options. */ 106 for unrecognized options. */
144 107
@@ -150,44 +113,12 @@ int opterr = 1;
150 113
151int optopt = '?'; 114int optopt = '?';
152 115
153/* Describe how to deal with options that follow non-option ARGV-elements. 116/* Keep a global copy of all internal members of getopt_data. */
154
155 If the caller did not specify anything,
156 the default is REQUIRE_ORDER if the environment variable
157 POSIXLY_CORRECT is defined, PERMUTE otherwise.
158
159 REQUIRE_ORDER means don't recognize them as options;
160 stop option processing when the first non-option is seen.
161 This is what Unix does.
162 This mode of operation is selected by either setting the environment
163 variable POSIXLY_CORRECT, or using `+' as the first character
164 of the list of option characters.
165
166 PERMUTE is the default. We permute the contents of ARGV as we scan,
167 so that eventually all the non-options are at the end. This allows options
168 to be given in any order, even with programs that were not written to
169 expect this.
170
171 RETURN_IN_ORDER is an option available to programs that were written
172 to expect options and other ARGV-elements in any order and that care about
173 the ordering of the two. We describe each non-option ARGV-element
174 as if it were the argument of an option with character code 1.
175 Using `-' as the first character of the list of option characters
176 selects this mode of operation.
177 117
178 The special argument `--' forces an end of option-scanning regardless 118static struct _getopt_data getopt_data;
179 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
180 `--' can cause `getopt' to return -1 with `optind' != ARGC. */
181 119
182static enum
183{
184 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
185} ordering;
186
187/* Value of POSIXLY_CORRECT environment variable. */
188static char *posixly_correct;
189 120
190#ifndef __GNU_LIBRARY__ 121#ifndef __GNU_LIBRARY__
191 122
192/* Avoid depending on library functions or files 123/* Avoid depending on library functions or files
193 whose names are inconsistent. */ 124 whose names are inconsistent. */
@@ -198,15 +129,6 @@ extern char *getenv ();
198 129
199#endif /* not __GNU_LIBRARY__ */ 130#endif /* not __GNU_LIBRARY__ */
200 131
201/* Handle permutation of arguments. */
202
203/* Describe the part of ARGV that contains non-options that have
204 been skipped. `first_nonopt' is the index in ARGV of the first of them;
205 `last_nonopt' is the index after the last of them. */
206
207static int first_nonopt;
208static int last_nonopt;
209
210#ifdef _LIBC 132#ifdef _LIBC
211/* Stored original parameters. 133/* Stored original parameters.
212 XXX This is no good solution. We should rather copy the args so 134 XXX This is no good solution. We should rather copy the args so
@@ -220,14 +142,11 @@ extern char **__libc_argv;
220# ifdef USE_NONOPTION_FLAGS 142# ifdef USE_NONOPTION_FLAGS
221/* Defined in getopt_init.c */ 143/* Defined in getopt_init.c */
222extern char *__getopt_nonoption_flags; 144extern char *__getopt_nonoption_flags;
223
224static int nonoption_flags_max_len;
225static int nonoption_flags_len;
226# endif 145# endif
227 146
228# ifdef USE_NONOPTION_FLAGS 147# ifdef USE_NONOPTION_FLAGS
229# define SWAP_FLAGS(ch1, ch2) \ 148# define SWAP_FLAGS(ch1, ch2) \
230 if (nonoption_flags_len > 0) \ 149 if (d->__nonoption_flags_len > 0) \
231 { \ 150 { \
232 char __tmp = __getopt_nonoption_flags[ch1]; \ 151 char __tmp = __getopt_nonoption_flags[ch1]; \
233 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ 152 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
@@ -250,11 +169,11 @@ static int nonoption_flags_len;
250 the new indices of the non-options in ARGV after they are moved. */ 169 the new indices of the non-options in ARGV after they are moved. */
251 170
252static void 171static void
253exchange (char **argv) 172exchange (char **argv, struct _getopt_data *d)
254{ 173{
255 int bottom = first_nonopt; 174 int bottom = d->__first_nonopt;
256 int middle = last_nonopt; 175 int middle = d->__last_nonopt;
257 int top = optind; 176 int top = d->optind;
258 char *tem; 177 char *tem;
259 178
260 /* Exchange the shorter segment with the far end of the longer segment. 179 /* Exchange the shorter segment with the far end of the longer segment.
@@ -266,19 +185,19 @@ exchange (char **argv)
266 /* First make sure the handling of the `__getopt_nonoption_flags' 185 /* First make sure the handling of the `__getopt_nonoption_flags'
267 string can work normally. Our top argument must be in the range 186 string can work normally. Our top argument must be in the range
268 of the string. */ 187 of the string. */
269 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) 188 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
270 { 189 {
271 /* We must extend the array. The user plays games with us and 190 /* We must extend the array. The user plays games with us and
272 presents new arguments. */ 191 presents new arguments. */
273 char *new_str = malloc (top + 1); 192 char *new_str = malloc (top + 1);
274 if (new_str == NULL) 193 if (new_str == NULL)
275 nonoption_flags_len = nonoption_flags_max_len = 0; 194 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
276 else 195 else
277 { 196 {
278 memset (__mempcpy (new_str, __getopt_nonoption_flags, 197 memset (__mempcpy (new_str, __getopt_nonoption_flags,
279 nonoption_flags_max_len), 198 d->__nonoption_flags_max_len),
280 '\0', top + 1 - nonoption_flags_max_len); 199 '\0', top + 1 - d->__nonoption_flags_max_len);
281 nonoption_flags_max_len = top + 1; 200 d->__nonoption_flags_max_len = top + 1;
282 __getopt_nonoption_flags = new_str; 201 __getopt_nonoption_flags = new_str;
283 } 202 }
284 } 203 }
@@ -324,70 +243,71 @@ exchange (char **argv)
324 243
325 /* Update records for the slots the non-options now occupy. */ 244 /* Update records for the slots the non-options now occupy. */
326 245
327 first_nonopt += (optind - last_nonopt); 246 d->__first_nonopt += (d->optind - d->__last_nonopt);
328 last_nonopt = optind; 247 d->__last_nonopt = d->optind;
329} 248}
330 249
331/* Initialize the internal data when the first call is made. */ 250/* Initialize the internal data when the first call is made. */
332 251
333static const char * 252static const char *
334_getopt_initialize (int argc, char *const *argv, const char *optstring) 253_getopt_initialize (int argc, char **argv, const char *optstring,
254 int posixly_correct, struct _getopt_data *d)
335{ 255{
336 /* Start processing options with ARGV-element 1 (since ARGV-element 0 256 /* Start processing options with ARGV-element 1 (since ARGV-element 0
337 is the program name); the sequence of previously skipped 257 is the program name); the sequence of previously skipped
338 non-option ARGV-elements is empty. */ 258 non-option ARGV-elements is empty. */
339 259
340 first_nonopt = last_nonopt = optind; 260 d->__first_nonopt = d->__last_nonopt = d->optind;
341 261
342 nextchar = NULL; 262 d->__nextchar = NULL;
343 263
344 posixly_correct = getenv ("POSIXLY_CORRECT"); 264 d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
345 265
346 /* Determine how to handle the ordering of options and nonoptions. */ 266 /* Determine how to handle the ordering of options and nonoptions. */
347 267
348 if (optstring[0] == '-') 268 if (optstring[0] == '-')
349 { 269 {
350 ordering = RETURN_IN_ORDER; 270 d->__ordering = RETURN_IN_ORDER;
351 ++optstring; 271 ++optstring;
352 } 272 }
353 else if (optstring[0] == '+') 273 else if (optstring[0] == '+')
354 { 274 {
355 ordering = REQUIRE_ORDER; 275 d->__ordering = REQUIRE_ORDER;
356 ++optstring; 276 ++optstring;
357 } 277 }
358 else if (posixly_correct != NULL) 278 else if (d->__posixly_correct)
359 ordering = REQUIRE_ORDER; 279 d->__ordering = REQUIRE_ORDER;
360 else 280 else
361 ordering = PERMUTE; 281 d->__ordering = PERMUTE;
362 282
363#if defined _LIBC && defined USE_NONOPTION_FLAGS 283#if defined _LIBC && defined USE_NONOPTION_FLAGS
364 if (posixly_correct == NULL 284 if (!d->__posixly_correct
365 && argc == __libc_argc && argv == __libc_argv) 285 && argc == __libc_argc && argv == __libc_argv)
366 { 286 {
367 if (nonoption_flags_max_len == 0) 287 if (d->__nonoption_flags_max_len == 0)
368 { 288 {
369 if (__getopt_nonoption_flags == NULL 289 if (__getopt_nonoption_flags == NULL
370 || __getopt_nonoption_flags[0] == '\0') 290 || __getopt_nonoption_flags[0] == '\0')
371 nonoption_flags_max_len = -1; 291 d->__nonoption_flags_max_len = -1;
372 else 292 else
373 { 293 {
374 const char *orig_str = __getopt_nonoption_flags; 294 const char *orig_str = __getopt_nonoption_flags;
375 int len = nonoption_flags_max_len = strlen (orig_str); 295 int len = d->__nonoption_flags_max_len = strlen (orig_str);
376 if (nonoption_flags_max_len < argc) 296 if (d->__nonoption_flags_max_len < argc)
377 nonoption_flags_max_len = argc; 297 d->__nonoption_flags_max_len = argc;
378 __getopt_nonoption_flags = 298 __getopt_nonoption_flags =
379 (char *) malloc (nonoption_flags_max_len); 299 (char *) malloc (d->__nonoption_flags_max_len);
380 if (__getopt_nonoption_flags == NULL) 300 if (__getopt_nonoption_flags == NULL)
381 nonoption_flags_max_len = -1; 301 d->__nonoption_flags_max_len = -1;
382 else 302 else
383 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len), 303 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
384 '\0', nonoption_flags_max_len - len); 304 '\0', d->__nonoption_flags_max_len - len);
385 } 305 }
386 } 306 }
387 nonoption_flags_len = nonoption_flags_max_len; 307 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
388 } 308 }
389 else 309 else
390 nonoption_flags_len = 0; 310 d->__nonoption_flags_len = 0;
391#endif 311#endif
392 312
393 return optstring; 313 return optstring;
@@ -435,10 +355,6 @@ _getopt_initialize (int argc, char *const *argv, const char *optstring)
435 `flag' field is nonzero, the value of the option's `val' field 355 `flag' field is nonzero, the value of the option's `val' field
436 if the `flag' field is zero. 356 if the `flag' field is zero.
437 357
438 The elements of ARGV aren't really const, because we permute them.
439 But we pretend they're const in the prototype to be compatible
440 with other systems.
441
442 LONGOPTS is a vector of `struct option' terminated by an 358 LONGOPTS is a vector of `struct option' terminated by an
443 element containing a name which is zero. 359 element containing a name which is zero.
444 360
@@ -447,28 +363,32 @@ _getopt_initialize (int argc, char *const *argv, const char *optstring)
447 recent call. 363 recent call.
448 364
449 If LONG_ONLY is nonzero, '-' as well as '--' can introduce 365 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
450 long-named options. */ 366 long-named options.
367
368 If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
369 environment variable were set. */
451 370
452int 371int
453_getopt_internal (int argc, char *const *argv, 372_getopt_internal_r (int argc, char **argv, const char *optstring,
454 const char *optstring, const struct option *longopts, 373 const struct option *longopts, int *longind,
455 int *longind, int long_only) 374 int long_only, int posixly_correct, struct _getopt_data *d)
456{ 375{
457 int print_errors = opterr; 376 int print_errors = d->opterr;
458 if (optstring[0] == ':') 377 if (optstring[0] == ':')
459 print_errors = 0; 378 print_errors = 0;
460 379
461 if (argc < 1) 380 if (argc < 1)
462 return -1; 381 return -1;
463 382
464 optarg = NULL; 383 d->optarg = NULL;
465 384
466 if (optind == 0 || !__getopt_initialized) 385 if (d->optind == 0 || !d->__initialized)
467 { 386 {
468 if (optind == 0) 387 if (d->optind == 0)
469 optind = 1; /* Don't scan ARGV[0], the program name. */ 388 d->optind = 1; /* Don't scan ARGV[0], the program name. */
470 optstring = _getopt_initialize (argc, argv, optstring); 389 optstring = _getopt_initialize (argc, argv, optstring,
471 __getopt_initialized = 1; 390 posixly_correct, d);
391 d->__initialized = 1;
472 } 392 }
473 393
474 /* Test whether ARGV[optind] points to a non-option argument. 394 /* Test whether ARGV[optind] points to a non-option argument.
@@ -476,40 +396,41 @@ _getopt_internal (int argc, char *const *argv,
476 from the shell indicating it is not an option. The later information 396 from the shell indicating it is not an option. The later information
477 is only used when the used in the GNU libc. */ 397 is only used when the used in the GNU libc. */
478#if defined _LIBC && defined USE_NONOPTION_FLAGS 398#if defined _LIBC && defined USE_NONOPTION_FLAGS
479# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ 399# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
480 || (optind < nonoption_flags_len \ 400 || (d->optind < d->__nonoption_flags_len \
481 && __getopt_nonoption_flags[optind] == '1')) 401 && __getopt_nonoption_flags[d->optind] == '1'))
482#else 402#else
483# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') 403# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
484#endif 404#endif
485 405
486 if (nextchar == NULL || *nextchar == '\0') 406 if (d->__nextchar == NULL || *d->__nextchar == '\0')
487 { 407 {
488 /* Advance to the next ARGV-element. */ 408 /* Advance to the next ARGV-element. */
489 409
490 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been 410 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
491 moved back by the user (who may also have changed the arguments). */ 411 moved back by the user (who may also have changed the arguments). */
492 if (last_nonopt > optind) 412 if (d->__last_nonopt > d->optind)
493 last_nonopt = optind; 413 d->__last_nonopt = d->optind;
494 if (first_nonopt > optind) 414 if (d->__first_nonopt > d->optind)
495 first_nonopt = optind; 415 d->__first_nonopt = d->optind;
496 416
497 if (ordering == PERMUTE) 417 if (d->__ordering == PERMUTE)
498 { 418 {
499 /* If we have just processed some options following some non-options, 419 /* If we have just processed some options following some non-options,
500 exchange them so that the options come first. */ 420 exchange them so that the options come first. */
501 421
502 if (first_nonopt != last_nonopt && last_nonopt != optind) 422 if (d->__first_nonopt != d->__last_nonopt
503 exchange ((char **) argv); 423 && d->__last_nonopt != d->optind)
504 else if (last_nonopt != optind) 424 exchange ((char **) argv, d);
505 first_nonopt = optind; 425 else if (d->__last_nonopt != d->optind)
426 d->__first_nonopt = d->optind;
506 427
507 /* Skip any additional non-options 428 /* Skip any additional non-options
508 and extend the range of non-options previously skipped. */ 429 and extend the range of non-options previously skipped. */
509 430
510 while (optind < argc && NONOPTION_P) 431 while (d->optind < argc && NONOPTION_P)
511 optind++; 432 d->optind++;
512 last_nonopt = optind; 433 d->__last_nonopt = d->optind;
513 } 434 }
514 435
515 /* The special ARGV-element `--' means premature end of options. 436 /* The special ARGV-element `--' means premature end of options.
@@ -517,28 +438,29 @@ _getopt_internal (int argc, char *const *argv,
517 then exchange with previous non-options as if it were an option, 438 then exchange with previous non-options as if it were an option,
518 then skip everything else like a non-option. */ 439 then skip everything else like a non-option. */
519 440
520 if (optind != argc && !strcmp (argv[optind], "--")) 441 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
521 { 442 {
522 optind++; 443 d->optind++;
523 444
524 if (first_nonopt != last_nonopt && last_nonopt != optind) 445 if (d->__first_nonopt != d->__last_nonopt
525 exchange ((char **) argv); 446 && d->__last_nonopt != d->optind)
526 else if (first_nonopt == last_nonopt) 447 exchange ((char **) argv, d);
527 first_nonopt = optind; 448 else if (d->__first_nonopt == d->__last_nonopt)
528 last_nonopt = argc; 449 d->__first_nonopt = d->optind;
450 d->__last_nonopt = argc;
529 451
530 optind = argc; 452 d->optind = argc;
531 } 453 }
532 454
533 /* If we have done all the ARGV-elements, stop the scan 455 /* If we have done all the ARGV-elements, stop the scan
534 and back over any non-options that we skipped and permuted. */ 456 and back over any non-options that we skipped and permuted. */
535 457
536 if (optind == argc) 458 if (d->optind == argc)
537 { 459 {
538 /* Set the next-arg-index to point at the non-options 460 /* Set the next-arg-index to point at the non-options
539 that we previously skipped, so the caller will digest them. */ 461 that we previously skipped, so the caller will digest them. */
540 if (first_nonopt != last_nonopt) 462 if (d->__first_nonopt != d->__last_nonopt)
541 optind = first_nonopt; 463 d->optind = d->__first_nonopt;
542 return -1; 464 return -1;
543 } 465 }
544 466
@@ -547,17 +469,17 @@ _getopt_internal (int argc, char *const *argv,
547 469
548 if (NONOPTION_P) 470 if (NONOPTION_P)
549 { 471 {
550 if (ordering == REQUIRE_ORDER) 472 if (d->__ordering == REQUIRE_ORDER)
551 return -1; 473 return -1;
552 optarg = argv[optind++]; 474 d->optarg = argv[d->optind++];
553 return 1; 475 return 1;
554 } 476 }
555 477
556 /* We have found another option-ARGV-element. 478 /* We have found another option-ARGV-element.
557 Skip the initial punctuation. */ 479 Skip the initial punctuation. */
558 480
559 nextchar = (argv[optind] + 1 481 d->__nextchar = (argv[d->optind] + 1
560 + (longopts != NULL && argv[optind][1] == '-')); 482 + (longopts != NULL && argv[d->optind][1] == '-'));
561 } 483 }
562 484
563 /* Decode the current option-ARGV-element. */ 485 /* Decode the current option-ARGV-element. */
@@ -576,9 +498,9 @@ _getopt_internal (int argc, char *const *argv,
576 This distinction seems to be the most useful approach. */ 498 This distinction seems to be the most useful approach. */
577 499
578 if (longopts != NULL 500 if (longopts != NULL
579 && (argv[optind][1] == '-' 501 && (argv[d->optind][1] == '-'
580 || (long_only 502 || (long_only && (argv[d->optind][2]
581 && (argv[optind][2] || !strchr (optstring, argv[optind][1]))))) 503 || !strchr (optstring, argv[d->optind][1])))))
582 { 504 {
583 char *nameend; 505 char *nameend;
584 const struct option *p; 506 const struct option *p;
@@ -588,15 +510,15 @@ _getopt_internal (int argc, char *const *argv,
588 int indfound = -1; 510 int indfound = -1;
589 int option_index; 511 int option_index;
590 512
591 for (nameend = nextchar; *nameend && *nameend != '='; nameend++) 513 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
592 /* Do nothing. */ ; 514 /* Do nothing. */ ;
593 515
594 /* Test all long options for either exact match 516 /* Test all long options for either exact match
595 or abbreviated matches. */ 517 or abbreviated matches. */
596 for (p = longopts, option_index = 0; p->name; p++, option_index++) 518 for (p = longopts, option_index = 0; p->name; p++, option_index++)
597 if (!strncmp (p->name, nextchar, nameend - nextchar)) 519 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
598 { 520 {
599 if ((unsigned int) (nameend - nextchar) 521 if ((unsigned int) (nameend - d->__nextchar)
600 == (unsigned int) strlen (p->name)) 522 == (unsigned int) strlen (p->name))
601 { 523 {
602 /* Exact match found. */ 524 /* Exact match found. */
@@ -627,37 +549,44 @@ _getopt_internal (int argc, char *const *argv,
627 char *buf; 549 char *buf;
628 550
629 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"), 551 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
630 argv[0], argv[optind]) >= 0) 552 argv[0], argv[d->optind]) >= 0)
631 { 553 {
554 _IO_flockfile (stderr);
555
556 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
557 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
632 558
633 if (_IO_fwide (stderr, 0) > 0) 559 if (_IO_fwide (stderr, 0) > 0)
634 __fwprintf (stderr, L"%s", buf); 560 __fwprintf (stderr, L"%s", buf);
635 else 561 else
636 fputs (buf, stderr); 562 fputs (buf, stderr);
637 563
564 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
565 _IO_funlockfile (stderr);
566
638 free (buf); 567 free (buf);
639 } 568 }
640#else 569#else
641 fprintf (stderr, _("%s: option `%s' is ambiguous\n"), 570 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
642 argv[0], argv[optind]); 571 argv[0], argv[d->optind]);
643#endif 572#endif
644 } 573 }
645 nextchar += strlen (nextchar); 574 d->__nextchar += strlen (d->__nextchar);
646 optind++; 575 d->optind++;
647 optopt = 0; 576 d->optopt = 0;
648 return '?'; 577 return '?';
649 } 578 }
650 579
651 if (pfound != NULL) 580 if (pfound != NULL)
652 { 581 {
653 option_index = indfound; 582 option_index = indfound;
654 optind++; 583 d->optind++;
655 if (*nameend) 584 if (*nameend)
656 { 585 {
657 /* Don't test has_arg with >, because some C compilers don't 586 /* Don't test has_arg with >, because some C compilers don't
658 allow it to be used on enums. */ 587 allow it to be used on enums. */
659 if (pfound->has_arg) 588 if (pfound->has_arg)
660 optarg = nameend + 1; 589 d->optarg = nameend + 1;
661 else 590 else
662 { 591 {
663 if (print_errors) 592 if (print_errors)
@@ -667,7 +596,7 @@ _getopt_internal (int argc, char *const *argv,
667 int n; 596 int n;
668#endif 597#endif
669 598
670 if (argv[optind - 1][1] == '-') 599 if (argv[d->optind - 1][1] == '-')
671 { 600 {
672 /* --option */ 601 /* --option */
673#if defined _LIBC && defined USE_IN_LIBIO 602#if defined _LIBC && defined USE_IN_LIBIO
@@ -686,38 +615,48 @@ _getopt_internal (int argc, char *const *argv,
686#if defined _LIBC && defined USE_IN_LIBIO 615#if defined _LIBC && defined USE_IN_LIBIO
687 n = __asprintf (&buf, _("\ 616 n = __asprintf (&buf, _("\
688%s: option `%c%s' doesn't allow an argument\n"), 617%s: option `%c%s' doesn't allow an argument\n"),
689 argv[0], argv[optind - 1][0], 618 argv[0], argv[d->optind - 1][0],
690 pfound->name); 619 pfound->name);
691#else 620#else
692 fprintf (stderr, _("\ 621 fprintf (stderr, _("\
693%s: option `%c%s' doesn't allow an argument\n"), 622%s: option `%c%s' doesn't allow an argument\n"),
694 argv[0], argv[optind - 1][0], pfound->name); 623 argv[0], argv[d->optind - 1][0],
624 pfound->name);
695#endif 625#endif
696 } 626 }
697 627
698#if defined _LIBC && defined USE_IN_LIBIO 628#if defined _LIBC && defined USE_IN_LIBIO
699 if (n >= 0) 629 if (n >= 0)
700 { 630 {
631 _IO_flockfile (stderr);
632
633 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
634 ((_IO_FILE *) stderr)->_flags2
635 |= _IO_FLAGS2_NOTCANCEL;
636
701 if (_IO_fwide (stderr, 0) > 0) 637 if (_IO_fwide (stderr, 0) > 0)
702 __fwprintf (stderr, L"%s", buf); 638 __fwprintf (stderr, L"%s", buf);
703 else 639 else
704 fputs (buf, stderr); 640 fputs (buf, stderr);
705 641
642 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
643 _IO_funlockfile (stderr);
644
706 free (buf); 645 free (buf);
707 } 646 }
708#endif 647#endif
709 } 648 }
710 649
711 nextchar += strlen (nextchar); 650 d->__nextchar += strlen (d->__nextchar);
712 651
713 optopt = pfound->val; 652 d->optopt = pfound->val;
714 return '?'; 653 return '?';
715 } 654 }
716 } 655 }
717 else if (pfound->has_arg == 1) 656 else if (pfound->has_arg == 1)
718 { 657 {
719 if (optind < argc) 658 if (d->optind < argc)
720 optarg = argv[optind++]; 659 d->optarg = argv[d->optind++];
721 else 660 else
722 { 661 {
723 if (print_errors) 662 if (print_errors)
@@ -727,27 +666,36 @@ _getopt_internal (int argc, char *const *argv,
727 666
728 if (__asprintf (&buf, _("\ 667 if (__asprintf (&buf, _("\
729%s: option `%s' requires an argument\n"), 668%s: option `%s' requires an argument\n"),
730 argv[0], argv[optind - 1]) >= 0) 669 argv[0], argv[d->optind - 1]) >= 0)
731 { 670 {
671 _IO_flockfile (stderr);
672
673 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
674 ((_IO_FILE *) stderr)->_flags2
675 |= _IO_FLAGS2_NOTCANCEL;
676
732 if (_IO_fwide (stderr, 0) > 0) 677 if (_IO_fwide (stderr, 0) > 0)
733 __fwprintf (stderr, L"%s", buf); 678 __fwprintf (stderr, L"%s", buf);
734 else 679 else
735 fputs (buf, stderr); 680 fputs (buf, stderr);
736 681
682 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
683 _IO_funlockfile (stderr);
684
737 free (buf); 685 free (buf);
738 } 686 }
739#else 687#else
740 fprintf (stderr, 688 fprintf (stderr,
741 _("%s: option `%s' requires an argument\n"), 689 _("%s: option `%s' requires an argument\n"),
742 argv[0], argv[optind - 1]); 690 argv[0], argv[d->optind - 1]);
743#endif 691#endif
744 } 692 }
745 nextchar += strlen (nextchar); 693 d->__nextchar += strlen (d->__nextchar);
746 optopt = pfound->val; 694 d->optopt = pfound->val;
747 return optstring[0] == ':' ? ':' : '?'; 695 return optstring[0] == ':' ? ':' : '?';
748 } 696 }
749 } 697 }
750 nextchar += strlen (nextchar); 698 d->__nextchar += strlen (d->__nextchar);
751 if (longind != NULL) 699 if (longind != NULL)
752 *longind = option_index; 700 *longind = option_index;
753 if (pfound->flag) 701 if (pfound->flag)
@@ -762,8 +710,8 @@ _getopt_internal (int argc, char *const *argv,
762 or the option starts with '--' or is not a valid short 710 or the option starts with '--' or is not a valid short
763 option, then it's an error. 711 option, then it's an error.
764 Otherwise interpret it as a short option. */ 712 Otherwise interpret it as a short option. */
765 if (!long_only || argv[optind][1] == '-' 713 if (!long_only || argv[d->optind][1] == '-'
766 || strchr (optstring, *nextchar) == NULL) 714 || strchr (optstring, *d->__nextchar) == NULL)
767 { 715 {
768 if (print_errors) 716 if (print_errors)
769 { 717 {
@@ -772,15 +720,15 @@ _getopt_internal (int argc, char *const *argv,
772 int n; 720 int n;
773#endif 721#endif
774 722
775 if (argv[optind][1] == '-') 723 if (argv[d->optind][1] == '-')
776 { 724 {
777 /* --option */ 725 /* --option */
778#if defined _LIBC && defined USE_IN_LIBIO 726#if defined _LIBC && defined USE_IN_LIBIO
779 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"), 727 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
780 argv[0], nextchar); 728 argv[0], d->__nextchar);
781#else 729#else
782 fprintf (stderr, _("%s: unrecognized option `--%s'\n"), 730 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
783 argv[0], nextchar); 731 argv[0], d->__nextchar);
784#endif 732#endif
785 } 733 }
786 else 734 else
@@ -788,28 +736,36 @@ _getopt_internal (int argc, char *const *argv,
788 /* +option or -option */ 736 /* +option or -option */
789#if defined _LIBC && defined USE_IN_LIBIO 737#if defined _LIBC && defined USE_IN_LIBIO
790 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"), 738 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
791 argv[0], argv[optind][0], nextchar); 739 argv[0], argv[d->optind][0], d->__nextchar);
792#else 740#else
793 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), 741 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
794 argv[0], argv[optind][0], nextchar); 742 argv[0], argv[d->optind][0], d->__nextchar);
795#endif 743#endif
796 } 744 }
797 745
798#if defined _LIBC && defined USE_IN_LIBIO 746#if defined _LIBC && defined USE_IN_LIBIO
799 if (n >= 0) 747 if (n >= 0)
800 { 748 {
749 _IO_flockfile (stderr);
750
751 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
752 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
753
801 if (_IO_fwide (stderr, 0) > 0) 754 if (_IO_fwide (stderr, 0) > 0)
802 __fwprintf (stderr, L"%s", buf); 755 __fwprintf (stderr, L"%s", buf);
803 else 756 else
804 fputs (buf, stderr); 757 fputs (buf, stderr);
805 758
759 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
760 _IO_funlockfile (stderr);
761
806 free (buf); 762 free (buf);
807 } 763 }
808#endif 764#endif
809 } 765 }
810 nextchar = (char *) ""; 766 d->__nextchar = (char *) "";
811 optind++; 767 d->optind++;
812 optopt = 0; 768 d->optopt = 0;
813 return '?'; 769 return '?';
814 } 770 }
815 } 771 }
@@ -817,12 +773,12 @@ _getopt_internal (int argc, char *const *argv,
817 /* Look at and handle the next short option-character. */ 773 /* Look at and handle the next short option-character. */
818 774
819 { 775 {
820 char c = *nextchar++; 776 char c = *d->__nextchar++;
821 char *temp = strchr (optstring, c); 777 char *temp = strchr (optstring, c);
822 778
823 /* Increment `optind' when we start to process its last character. */ 779 /* Increment `optind' when we start to process its last character. */
824 if (*nextchar == '\0') 780 if (*d->__nextchar == '\0')
825 ++optind; 781 ++d->optind;
826 782
827 if (temp == NULL || c == ':') 783 if (temp == NULL || c == ':')
828 { 784 {
@@ -833,7 +789,7 @@ _getopt_internal (int argc, char *const *argv,
833 int n; 789 int n;
834#endif 790#endif
835 791
836 if (posixly_correct) 792 if (d->__posixly_correct)
837 { 793 {
838 /* 1003.2 specifies the format of this message. */ 794 /* 1003.2 specifies the format of this message. */
839#if defined _LIBC && defined USE_IN_LIBIO 795#if defined _LIBC && defined USE_IN_LIBIO
@@ -856,16 +812,24 @@ _getopt_internal (int argc, char *const *argv,
856#if defined _LIBC && defined USE_IN_LIBIO 812#if defined _LIBC && defined USE_IN_LIBIO
857 if (n >= 0) 813 if (n >= 0)
858 { 814 {
815 _IO_flockfile (stderr);
816
817 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
818 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
819
859 if (_IO_fwide (stderr, 0) > 0) 820 if (_IO_fwide (stderr, 0) > 0)
860 __fwprintf (stderr, L"%s", buf); 821 __fwprintf (stderr, L"%s", buf);
861 else 822 else
862 fputs (buf, stderr); 823 fputs (buf, stderr);
863 824
825 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
826 _IO_funlockfile (stderr);
827
864 free (buf); 828 free (buf);
865 } 829 }
866#endif 830#endif
867 } 831 }
868 optopt = c; 832 d->optopt = c;
869 return '?'; 833 return '?';
870 } 834 }
871 /* Convenience. Treat POSIX -W foo same as long option --foo */ 835 /* Convenience. Treat POSIX -W foo same as long option --foo */
@@ -880,14 +844,14 @@ _getopt_internal (int argc, char *const *argv,
880 int option_index; 844 int option_index;
881 845
882 /* This is an option that requires an argument. */ 846 /* This is an option that requires an argument. */
883 if (*nextchar != '\0') 847 if (*d->__nextchar != '\0')
884 { 848 {
885 optarg = nextchar; 849 d->optarg = d->__nextchar;
886 /* If we end this ARGV-element by taking the rest as an arg, 850 /* If we end this ARGV-element by taking the rest as an arg,
887 we must advance to the next element now. */ 851 we must advance to the next element now. */
888 optind++; 852 d->optind++;
889 } 853 }
890 else if (optind == argc) 854 else if (d->optind == argc)
891 { 855 {
892 if (print_errors) 856 if (print_errors)
893 { 857 {
@@ -899,11 +863,19 @@ _getopt_internal (int argc, char *const *argv,
899 _("%s: option requires an argument -- %c\n"), 863 _("%s: option requires an argument -- %c\n"),
900 argv[0], c) >= 0) 864 argv[0], c) >= 0)
901 { 865 {
866 _IO_flockfile (stderr);
867
868 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
869 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
870
902 if (_IO_fwide (stderr, 0) > 0) 871 if (_IO_fwide (stderr, 0) > 0)
903 __fwprintf (stderr, L"%s", buf); 872 __fwprintf (stderr, L"%s", buf);
904 else 873 else
905 fputs (buf, stderr); 874 fputs (buf, stderr);
906 875
876 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
877 _IO_funlockfile (stderr);
878
907 free (buf); 879 free (buf);
908 } 880 }
909#else 881#else
@@ -911,7 +883,7 @@ _getopt_internal (int argc, char *const *argv,
911 argv[0], c); 883 argv[0], c);
912#endif 884#endif
913 } 885 }
914 optopt = c; 886 d->optopt = c;
915 if (optstring[0] == ':') 887 if (optstring[0] == ':')
916 c = ':'; 888 c = ':';
917 else 889 else
@@ -919,22 +891,23 @@ _getopt_internal (int argc, char *const *argv,
919 return c; 891 return c;
920 } 892 }
921 else 893 else
922 /* We already incremented `optind' once; 894 /* We already incremented `d->optind' once;
923 increment it again when taking next ARGV-elt as argument. */ 895 increment it again when taking next ARGV-elt as argument. */
924 optarg = argv[optind++]; 896 d->optarg = argv[d->optind++];
925 897
926 /* optarg is now the argument, see if it's in the 898 /* optarg is now the argument, see if it's in the
927 table of longopts. */ 899 table of longopts. */
928 900
929 for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) 901 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
902 nameend++)
930 /* Do nothing. */ ; 903 /* Do nothing. */ ;
931 904
932 /* Test all long options for either exact match 905 /* Test all long options for either exact match
933 or abbreviated matches. */ 906 or abbreviated matches. */
934 for (p = longopts, option_index = 0; p->name; p++, option_index++) 907 for (p = longopts, option_index = 0; p->name; p++, option_index++)
935 if (!strncmp (p->name, nextchar, nameend - nextchar)) 908 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
936 { 909 {
937 if ((unsigned int) (nameend - nextchar) == strlen (p->name)) 910 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
938 { 911 {
939 /* Exact match found. */ 912 /* Exact match found. */
940 pfound = p; 913 pfound = p;
@@ -960,22 +933,30 @@ _getopt_internal (int argc, char *const *argv,
960 char *buf; 933 char *buf;
961 934
962 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"), 935 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
963 argv[0], argv[optind]) >= 0) 936 argv[0], argv[d->optind]) >= 0)
964 { 937 {
938 _IO_flockfile (stderr);
939
940 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
941 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
942
965 if (_IO_fwide (stderr, 0) > 0) 943 if (_IO_fwide (stderr, 0) > 0)
966 __fwprintf (stderr, L"%s", buf); 944 __fwprintf (stderr, L"%s", buf);
967 else 945 else
968 fputs (buf, stderr); 946 fputs (buf, stderr);
969 947
948 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
949 _IO_funlockfile (stderr);
950
970 free (buf); 951 free (buf);
971 } 952 }
972#else 953#else
973 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), 954 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
974 argv[0], argv[optind]); 955 argv[0], argv[d->optind]);
975#endif 956#endif
976 } 957 }
977 nextchar += strlen (nextchar); 958 d->__nextchar += strlen (d->__nextchar);
978 optind++; 959 d->optind++;
979 return '?'; 960 return '?';
980 } 961 }
981 if (pfound != NULL) 962 if (pfound != NULL)
@@ -986,7 +967,7 @@ _getopt_internal (int argc, char *const *argv,
986 /* Don't test has_arg with >, because some C compilers don't 967 /* Don't test has_arg with >, because some C compilers don't
987 allow it to be used on enums. */ 968 allow it to be used on enums. */
988 if (pfound->has_arg) 969 if (pfound->has_arg)
989 optarg = nameend + 1; 970 d->optarg = nameend + 1;
990 else 971 else
991 { 972 {
992 if (print_errors) 973 if (print_errors)
@@ -998,11 +979,20 @@ _getopt_internal (int argc, char *const *argv,
998%s: option `-W %s' doesn't allow an argument\n"), 979%s: option `-W %s' doesn't allow an argument\n"),
999 argv[0], pfound->name) >= 0) 980 argv[0], pfound->name) >= 0)
1000 { 981 {
982 _IO_flockfile (stderr);
983
984 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
985 ((_IO_FILE *) stderr)->_flags2
986 |= _IO_FLAGS2_NOTCANCEL;
987
1001 if (_IO_fwide (stderr, 0) > 0) 988 if (_IO_fwide (stderr, 0) > 0)
1002 __fwprintf (stderr, L"%s", buf); 989 __fwprintf (stderr, L"%s", buf);
1003 else 990 else
1004 fputs (buf, stderr); 991 fputs (buf, stderr);
1005 992
993 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
994 _IO_funlockfile (stderr);
995
1006 free (buf); 996 free (buf);
1007 } 997 }
1008#else 998#else
@@ -1012,14 +1002,14 @@ _getopt_internal (int argc, char *const *argv,
1012#endif 1002#endif
1013 } 1003 }
1014 1004
1015 nextchar += strlen (nextchar); 1005 d->__nextchar += strlen (d->__nextchar);
1016 return '?'; 1006 return '?';
1017 } 1007 }
1018 } 1008 }
1019 else if (pfound->has_arg == 1) 1009 else if (pfound->has_arg == 1)
1020 { 1010 {
1021 if (optind < argc) 1011 if (d->optind < argc)
1022 optarg = argv[optind++]; 1012 d->optarg = argv[d->optind++];
1023 else 1013 else
1024 { 1014 {
1025 if (print_errors) 1015 if (print_errors)
@@ -1029,26 +1019,35 @@ _getopt_internal (int argc, char *const *argv,
1029 1019
1030 if (__asprintf (&buf, _("\ 1020 if (__asprintf (&buf, _("\
1031%s: option `%s' requires an argument\n"), 1021%s: option `%s' requires an argument\n"),
1032 argv[0], argv[optind - 1]) >= 0) 1022 argv[0], argv[d->optind - 1]) >= 0)
1033 { 1023 {
1024 _IO_flockfile (stderr);
1025
1026 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1027 ((_IO_FILE *) stderr)->_flags2
1028 |= _IO_FLAGS2_NOTCANCEL;
1029
1034 if (_IO_fwide (stderr, 0) > 0) 1030 if (_IO_fwide (stderr, 0) > 0)
1035 __fwprintf (stderr, L"%s", buf); 1031 __fwprintf (stderr, L"%s", buf);
1036 else 1032 else
1037 fputs (buf, stderr); 1033 fputs (buf, stderr);
1038 1034
1035 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1036 _IO_funlockfile (stderr);
1037
1039 free (buf); 1038 free (buf);
1040 } 1039 }
1041#else 1040#else
1042 fprintf (stderr, 1041 fprintf (stderr,
1043 _("%s: option `%s' requires an argument\n"), 1042 _("%s: option `%s' requires an argument\n"),
1044 argv[0], argv[optind - 1]); 1043 argv[0], argv[d->optind - 1]);
1045#endif 1044#endif
1046 } 1045 }
1047 nextchar += strlen (nextchar); 1046 d->__nextchar += strlen (d->__nextchar);
1048 return optstring[0] == ':' ? ':' : '?'; 1047 return optstring[0] == ':' ? ':' : '?';
1049 } 1048 }
1050 } 1049 }
1051 nextchar += strlen (nextchar); 1050 d->__nextchar += strlen (d->__nextchar);
1052 if (longind != NULL) 1051 if (longind != NULL)
1053 *longind = option_index; 1052 *longind = option_index;
1054 if (pfound->flag) 1053 if (pfound->flag)
@@ -1058,7 +1057,7 @@ _getopt_internal (int argc, char *const *argv,
1058 } 1057 }
1059 return pfound->val; 1058 return pfound->val;
1060 } 1059 }
1061 nextchar = NULL; 1060 d->__nextchar = NULL;
1062 return 'W'; /* Let the application handle it. */ 1061 return 'W'; /* Let the application handle it. */
1063 } 1062 }
1064 if (temp[1] == ':') 1063 if (temp[1] == ':')
@@ -1066,26 +1065,26 @@ _getopt_internal (int argc, char *const *argv,
1066 if (temp[2] == ':') 1065 if (temp[2] == ':')
1067 { 1066 {
1068 /* This is an option that accepts an argument optionally. */ 1067 /* This is an option that accepts an argument optionally. */
1069 if (*nextchar != '\0') 1068 if (*d->__nextchar != '\0')
1070 { 1069 {
1071 optarg = nextchar; 1070 d->optarg = d->__nextchar;
1072 optind++; 1071 d->optind++;
1073 } 1072 }
1074 else 1073 else
1075 optarg = NULL; 1074 d->optarg = NULL;
1076 nextchar = NULL; 1075 d->__nextchar = NULL;
1077 } 1076 }
1078 else 1077 else
1079 { 1078 {
1080 /* This is an option that requires an argument. */ 1079 /* This is an option that requires an argument. */
1081 if (*nextchar != '\0') 1080 if (*d->__nextchar != '\0')
1082 { 1081 {
1083 optarg = nextchar; 1082 d->optarg = d->__nextchar;
1084 /* If we end this ARGV-element by taking the rest as an arg, 1083 /* If we end this ARGV-element by taking the rest as an arg,
1085 we must advance to the next element now. */ 1084 we must advance to the next element now. */
1086 optind++; 1085 d->optind++;
1087 } 1086 }
1088 else if (optind == argc) 1087 else if (d->optind == argc)
1089 { 1088 {
1090 if (print_errors) 1089 if (print_errors)
1091 { 1090 {
@@ -1097,11 +1096,19 @@ _getopt_internal (int argc, char *const *argv,
1097%s: option requires an argument -- %c\n"), 1096%s: option requires an argument -- %c\n"),
1098 argv[0], c) >= 0) 1097 argv[0], c) >= 0)
1099 { 1098 {
1099 _IO_flockfile (stderr);
1100
1101 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1102 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1103
1100 if (_IO_fwide (stderr, 0) > 0) 1104 if (_IO_fwide (stderr, 0) > 0)
1101 __fwprintf (stderr, L"%s", buf); 1105 __fwprintf (stderr, L"%s", buf);
1102 else 1106 else
1103 fputs (buf, stderr); 1107 fputs (buf, stderr);
1104 1108
1109 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1110 _IO_funlockfile (stderr);
1111
1105 free (buf); 1112 free (buf);
1106 } 1113 }
1107#else 1114#else
@@ -1110,7 +1117,7 @@ _getopt_internal (int argc, char *const *argv,
1110 argv[0], c); 1117 argv[0], c);
1111#endif 1118#endif
1112 } 1119 }
1113 optopt = c; 1120 d->optopt = c;
1114 if (optstring[0] == ':') 1121 if (optstring[0] == ':')
1115 c = ':'; 1122 c = ':';
1116 else 1123 else
@@ -1119,8 +1126,8 @@ _getopt_internal (int argc, char *const *argv,
1119 else 1126 else
1120 /* We already incremented `optind' once; 1127 /* We already incremented `optind' once;
1121 increment it again when taking next ARGV-elt as argument. */ 1128 increment it again when taking next ARGV-elt as argument. */
1122 optarg = argv[optind++]; 1129 d->optarg = argv[d->optind++];
1123 nextchar = NULL; 1130 d->__nextchar = NULL;
1124 } 1131 }
1125 } 1132 }
1126 return c; 1133 return c;
@@ -1128,15 +1135,40 @@ _getopt_internal (int argc, char *const *argv,
1128} 1135}
1129 1136
1130int 1137int
1138_getopt_internal (int argc, char **argv, const char *optstring,
1139 const struct option *longopts, int *longind,
1140 int long_only, int posixly_correct)
1141{
1142 int result;
1143
1144 getopt_data.optind = optind;
1145 getopt_data.opterr = opterr;
1146
1147 result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
1148 long_only, posixly_correct, &getopt_data);
1149
1150 optind = getopt_data.optind;
1151 optarg = getopt_data.optarg;
1152 optopt = getopt_data.optopt;
1153
1154 return result;
1155}
1156
1157/* glibc gets a LSB-compliant getopt.
1158 Standalone applications get a POSIX-compliant getopt. */
1159#if _LIBC
1160enum { POSIXLY_CORRECT = 0 };
1161#else
1162enum { POSIXLY_CORRECT = 1 };
1163#endif
1164
1165int
1131getopt (int argc, char *const *argv, const char *optstring) 1166getopt (int argc, char *const *argv, const char *optstring)
1132{ 1167{
1133 return _getopt_internal (argc, argv, optstring, 1168 return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
1134 (const struct option *) 0, 1169 POSIXLY_CORRECT);
1135 (int *) 0,
1136 0);
1137} 1170}
1138 1171
1139#endif /* Not ELIDE_CODE. */
1140 1172
1141#ifdef TEST 1173#ifdef TEST
1142 1174