| 39 | static char *optarg; |
| 40 | |
| 41 | static int getopt(int argc, char *argv[], const char *opts) |
| 42 | { |
| 43 | static int sp = 1; |
| 44 | int c; |
| 45 | char *cp; |
| 46 | |
| 47 | if (sp == 1) { |
| 48 | if (optind >= argc || |
| 49 | argv[optind][0] != '-' || argv[optind][1] == '\0') |
| 50 | return EOF; |
| 51 | else if (!strcmp(argv[optind], "--")) { |
| 52 | optind++; |
| 53 | return EOF; |
| 54 | } |
| 55 | } |
| 56 | optopt = c = argv[optind][sp]; |
| 57 | if (c == ':' || !(cp = strchr(opts, c))) { |
| 58 | fprintf(stderr, ": illegal option -- %c\n", c); |
| 59 | if (argv[optind][++sp] == '\0') { |
| 60 | optind++; |
| 61 | sp = 1; |
| 62 | } |
| 63 | return '?'; |
| 64 | } |
| 65 | if (*++cp == ':') { |
| 66 | if (argv[optind][sp+1] != '\0') |
| 67 | optarg = &argv[optind++][sp+1]; |
| 68 | else if(++optind >= argc) { |
| 69 | fprintf(stderr, ": option requires an argument -- %c\n", c); |
| 70 | sp = 1; |
| 71 | return '?'; |
| 72 | } else |
| 73 | optarg = argv[optind++]; |
| 74 | sp = 1; |
| 75 | } else { |
| 76 | if (argv[optind][++sp] == '\0') { |
| 77 | sp = 1; |
| 78 | optind++; |
| 79 | } |
| 80 | optarg = NULL; |
| 81 | } |
| 82 | |
| 83 | return c; |
| 84 | } |