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