|
27 | 27 | #include <inttypes.h> |
28 | 28 | #include <string.h> |
29 | 29 | #include <assert.h> |
| 30 | + |
| 31 | +#if !defined(_MSC_VER) |
30 | 32 | #include <unistd.h> |
| 33 | +#endif |
| 34 | + |
31 | 35 | #include <errno.h> |
32 | 36 | #if !defined(_WIN32) |
33 | 37 | #include <sys/wait.h> |
@@ -521,6 +525,80 @@ static const char *get_short_optarg(int *poptind, int opt, |
521 | 525 | return optarg; |
522 | 526 | } |
523 | 527 |
|
| 528 | +#if defined(_MSC_VER) |
| 529 | + |
| 530 | +static int opterr = 1; /* if error message should be printed */ |
| 531 | +static int optind = 1; /* index into parent argv vector */ |
| 532 | +static int optopt; /* character checked for validity */ |
| 533 | +static int optreset; /* reset getopt */ |
| 534 | +static char* optarg; /* argument associated with option */ |
| 535 | + |
| 536 | +#define BADCH (int)'?' |
| 537 | +#define BADARG (int)':' |
| 538 | +#define EMSG "" |
| 539 | + |
| 540 | +/* |
| 541 | + * getopt -- |
| 542 | + * Parse argc/argv argument vector. |
| 543 | + */ |
| 544 | +static int getopt(int nargc, char* const nargv[], const char* ostr) |
| 545 | +{ |
| 546 | + static char* place = EMSG; /* option letter processing */ |
| 547 | + const char* oli; /* option letter list index */ |
| 548 | + |
| 549 | + if (optreset || !*place) { /* update scanning pointer */ |
| 550 | + optreset = 0; |
| 551 | + if (optind >= nargc || *(place = nargv[optind]) != '-') { |
| 552 | + place = EMSG; |
| 553 | + return (-1); |
| 554 | + } |
| 555 | + if (place[1] && *++place == '-') { /* found "--" */ |
| 556 | + ++optind; |
| 557 | + place = EMSG; |
| 558 | + return (-1); |
| 559 | + } |
| 560 | + } /* option letter okay? */ |
| 561 | + if ((optopt = (int)*place++) == (int)':' || |
| 562 | + !(oli = strchr(ostr, optopt))) { |
| 563 | + /* |
| 564 | + * if the user didn't specify '-' as an option, |
| 565 | + * assume it means -1. |
| 566 | + */ |
| 567 | + if (optopt == (int)'-') |
| 568 | + return (-1); |
| 569 | + if (!*place) |
| 570 | + ++optind; |
| 571 | + if (opterr && *ostr != ':') |
| 572 | + (void)printf("illegal option -- %c\n", optopt); |
| 573 | + return (BADCH); |
| 574 | + } |
| 575 | + if (*++oli != ':') { /* don't need argument */ |
| 576 | + optarg = NULL; |
| 577 | + if (!*place) |
| 578 | + ++optind; |
| 579 | + } |
| 580 | + else { /* need an argument */ |
| 581 | + if (*place) /* no white space */ |
| 582 | + optarg = place; |
| 583 | + else if (nargc <= ++optind) { /* no arg */ |
| 584 | + place = EMSG; |
| 585 | + if (*ostr == ':') |
| 586 | + return (BADARG); |
| 587 | + if (opterr) |
| 588 | + (void)printf("option requires an argument -- %c\n", optopt); |
| 589 | + return (BADCH); |
| 590 | + } |
| 591 | + else /* white space */ |
| 592 | + optarg = nargv[optind]; |
| 593 | + place = EMSG; |
| 594 | + ++optind; |
| 595 | + } |
| 596 | + return (optopt); /* dump back option letter */ |
| 597 | +} |
| 598 | + |
| 599 | +#endif |
| 600 | + |
| 601 | + |
524 | 602 | int main(int argc, char **argv) |
525 | 603 | { |
526 | 604 | int i, verbose, strip_flags; |
|
0 commit comments