| 1 | /* |
|---|---|
| 2 | * This is a version of the public domain getopt implementation by |
| 3 | * Henry Spencer originally posted to net.sources. |
| 4 | * |
| 5 | * This file is in the public domain. |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | #define getopt fz_getopt |
| 12 | #define optarg fz_optarg |
| 13 | #define optind fz_optind |
| 14 | |
| 15 | char *optarg; /* Global argument pointer. */ |
| 16 | int optind = 0; /* Global argv index. */ |
| 17 | |
| 18 | static char *scan = NULL; /* Private scan pointer. */ |
| 19 | |
| 20 | int |
| 21 | getopt(int argc, char *argv[], char *optstring) |
| 22 | { |
| 23 | char c; |
| 24 | char *place; |
| 25 | |
| 26 | optarg = NULL; |
| 27 | |
| 28 | if (!scan || *scan == '\0') { |
| 29 | if (optind == 0) |
| 30 | optind++; |
| 31 | |
| 32 | if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0') |
| 33 | return EOF; |
| 34 | if (argv[optind][1] == '-' && argv[optind][2] == '\0') { |
| 35 | optind++; |
| 36 | return EOF; |
| 37 | } |
| 38 | |
| 39 | scan = argv[optind]+1; |
| 40 | optind++; |
| 41 | } |
| 42 | |
| 43 | c = *scan++; |
| 44 | place = strchr(optstring, c); |
| 45 | |
| 46 | if (!place || c == ':') { |
| 47 | fprintf(stderr, "%s: unknown option -%c\n", argv[0], c); |
| 48 | return '?'; |
| 49 | } |
| 50 | |
| 51 | place++; |
| 52 | if (*place == ':') { |
| 53 | if (*scan != '\0') { |
| 54 | optarg = scan; |
| 55 | scan = NULL; |
| 56 | } else if( optind < argc ) { |
| 57 | optarg = argv[optind]; |
| 58 | optind++; |
| 59 | } else { |
| 60 | fprintf(stderr, "%s: option requires argument -%c\n", argv[0], c); |
| 61 | return ':'; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return c; |
| 66 | } |
| 67 |