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