00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _NO_PROTO
00027 # define _NO_PROTO
00028 #endif
00029
00030 #ifdef HAVE_CONFIG_H
00031 # include <config.h>
00032 #endif
00033
00034 #include <stdio.h>
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #define GETOPT_INTERFACE_VERSION 2
00045 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
00046 # include <gnu-versions.h>
00047 # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
00048 # define ELIDE_CODE
00049 # endif
00050 #endif
00051
00052 #ifndef ELIDE_CODE
00053
00054
00055
00056
00057 #ifdef __GNU_LIBRARY__
00058
00059
00060 # include <stdlib.h>
00061 # include <unistd.h>
00062 #endif
00063
00064 #include <string.h>
00065
00066 #ifdef VMS
00067 # include <unixlib.h>
00068 #endif
00069
00070 #ifdef _LIBC
00071 # include <libintl.h>
00072 #else
00073
00074
00075 #endif
00076
00077 #if defined _LIBC && defined USE_IN_LIBIO
00078 # include <wchar.h>
00079 #endif
00080
00081 #ifndef attribute_hidden
00082 # define attribute_hidden
00083 #endif
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 #include "getopt.h"
00100 #include "getopt_int.h"
00101
00102
00103
00104
00105
00106
00107
00108 char *optarg;
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 int optind = 1;
00124
00125
00126
00127
00128 int opterr = 1;
00129
00130
00131
00132
00133
00134 int optopt = '?';
00135
00136
00137
00138 static struct _getopt_data getopt_data;
00139
00140 #ifndef __GNU_LIBRARY__
00141
00142
00143
00144
00145 #ifndef getenv
00146 extern char *getenv ();
00147 #endif
00148
00149 #endif
00150
00151 #ifdef _LIBC
00152
00153
00154
00155 extern int __libc_argc;
00156 extern char **__libc_argv;
00157
00158
00159
00160
00161 # ifdef USE_NONOPTION_FLAGS
00162
00163 extern char *__getopt_nonoption_flags;
00164 # endif
00165
00166 # ifdef USE_NONOPTION_FLAGS
00167 # define SWAP_FLAGS(ch1, ch2) \
00168 if (d->__nonoption_flags_len > 0) \
00169 { \
00170 char __tmp = __getopt_nonoption_flags[ch1]; \
00171 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
00172 __getopt_nonoption_flags[ch2] = __tmp; \
00173 }
00174 # else
00175 # define SWAP_FLAGS(ch1, ch2)
00176 # endif
00177 #else
00178 # define SWAP_FLAGS(ch1, ch2)
00179 #endif
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 static void
00191 exchange (char **argv, struct _getopt_data *d)
00192 {
00193 int bottom = d->__first_nonopt;
00194 int middle = d->__last_nonopt;
00195 int top = d->optind;
00196 char *tem;
00197
00198
00199
00200
00201
00202
00203 #if defined _LIBC && defined USE_NONOPTION_FLAGS
00204
00205
00206
00207 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
00208 {
00209
00210
00211 char *new_str = malloc (top + 1);
00212 if (new_str == NULL)
00213 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
00214 else
00215 {
00216 memset (__mempcpy (new_str, __getopt_nonoption_flags,
00217 d->__nonoption_flags_max_len),
00218 '\0', top + 1 - d->__nonoption_flags_max_len);
00219 d->__nonoption_flags_max_len = top + 1;
00220 __getopt_nonoption_flags = new_str;
00221 }
00222 }
00223 #endif
00224
00225 while (top > middle && middle > bottom)
00226 {
00227 if (top - middle > middle - bottom)
00228 {
00229
00230 int len = middle - bottom;
00231 register int i;
00232
00233
00234 for (i = 0; i < len; i++)
00235 {
00236 tem = argv[bottom + i];
00237 argv[bottom + i] = argv[top - (middle - bottom) + i];
00238 argv[top - (middle - bottom) + i] = tem;
00239 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
00240 }
00241
00242 top -= len;
00243 }
00244 else
00245 {
00246
00247 int len = top - middle;
00248 register int i;
00249
00250
00251 for (i = 0; i < len; i++)
00252 {
00253 tem = argv[bottom + i];
00254 argv[bottom + i] = argv[middle + i];
00255 argv[middle + i] = tem;
00256 SWAP_FLAGS (bottom + i, middle + i);
00257 }
00258
00259 bottom += len;
00260 }
00261 }
00262
00263
00264
00265 d->__first_nonopt += (d->optind - d->__last_nonopt);
00266 d->__last_nonopt = d->optind;
00267 }
00268
00269
00270
00271 static const char *
00272 _getopt_initialize (int argc, char *const *argv, const char *optstring,
00273 struct _getopt_data *d)
00274 {
00275
00276
00277
00278
00279 d->__first_nonopt = d->__last_nonopt = d->optind;
00280
00281 d->__nextchar = NULL;
00282
00283 d->__posixly_correct = !!getenv ("POSIXLY_CORRECT");
00284
00285
00286
00287 if (optstring[0] == '-')
00288 {
00289 d->__ordering = RETURN_IN_ORDER;
00290 ++optstring;
00291 }
00292 else if (optstring[0] == '+')
00293 {
00294 d->__ordering = REQUIRE_ORDER;
00295 ++optstring;
00296 }
00297 else if (d->__posixly_correct)
00298 d->__ordering = REQUIRE_ORDER;
00299 else
00300 d->__ordering = PERMUTE;
00301
00302 #if defined _LIBC && defined USE_NONOPTION_FLAGS
00303 if (!d->__posixly_correct
00304 && argc == __libc_argc && argv == __libc_argv)
00305 {
00306 if (d->__nonoption_flags_max_len == 0)
00307 {
00308 if (__getopt_nonoption_flags == NULL
00309 || __getopt_nonoption_flags[0] == '\0')
00310 d->__nonoption_flags_max_len = -1;
00311 else
00312 {
00313 const char *orig_str = __getopt_nonoption_flags;
00314 int len = d->__nonoption_flags_max_len = strlen (orig_str);
00315 if (d->__nonoption_flags_max_len < argc)
00316 d->__nonoption_flags_max_len = argc;
00317 __getopt_nonoption_flags =
00318 (char *) malloc (d->__nonoption_flags_max_len);
00319 if (__getopt_nonoption_flags == NULL)
00320 d->__nonoption_flags_max_len = -1;
00321 else
00322 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
00323 '\0', d->__nonoption_flags_max_len - len);
00324 }
00325 }
00326 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
00327 }
00328 else
00329 d->__nonoption_flags_len = 0;
00330 #endif
00331
00332 return optstring;
00333 }
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 int
00393 _getopt_internal_r (int argc, char *const *argv, const char *optstring,
00394 const struct option *longopts, int *longind,
00395 int long_only, struct _getopt_data *d)
00396 {
00397 int print_errors = d->opterr;
00398 if (optstring[0] == ':')
00399 print_errors = 0;
00400
00401 if (argc < 1)
00402 return -1;
00403
00404 d->optarg = NULL;
00405
00406 if (d->optind == 0 || !d->__initialized)
00407 {
00408 if (d->optind == 0)
00409 d->optind = 1;
00410 optstring = _getopt_initialize (argc, argv, optstring, d);
00411 d->__initialized = 1;
00412 }
00413
00414
00415
00416
00417
00418 #if defined _LIBC && defined USE_NONOPTION_FLAGS
00419 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
00420 || (d->optind < d->__nonoption_flags_len \
00421 && __getopt_nonoption_flags[d->optind] == '1'))
00422 #else
00423 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
00424 #endif
00425
00426 if (d->__nextchar == NULL || *d->__nextchar == '\0')
00427 {
00428
00429
00430
00431
00432 if (d->__last_nonopt > d->optind)
00433 d->__last_nonopt = d->optind;
00434 if (d->__first_nonopt > d->optind)
00435 d->__first_nonopt = d->optind;
00436
00437 if (d->__ordering == PERMUTE)
00438 {
00439
00440
00441
00442 if (d->__first_nonopt != d->__last_nonopt
00443 && d->__last_nonopt != d->optind)
00444 exchange ((char **) argv, d);
00445 else if (d->__last_nonopt != d->optind)
00446 d->__first_nonopt = d->optind;
00447
00448
00449
00450
00451 while (d->optind < argc && NONOPTION_P)
00452 d->optind++;
00453 d->__last_nonopt = d->optind;
00454 }
00455
00456
00457
00458
00459
00460
00461 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
00462 {
00463 d->optind++;
00464
00465 if (d->__first_nonopt != d->__last_nonopt
00466 && d->__last_nonopt != d->optind)
00467 exchange ((char **) argv, d);
00468 else if (d->__first_nonopt == d->__last_nonopt)
00469 d->__first_nonopt = d->optind;
00470 d->__last_nonopt = argc;
00471
00472 d->optind = argc;
00473 }
00474
00475
00476
00477
00478 if (d->optind == argc)
00479 {
00480
00481
00482 if (d->__first_nonopt != d->__last_nonopt)
00483 d->optind = d->__first_nonopt;
00484 return -1;
00485 }
00486
00487
00488
00489
00490 if (NONOPTION_P)
00491 {
00492 if (d->__ordering == REQUIRE_ORDER)
00493 return -1;
00494 d->optarg = argv[d->optind++];
00495 return 1;
00496 }
00497
00498
00499
00500
00501 d->__nextchar = (argv[d->optind] + 1
00502 + (longopts != NULL && argv[d->optind][1] == '-'));
00503 }
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520 if (longopts != NULL
00521 && (argv[d->optind][1] == '-'
00522 || (long_only && (argv[d->optind][2]
00523 || !strchr (optstring, argv[d->optind][1])))))
00524 {
00525 char *nameend;
00526 const struct option *p;
00527 const struct option *pfound = NULL;
00528 int exact = 0;
00529 int ambig = 0;
00530 int indfound = -1;
00531 int option_index;
00532
00533 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
00534 ;
00535
00536
00537
00538 for (p = longopts, option_index = 0; p->name; p++, option_index++)
00539 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
00540 {
00541 if ((unsigned int) (nameend - d->__nextchar)
00542 == (unsigned int) strlen (p->name))
00543 {
00544
00545 pfound = p;
00546 indfound = option_index;
00547 exact = 1;
00548 break;
00549 }
00550 else if (pfound == NULL)
00551 {
00552
00553 pfound = p;
00554 indfound = option_index;
00555 }
00556 else if (long_only
00557 || pfound->has_arg != p->has_arg
00558 || pfound->flag != p->flag
00559 || pfound->val != p->val)
00560
00561 ambig = 1;
00562 }
00563
00564 if (ambig && !exact)
00565 {
00566 if (print_errors)
00567 {
00568 #if defined _LIBC && defined USE_IN_LIBIO
00569 char *buf;
00570
00571 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
00572 argv[0], argv[d->optind]) >= 0)
00573 {
00574 _IO_flockfile (stderr);
00575
00576 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
00577 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
00578
00579 __fxprintf (NULL, "%s", buf);
00580
00581 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
00582 _IO_funlockfile (stderr);
00583
00584 free (buf);
00585 }
00586 #else
00587 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
00588 argv[0], argv[d->optind]);
00589 #endif
00590 }
00591 d->__nextchar += strlen (d->__nextchar);
00592 d->optind++;
00593 d->optopt = 0;
00594 return '?';
00595 }
00596
00597 if (pfound != NULL)
00598 {
00599 option_index = indfound;
00600 d->optind++;
00601 if (*nameend)
00602 {
00603
00604
00605 if (pfound->has_arg)
00606 d->optarg = nameend + 1;
00607 else
00608 {
00609 if (print_errors)
00610 {
00611 #if defined _LIBC && defined USE_IN_LIBIO
00612 char *buf;
00613 int n;
00614 #endif
00615
00616 if (argv[d->optind - 1][1] == '-')
00617 {
00618
00619 #if defined _LIBC && defined USE_IN_LIBIO
00620 n = __asprintf (&buf, _("\
00621 %s: option `--%s' doesn't allow an argument\n"),
00622 argv[0], pfound->name);
00623 #else
00624 fprintf (stderr, _("\
00625 %s: option `--%s' doesn't allow an argument\n"),
00626 argv[0], pfound->name);
00627 #endif
00628 }
00629 else
00630 {
00631
00632 #if defined _LIBC && defined USE_IN_LIBIO
00633 n = __asprintf (&buf, _("\
00634 %s: option `%c%s' doesn't allow an argument\n"),
00635 argv[0], argv[d->optind - 1][0],
00636 pfound->name);
00637 #else
00638 fprintf (stderr, _("\
00639 %s: option `%c%s' doesn't allow an argument\n"),
00640 argv[0], argv[d->optind - 1][0],
00641 pfound->name);
00642 #endif
00643 }
00644
00645 #if defined _LIBC && defined USE_IN_LIBIO
00646 if (n >= 0)
00647 {
00648 _IO_flockfile (stderr);
00649
00650 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
00651 ((_IO_FILE *) stderr)->_flags2
00652 |= _IO_FLAGS2_NOTCANCEL;
00653
00654 __fxprintf (NULL, "%s", buf);
00655
00656 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
00657 _IO_funlockfile (stderr);
00658
00659 free (buf);
00660 }
00661 #endif
00662 }
00663
00664 d->__nextchar += strlen (d->__nextchar);
00665
00666 d->optopt = pfound->val;
00667 return '?';
00668 }
00669 }
00670 else if (pfound->has_arg == 1)
00671 {
00672 if (d->optind < argc)
00673 d->optarg = argv[d->optind++];
00674 else
00675 {
00676 if (print_errors)
00677 {
00678 #if defined _LIBC && defined USE_IN_LIBIO
00679 char *buf;
00680
00681 if (__asprintf (&buf, _("\
00682 %s: option `%s' requires an argument\n"),
00683 argv[0], argv[d->optind - 1]) >= 0)
00684 {
00685 _IO_flockfile (stderr);
00686
00687 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
00688 ((_IO_FILE *) stderr)->_flags2
00689 |= _IO_FLAGS2_NOTCANCEL;
00690
00691 __fxprintf (NULL, "%s", buf);
00692
00693 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
00694 _IO_funlockfile (stderr);
00695
00696 free (buf);
00697 }
00698 #else
00699 fprintf (stderr, _("%s: option `%s' requires an argument\n"),
00700 argv[0], argv[d->optind - 1]);
00701 #endif
00702 }
00703 d->__nextchar += strlen (d->__nextchar);
00704 d->optopt = pfound->val;
00705 return optstring[0] == ':' ? ':' : '?';
00706 }
00707 }
00708 d->__nextchar += strlen (d->__nextchar);
00709 if (longind != NULL)
00710 *longind = option_index;
00711 if (pfound->flag)
00712 {
00713 *(pfound->flag) = pfound->val;
00714 return 0;
00715 }
00716 return pfound->val;
00717 }
00718
00719
00720
00721
00722
00723 if (!long_only || argv[d->optind][1] == '-'
00724 || strchr (optstring, *d->__nextchar) == NULL)
00725 {
00726 if (print_errors)
00727 {
00728 #if defined _LIBC && defined USE_IN_LIBIO
00729 char *buf;
00730 int n;
00731 #endif
00732
00733 if (argv[d->optind][1] == '-')
00734 {
00735
00736 #if defined _LIBC && defined USE_IN_LIBIO
00737 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
00738 argv[0], d->__nextchar);
00739 #else
00740 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
00741 argv[0], d->__nextchar);
00742 #endif
00743 }
00744 else
00745 {
00746
00747 #if defined _LIBC && defined USE_IN_LIBIO
00748 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
00749 argv[0], argv[d->optind][0], d->__nextchar);
00750 #else
00751 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
00752 argv[0], argv[d->optind][0], d->__nextchar);
00753 #endif
00754 }
00755
00756 #if defined _LIBC && defined USE_IN_LIBIO
00757 if (n >= 0)
00758 {
00759 _IO_flockfile (stderr);
00760
00761 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
00762 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
00763
00764 __fxprintf (NULL, "%s", buf);
00765
00766 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
00767 _IO_funlockfile (stderr);
00768
00769 free (buf);
00770 }
00771 #endif
00772 }
00773 d->__nextchar = (char *) "";
00774 d->optind++;
00775 d->optopt = 0;
00776 return '?';
00777 }
00778 }
00779
00780
00781
00782 {
00783 char c = *d->__nextchar++;
00784 char *temp = strchr (optstring, c);
00785
00786
00787 if (*d->__nextchar == '\0')
00788 ++d->optind;
00789
00790 if (temp == NULL || c == ':')
00791 {
00792 if (print_errors)
00793 {
00794 #if defined _LIBC && defined USE_IN_LIBIO
00795 char *buf;
00796 int n;
00797 #endif
00798
00799 if (d->__posixly_correct)
00800 {
00801
00802 #if defined _LIBC && defined USE_IN_LIBIO
00803 n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
00804 argv[0], c);
00805 #else
00806 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
00807 #endif
00808 }
00809 else
00810 {
00811 #if defined _LIBC && defined USE_IN_LIBIO
00812 n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
00813 argv[0], c);
00814 #else
00815 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
00816 #endif
00817 }
00818
00819 #if defined _LIBC && defined USE_IN_LIBIO
00820 if (n >= 0)
00821 {
00822 _IO_flockfile (stderr);
00823
00824 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
00825 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
00826
00827 __fxprintf (NULL, "%s", buf);
00828
00829 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
00830 _IO_funlockfile (stderr);
00831
00832 free (buf);
00833 }
00834 #endif
00835 }
00836 d->optopt = c;
00837 return '?';
00838 }
00839
00840 if (temp[0] == 'W' && temp[1] == ';')
00841 {
00842 char *nameend;
00843 const struct option *p;
00844 const struct option *pfound = NULL;
00845 int exact = 0;
00846 int ambig = 0;
00847 int indfound = 0;
00848 int option_index;
00849
00850
00851 if (*d->__nextchar != '\0')
00852 {
00853 d->optarg = d->__nextchar;
00854
00855
00856 d->optind++;
00857 }
00858 else if (d->optind == argc)
00859 {
00860 if (print_errors)
00861 {
00862
00863 #if defined _LIBC && defined USE_IN_LIBIO
00864 char *buf;
00865
00866 if (__asprintf (&buf,
00867 _("%s: option requires an argument -- %c\n"),
00868 argv[0], c) >= 0)
00869 {
00870 _IO_flockfile (stderr);
00871
00872 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
00873 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
00874
00875 __fxprintf (NULL, "%s", buf);
00876
00877 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
00878 _IO_funlockfile (stderr);
00879
00880 free (buf);
00881 }
00882 #else
00883 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
00884 argv[0], c);
00885 #endif
00886 }
00887 d->optopt = c;
00888 if (optstring[0] == ':')
00889 c = ':';
00890 else
00891 c = '?';
00892 return c;
00893 }
00894 else
00895
00896
00897 d->optarg = argv[d->optind++];
00898
00899
00900
00901
00902 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
00903 nameend++)
00904 ;
00905
00906
00907
00908 for (p = longopts, option_index = 0; p->name; p++, option_index++)
00909 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
00910 {
00911 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
00912 {
00913
00914 pfound = p;
00915 indfound = option_index;
00916 exact = 1;
00917 break;
00918 }
00919 else if (pfound == NULL)
00920 {
00921
00922 pfound = p;
00923 indfound = option_index;
00924 }
00925 else
00926
00927 ambig = 1;
00928 }
00929 if (ambig && !exact)
00930 {
00931 if (print_errors)
00932 {
00933 #if defined _LIBC && defined USE_IN_LIBIO
00934 char *buf;
00935
00936 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
00937 argv[0], argv[d->optind]) >= 0)
00938 {
00939 _IO_flockfile (stderr);
00940
00941 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
00942 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
00943
00944 __fxprintf (NULL, "%s", buf);
00945
00946 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
00947 _IO_funlockfile (stderr);
00948
00949 free (buf);
00950 }
00951 #else
00952 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
00953 argv[0], argv[d->optind]);
00954 #endif
00955 }
00956 d->__nextchar += strlen (d->__nextchar);
00957 d->optind++;
00958 return '?';
00959 }
00960 if (pfound != NULL)
00961 {
00962 option_index = indfound;
00963 if (*nameend)
00964 {
00965
00966
00967 if (pfound->has_arg)
00968 d->optarg = nameend + 1;
00969 else
00970 {
00971 if (print_errors)
00972 {
00973 #if defined _LIBC && defined USE_IN_LIBIO
00974 char *buf;
00975
00976 if (__asprintf (&buf, _("\
00977 %s: option `-W %s' doesn't allow an argument\n"),
00978 argv[0], pfound->name) >= 0)
00979 {
00980 _IO_flockfile (stderr);
00981
00982 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
00983 ((_IO_FILE *) stderr)->_flags2
00984 |= _IO_FLAGS2_NOTCANCEL;
00985
00986 __fxprintf (NULL, "%s", buf);
00987
00988 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
00989 _IO_funlockfile (stderr);
00990
00991 free (buf);
00992 }
00993 #else
00994 fprintf (stderr, _("\
00995 %s: option `-W %s' doesn't allow an argument\n"),
00996 argv[0], pfound->name);
00997 #endif
00998 }
00999
01000 d->__nextchar += strlen (d->__nextchar);
01001 return '?';
01002 }
01003 }
01004 else if (pfound->has_arg == 1)
01005 {
01006 if (d->optind < argc)
01007 d->optarg = argv[d->optind++];
01008 else
01009 {
01010 if (print_errors)
01011 {
01012 #if defined _LIBC && defined USE_IN_LIBIO
01013 char *buf;
01014
01015 if (__asprintf (&buf, _("\
01016 %s: option `%s' requires an argument\n"),
01017 argv[0], argv[d->optind - 1]) >= 0)
01018 {
01019 _IO_flockfile (stderr);
01020
01021 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
01022 ((_IO_FILE *) stderr)->_flags2
01023 |= _IO_FLAGS2_NOTCANCEL;
01024
01025 __fxprintf (NULL, "%s", buf);
01026
01027 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
01028 _IO_funlockfile (stderr);
01029
01030 free (buf);
01031 }
01032 #else
01033 fprintf (stderr,
01034 _("%s: option `%s' requires an argument\n"),
01035 argv[0], argv[d->optind - 1]);
01036 #endif
01037 }
01038 d->__nextchar += strlen (d->__nextchar);
01039 return optstring[0] == ':' ? ':' : '?';
01040 }
01041 }
01042 d->__nextchar += strlen (d->__nextchar);
01043 if (longind != NULL)
01044 *longind = option_index;
01045 if (pfound->flag)
01046 {
01047 *(pfound->flag) = pfound->val;
01048 return 0;
01049 }
01050 return pfound->val;
01051 }
01052 d->__nextchar = NULL;
01053 return 'W';
01054 }
01055 if (temp[1] == ':')
01056 {
01057 if (temp[2] == ':')
01058 {
01059
01060 if (*d->__nextchar != '\0')
01061 {
01062 d->optarg = d->__nextchar;
01063 d->optind++;
01064 }
01065 else
01066 d->optarg = NULL;
01067 d->__nextchar = NULL;
01068 }
01069 else
01070 {
01071
01072 if (*d->__nextchar != '\0')
01073 {
01074 d->optarg = d->__nextchar;
01075
01076
01077 d->optind++;
01078 }
01079 else if (d->optind == argc)
01080 {
01081 if (print_errors)
01082 {
01083
01084 #if defined _LIBC && defined USE_IN_LIBIO
01085 char *buf;
01086
01087 if (__asprintf (&buf, _("\
01088 %s: option requires an argument -- %c\n"),
01089 argv[0], c) >= 0)
01090 {
01091 _IO_flockfile (stderr);
01092
01093 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
01094 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
01095
01096 __fxprintf (NULL, "%s", buf);
01097
01098 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
01099 _IO_funlockfile (stderr);
01100
01101 free (buf);
01102 }
01103 #else
01104 fprintf (stderr,
01105 _("%s: option requires an argument -- %c\n"),
01106 argv[0], c);
01107 #endif
01108 }
01109 d->optopt = c;
01110 if (optstring[0] == ':')
01111 c = ':';
01112 else
01113 c = '?';
01114 }
01115 else
01116
01117
01118 d->optarg = argv[d->optind++];
01119 d->__nextchar = NULL;
01120 }
01121 }
01122 return c;
01123 }
01124 }
01125
01126 int
01127 _getopt_internal (int argc, char *const *argv, const char *optstring,
01128 const struct option *longopts, int *longind, int long_only)
01129 {
01130 int result;
01131
01132 getopt_data.optind = optind;
01133 getopt_data.opterr = opterr;
01134
01135 result = _getopt_internal_r (argc, argv, optstring, longopts,
01136 longind, long_only, &getopt_data);
01137
01138 optind = getopt_data.optind;
01139 optarg = getopt_data.optarg;
01140 optopt = getopt_data.optopt;
01141
01142 return result;
01143 }
01144
01145 int
01146 getopt (int argc, char *const *argv, const char *optstring)
01147 {
01148 return _getopt_internal (argc, argv, optstring,
01149 (const struct option *) 0,
01150 (int *) 0,
01151 0);
01152 }
01153
01154 #endif
01155
01156
01157 #ifdef TEST
01158
01159
01160
01161
01162 int
01163 main (int argc, char **argv)
01164 {
01165 int c;
01166 int digit_optind = 0;
01167
01168 while (1)
01169 {
01170 int this_option_optind = optind ? optind : 1;
01171
01172 c = getopt (argc, argv, "abc:d:0123456789");
01173 if (c == -1)
01174 break;
01175
01176 switch (c)
01177 {
01178 case '0':
01179 case '1':
01180 case '2':
01181 case '3':
01182 case '4':
01183 case '5':
01184 case '6':
01185 case '7':
01186 case '8':
01187 case '9':
01188 if (digit_optind != 0 && digit_optind != this_option_optind)
01189 printf ("digits occur in two different argv-elements.\n");
01190 digit_optind = this_option_optind;
01191 printf ("option %c\n", c);
01192 break;
01193
01194 case 'a':
01195 printf ("option a\n");
01196 break;
01197
01198 case 'b':
01199 printf ("option b\n");
01200 break;
01201
01202 case 'c':
01203 printf ("option c with value `%s'\n", optarg);
01204 break;
01205
01206 case '?':
01207 break;
01208
01209 default:
01210 printf ("?? getopt returned character code 0%o ??\n", c);
01211 }
01212 }
01213
01214 if (optind < argc)
01215 {
01216 printf ("non-option ARGV-elements: ");
01217 while (optind < argc)
01218 printf ("%s ", argv[optind++]);
01219 printf ("\n");
01220 }
01221
01222 exit (0);
01223 }
01224
01225 #endif