| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef _LIBC |
| 21 |
# include <config.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#include <getopt.h> |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include <unistd.h> |
| 30 |
|
| 31 |
#ifdef _LIBC |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
# include <libintl.h> |
| 40 |
# define fprintf __fxprintf_nocancel |
| 41 |
# define flockfile(fp) _IO_flockfile (fp) |
| 42 |
# define funlockfile(fp) _IO_funlockfile (fp) |
| 43 |
#else |
| 44 |
# include "gettext.h" |
| 45 |
# define _(msgid) gettext (msgid) |
| 46 |
|
| 47 |
|
| 48 |
# if (!defined _POSIX_THREAD_SAFE_FUNCTIONS \ |
| 49 |
|| (defined _WIN32 && ! defined __CYGWIN__)) |
| 50 |
# define flockfile(fp) |
| 51 |
# define funlockfile(fp) |
| 52 |
# endif |
| 53 |
|
| 54 |
# define __libc_use_alloca(size) 0 |
| 55 |
# undef alloca |
| 56 |
# define alloca(size) (abort (), (void *)0) |
| 57 |
#endif |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
#include "getopt_int.h" |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
char *optarg; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
int optind = 1; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
int opterr = 1; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
int optopt = '?'; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
static struct _getopt_data getopt_data; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
static void |
| 131 |
exchange (char **argv, struct _getopt_data *d) |
| 132 |
{ |
| 133 |
int bottom = d->__first_nonopt; |
| 134 |
int middle = d->__last_nonopt; |
| 135 |
int top = d->optind; |
| 136 |
char *tem; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
while (top > middle && middle > bottom) |
| 144 |
{ |
| 145 |
if (top - middle > middle - bottom) |
| 146 |
{ |
| 147 |
|
| 148 |
int len = middle - bottom; |
| 149 |
int i; |
| 150 |
|
| 151 |
|
| 152 |
for (i = 0; i < len; i++) |
| 153 |
{ |
| 154 |
tem = argv[bottom + i]; |
| 155 |
argv[bottom + i] = argv[top - (middle - bottom) + i]; |
| 156 |
argv[top - (middle - bottom) + i] = tem; |
| 157 |
} |
| 158 |
|
| 159 |
top -= len; |
| 160 |
} |
| 161 |
else |
| 162 |
{ |
| 163 |
|
| 164 |
int len = top - middle; |
| 165 |
int i; |
| 166 |
|
| 167 |
|
| 168 |
for (i = 0; i < len; i++) |
| 169 |
{ |
| 170 |
tem = argv[bottom + i]; |
| 171 |
argv[bottom + i] = argv[middle + i]; |
| 172 |
argv[middle + i] = tem; |
| 173 |
} |
| 174 |
|
| 175 |
bottom += len; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
d->__first_nonopt += (d->optind - d->__last_nonopt); |
| 182 |
d->__last_nonopt = d->optind; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
static int |
| 194 |
process_long_option (int argc, char **argv, const char *optstring, |
| 195 |
const struct option *longopts, int *longind, |
| 196 |
int long_only, struct _getopt_data *d, |
| 197 |
int print_errors, const char *prefix) |
| 198 |
{ |
| 199 |
char *nameend; |
| 200 |
size_t namelen; |
| 201 |
const struct option *p; |
| 202 |
const struct option *pfound = NULL; |
| 203 |
int n_options; |
| 204 |
int option_index; |
| 205 |
|
| 206 |
for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++) |
| 207 |
; |
| 208 |
namelen = nameend - d->__nextchar; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
for (p = longopts, n_options = 0; p->name; p++, n_options++) |
| 213 |
if (!strncmp (p->name, d->__nextchar, namelen) |
| 214 |
&& namelen == strlen (p->name)) |
| 215 |
{ |
| 216 |
|
| 217 |
pfound = p; |
| 218 |
option_index = n_options; |
| 219 |
break; |
| 220 |
} |
| 221 |
|
| 222 |
if (pfound == NULL) |
| 223 |
{ |
| 224 |
|
| 225 |
unsigned char *ambig_set = NULL; |
| 226 |
|
| 227 |
unsigned char ambig_fallback; |
| 228 |
void *ambig_malloced = NULL; |
| 229 |
int indfound = -1; |
| 230 |
|
| 231 |
for (p = longopts, option_index = 0; p->name; p++, option_index++) |
| 232 |
if (!strncmp (p->name, d->__nextchar, namelen)) |
| 233 |
{ |
| 234 |
if (pfound == NULL) |
| 235 |
{ |
| 236 |
|
| 237 |
pfound = p; |
| 238 |
indfound = option_index; |
| 239 |
} |
| 240 |
else if (long_only |
| 241 |
|| pfound->has_arg != p->has_arg |
| 242 |
|| pfound->flag != p->flag |
| 243 |
|| pfound->val != p->val) |
| 244 |
{ |
| 245 |
|
| 246 |
if (ambig_set != &ambig_fallback) |
| 247 |
{ |
| 248 |
if (!print_errors) |
| 249 |
|
| 250 |
|
| 251 |
ambig_set = &ambig_fallback; |
| 252 |
else if (!ambig_set) |
| 253 |
{ |
| 254 |
if (__libc_use_alloca (n_options)) |
| 255 |
ambig_set = alloca (n_options); |
| 256 |
else |
| 257 |
{ |
| 258 |
ambig_malloced = malloc (n_options); |
| 259 |
|
| 260 |
|
| 261 |
ambig_set = (ambig_malloced ? ambig_malloced |
| 262 |
: &ambig_fallback); |
| 263 |
} |
| 264 |
|
| 265 |
if (ambig_set != &ambig_fallback) |
| 266 |
{ |
| 267 |
memset (ambig_set, 0, n_options); |
| 268 |
ambig_set[indfound] = 1; |
| 269 |
} |
| 270 |
} |
| 271 |
if (ambig_set && ambig_set != &ambig_fallback) |
| 272 |
ambig_set[option_index] = 1; |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
if (ambig_set) |
| 278 |
{ |
| 279 |
if (print_errors) |
| 280 |
{ |
| 281 |
if (ambig_set == &ambig_fallback) |
| 282 |
fprintf (stderr, _("%s: option '%s%s' is ambiguous\n"), |
| 283 |
argv[0], prefix, d->__nextchar); |
| 284 |
else |
| 285 |
{ |
| 286 |
flockfile (stderr); |
| 287 |
fprintf (stderr, |
| 288 |
_("%s: option '%s%s' is ambiguous; possibilities:"), |
| 289 |
argv[0], prefix, d->__nextchar); |
| 290 |
|
| 291 |
for (option_index = 0; option_index < n_options; option_index++) |
| 292 |
if (ambig_set[option_index]) |
| 293 |
fprintf (stderr, " '%s%s'", |
| 294 |
prefix, longopts[option_index].name); |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
fprintf (stderr, "\n"); |
| 300 |
funlockfile (stderr); |
| 301 |
} |
| 302 |
} |
| 303 |
free (ambig_malloced); |
| 304 |
d->__nextchar += strlen (d->__nextchar); |
| 305 |
d->optind++; |
| 306 |
d->optopt = 0; |
| 307 |
return '?'; |
| 308 |
} |
| 309 |
|
| 310 |
option_index = indfound; |
| 311 |
} |
| 312 |
|
| 313 |
if (pfound == NULL) |
| 314 |
{ |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
if (!long_only || argv[d->optind][1] == '-' |
| 319 |
|| strchr (optstring, *d->__nextchar) == NULL) |
| 320 |
{ |
| 321 |
if (print_errors) |
| 322 |
fprintf (stderr, _("%s: unrecognized option '%s%s'\n"), |
| 323 |
argv[0], prefix, d->__nextchar); |
| 324 |
|
| 325 |
d->__nextchar = NULL; |
| 326 |
d->optind++; |
| 327 |
d->optopt = 0; |
| 328 |
return '?'; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
return -1; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
d->optind++; |
| 337 |
d->__nextchar = NULL; |
| 338 |
if (*nameend) |
| 339 |
{ |
| 340 |
|
| 341 |
|
| 342 |
if (pfound->has_arg) |
| 343 |
d->optarg = nameend + 1; |
| 344 |
else |
| 345 |
{ |
| 346 |
if (print_errors) |
| 347 |
fprintf (stderr, |
| 348 |
_("%s: option '%s%s' doesn't allow an argument\n"), |
| 349 |
argv[0], prefix, pfound->name); |
| 350 |
|
| 351 |
d->optopt = pfound->val; |
| 352 |
return '?'; |
| 353 |
} |
| 354 |
} |
| 355 |
else if (pfound->has_arg == 1) |
| 356 |
{ |
| 357 |
if (d->optind < argc) |
| 358 |
d->optarg = argv[d->optind++]; |
| 359 |
else |
| 360 |
{ |
| 361 |
if (print_errors) |
| 362 |
fprintf (stderr, |
| 363 |
_("%s: option '%s%s' requires an argument\n"), |
| 364 |
argv[0], prefix, pfound->name); |
| 365 |
|
| 366 |
d->optopt = pfound->val; |
| 367 |
return optstring[0] == ':' ? ':' : '?'; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
if (longind != NULL) |
| 372 |
*longind = option_index; |
| 373 |
if (pfound->flag) |
| 374 |
{ |
| 375 |
*(pfound->flag) = pfound->val; |
| 376 |
return 0; |
| 377 |
} |
| 378 |
return pfound->val; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
static const char * |
| 384 |
_getopt_initialize (_GL_UNUSED int argc, |
| 385 |
_GL_UNUSED char **argv, const char *optstring, |
| 386 |
struct _getopt_data *d, int posixly_correct) |
| 387 |
{ |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
if (d->optind == 0) |
| 392 |
d->optind = 1; |
| 393 |
|
| 394 |
d->__first_nonopt = d->__last_nonopt = d->optind; |
| 395 |
d->__nextchar = NULL; |
| 396 |
|
| 397 |
|
| 398 |
if (optstring[0] == '-') |
| 399 |
{ |
| 400 |
d->__ordering = RETURN_IN_ORDER; |
| 401 |
++optstring; |
| 402 |
} |
| 403 |
else if (optstring[0] == '+') |
| 404 |
{ |
| 405 |
d->__ordering = REQUIRE_ORDER; |
| 406 |
++optstring; |
| 407 |
} |
| 408 |
else if (posixly_correct || !!getenv ("POSIXLY_CORRECT")) |
| 409 |
d->__ordering = REQUIRE_ORDER; |
| 410 |
else |
| 411 |
d->__ordering = PERMUTE; |
| 412 |
|
| 413 |
d->__initialized = 1; |
| 414 |
return optstring; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
int |
| 474 |
_getopt_internal_r (int argc, char **argv, const char *optstring, |
| 475 |
const struct option *longopts, int *longind, |
| 476 |
int long_only, struct _getopt_data *d, int posixly_correct) |
| 477 |
{ |
| 478 |
int print_errors = d->opterr; |
| 479 |
|
| 480 |
if (argc < 1) |
| 481 |
return -1; |
| 482 |
|
| 483 |
d->optarg = NULL; |
| 484 |
|
| 485 |
if (d->optind == 0 || !d->__initialized) |
| 486 |
optstring = _getopt_initialize (argc, argv, optstring, d, posixly_correct); |
| 487 |
else if (optstring[0] == '-' || optstring[0] == '+') |
| 488 |
optstring++; |
| 489 |
|
| 490 |
if (optstring[0] == ':') |
| 491 |
print_errors = 0; |
| 492 |
|
| 493 |
|
| 494 |
#define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0') |
| 495 |
|
| 496 |
if (d->__nextchar == NULL || *d->__nextchar == '\0') |
| 497 |
{ |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
if (d->__last_nonopt > d->optind) |
| 503 |
d->__last_nonopt = d->optind; |
| 504 |
if (d->__first_nonopt > d->optind) |
| 505 |
d->__first_nonopt = d->optind; |
| 506 |
|
| 507 |
if (d->__ordering == PERMUTE) |
| 508 |
{ |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
if (d->__first_nonopt != d->__last_nonopt |
| 513 |
&& d->__last_nonopt != d->optind) |
| 514 |
exchange (argv, d); |
| 515 |
else if (d->__last_nonopt != d->optind) |
| 516 |
d->__first_nonopt = d->optind; |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
while (d->optind < argc && NONOPTION_P) |
| 522 |
d->optind++; |
| 523 |
d->__last_nonopt = d->optind; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
if (d->optind != argc && !strcmp (argv[d->optind], "--")) |
| 532 |
{ |
| 533 |
d->optind++; |
| 534 |
|
| 535 |
if (d->__first_nonopt != d->__last_nonopt |
| 536 |
&& d->__last_nonopt != d->optind) |
| 537 |
exchange (argv, d); |
| 538 |
else if (d->__first_nonopt == d->__last_nonopt) |
| 539 |
d->__first_nonopt = d->optind; |
| 540 |
d->__last_nonopt = argc; |
| 541 |
|
| 542 |
d->optind = argc; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
if (d->optind == argc) |
| 549 |
{ |
| 550 |
|
| 551 |
|
| 552 |
if (d->__first_nonopt != d->__last_nonopt) |
| 553 |
d->optind = d->__first_nonopt; |
| 554 |
return -1; |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
if (NONOPTION_P) |
| 561 |
{ |
| 562 |
if (d->__ordering == REQUIRE_ORDER) |
| 563 |
return -1; |
| 564 |
d->optarg = argv[d->optind++]; |
| 565 |
return 1; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
if (longopts) |
| 571 |
{ |
| 572 |
if (argv[d->optind][1] == '-') |
| 573 |
{ |
| 574 |
|
| 575 |
|
| 576 |
d->__nextchar = argv[d->optind] + 2; |
| 577 |
return process_long_option (argc, argv, optstring, longopts, |
| 578 |
longind, long_only, d, |
| 579 |
print_errors, "--"); |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
if (long_only && (argv[d->optind][2] |
| 595 |
|| !strchr (optstring, argv[d->optind][1]))) |
| 596 |
{ |
| 597 |
int code; |
| 598 |
d->__nextchar = argv[d->optind] + 1; |
| 599 |
code = process_long_option (argc, argv, optstring, longopts, |
| 600 |
longind, long_only, d, |
| 601 |
print_errors, "-"); |
| 602 |
if (code != -1) |
| 603 |
return code; |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
d->__nextchar = argv[d->optind] + 1; |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
{ |
| 614 |
char c = *d->__nextchar++; |
| 615 |
const char *temp = strchr (optstring, c); |
| 616 |
|
| 617 |
|
| 618 |
if (*d->__nextchar == '\0') |
| 619 |
++d->optind; |
| 620 |
|
| 621 |
if (temp == NULL || c == ':' || c == ';') |
| 622 |
{ |
| 623 |
if (print_errors) |
| 624 |
fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c); |
| 625 |
d->optopt = c; |
| 626 |
return '?'; |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
if (temp[0] == 'W' && temp[1] == ';' && longopts != NULL) |
| 631 |
{ |
| 632 |
|
| 633 |
if (*d->__nextchar != '\0') |
| 634 |
d->optarg = d->__nextchar; |
| 635 |
else if (d->optind == argc) |
| 636 |
{ |
| 637 |
if (print_errors) |
| 638 |
fprintf (stderr, |
| 639 |
_("%s: option requires an argument -- '%c'\n"), |
| 640 |
argv[0], c); |
| 641 |
|
| 642 |
d->optopt = c; |
| 643 |
if (optstring[0] == ':') |
| 644 |
c = ':'; |
| 645 |
else |
| 646 |
c = '?'; |
| 647 |
return c; |
| 648 |
} |
| 649 |
else |
| 650 |
d->optarg = argv[d->optind]; |
| 651 |
|
| 652 |
d->__nextchar = d->optarg; |
| 653 |
d->optarg = NULL; |
| 654 |
return process_long_option (argc, argv, optstring, longopts, longind, |
| 655 |
0 , d, print_errors, "-W "); |
| 656 |
} |
| 657 |
if (temp[1] == ':') |
| 658 |
{ |
| 659 |
if (temp[2] == ':') |
| 660 |
{ |
| 661 |
|
| 662 |
if (*d->__nextchar != '\0') |
| 663 |
{ |
| 664 |
d->optarg = d->__nextchar; |
| 665 |
d->optind++; |
| 666 |
} |
| 667 |
else |
| 668 |
d->optarg = NULL; |
| 669 |
d->__nextchar = NULL; |
| 670 |
} |
| 671 |
else |
| 672 |
{ |
| 673 |
|
| 674 |
if (*d->__nextchar != '\0') |
| 675 |
{ |
| 676 |
d->optarg = d->__nextchar; |
| 677 |
|
| 678 |
|
| 679 |
d->optind++; |
| 680 |
} |
| 681 |
else if (d->optind == argc) |
| 682 |
{ |
| 683 |
if (print_errors) |
| 684 |
fprintf (stderr, |
| 685 |
_("%s: option requires an argument -- '%c'\n"), |
| 686 |
argv[0], c); |
| 687 |
|
| 688 |
d->optopt = c; |
| 689 |
if (optstring[0] == ':') |
| 690 |
c = ':'; |
| 691 |
else |
| 692 |
c = '?'; |
| 693 |
} |
| 694 |
else |
| 695 |
|
| 696 |
|
| 697 |
d->optarg = argv[d->optind++]; |
| 698 |
d->__nextchar = NULL; |
| 699 |
} |
| 700 |
} |
| 701 |
return c; |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
int |
| 706 |
_getopt_internal (int argc, char **argv, const char *optstring, |
| 707 |
const struct option *longopts, int *longind, int long_only, |
| 708 |
int posixly_correct) |
| 709 |
{ |
| 710 |
int result; |
| 711 |
|
| 712 |
getopt_data.optind = optind; |
| 713 |
getopt_data.opterr = opterr; |
| 714 |
|
| 715 |
result = _getopt_internal_r (argc, argv, optstring, longopts, |
| 716 |
longind, long_only, &getopt_data, |
| 717 |
posixly_correct); |
| 718 |
|
| 719 |
optind = getopt_data.optind; |
| 720 |
optarg = getopt_data.optarg; |
| 721 |
optopt = getopt_data.optopt; |
| 722 |
|
| 723 |
return result; |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
#define GETOPT_ENTRY(NAME, POSIXLY_CORRECT) \ |
| 731 |
int \ |
| 732 |
NAME (int argc, char *const *argv, const char *optstring) \ |
| 733 |
{ \ |
| 734 |
return _getopt_internal (argc, (char **)argv, optstring, \ |
| 735 |
0, 0, 0, POSIXLY_CORRECT); \ |
| 736 |
} |
| 737 |
|
| 738 |
#ifdef _LIBC |
| 739 |
GETOPT_ENTRY(getopt, 0) |
| 740 |
GETOPT_ENTRY(__posix_getopt, 1) |
| 741 |
#else |
| 742 |
GETOPT_ENTRY(getopt, 1) |
| 743 |
#endif |
| 744 |
|
| 745 |
|
| 746 |
#ifdef TEST |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
int |
| 752 |
main (int argc, char **argv) |
| 753 |
{ |
| 754 |
int c; |
| 755 |
int digit_optind = 0; |
| 756 |
|
| 757 |
while (1) |
| 758 |
{ |
| 759 |
int this_option_optind = optind ? optind : 1; |
| 760 |
|
| 761 |
c = getopt (argc, argv, "abc:d:0123456789"); |
| 762 |
if (c == -1) |
| 763 |
break; |
| 764 |
|
| 765 |
switch (c) |
| 766 |
{ |
| 767 |
case '0': |
| 768 |
case '1': |
| 769 |
case '2': |
| 770 |
case '3': |
| 771 |
case '4': |
| 772 |
case '5': |
| 773 |
case '6': |
| 774 |
case '7': |
| 775 |
case '8': |
| 776 |
case '9': |
| 777 |
if (digit_optind != 0 && digit_optind != this_option_optind) |
| 778 |
printf ("digits occur in two different argv-elements.\n"); |
| 779 |
digit_optind = this_option_optind; |
| 780 |
printf ("option %c\n", c); |
| 781 |
break; |
| 782 |
|
| 783 |
case 'a': |
| 784 |
printf ("option a\n"); |
| 785 |
break; |
| 786 |
|
| 787 |
case 'b': |
| 788 |
printf ("option b\n"); |
| 789 |
break; |
| 790 |
|
| 791 |
case 'c': |
| 792 |
printf ("option c with value '%s'\n", optarg); |
| 793 |
break; |
| 794 |
|
| 795 |
case '?': |
| 796 |
break; |
| 797 |
|
| 798 |
default: |
| 799 |
printf ("?? getopt returned character code 0%o ??\n", c); |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
if (optind < argc) |
| 804 |
{ |
| 805 |
printf ("non-option ARGV-elements: "); |
| 806 |
while (optind < argc) |
| 807 |
printf ("%s ", argv[optind++]); |
| 808 |
printf ("\n"); |
| 809 |
} |
| 810 |
|
| 811 |
exit (0); |
| 812 |
} |
| 813 |
|
| 814 |
#endif |