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