* getopt -- * Parse argc/argv argument vector. */
| 65 | * Parse argc/argv argument vector. |
| 66 | */ |
| 67 | int |
| 68 | getopt(int nargc, char * const nargv[], const char *ostr) |
| 69 | { |
| 70 | static char *place = EMSG; /* option letter processing */ |
| 71 | char *oli; /* option letter list index */ |
| 72 | |
| 73 | if (optreset || *place == 0) { /* update scanning pointer */ |
| 74 | optreset = 0; |
| 75 | place = nargv[optind]; |
| 76 | if (optind >= nargc || *place++ != '-') { |
| 77 | /* Argument is absent or is not an option */ |
| 78 | place = EMSG; |
| 79 | return (-1); |
| 80 | } |
| 81 | optopt = *place++; |
| 82 | if (optopt == '-' && *place == 0) { |
| 83 | /* "--" => end of options */ |
| 84 | ++optind; |
| 85 | place = EMSG; |
| 86 | return (-1); |
| 87 | } |
| 88 | if (optopt == 0) { |
| 89 | /* Solitary '-', treat as a '-' option |
| 90 | if the program (eg su) is looking for it. */ |
| 91 | place = EMSG; |
| 92 | if (strchr(ostr, '-') == NULL) |
| 93 | return (-1); |
| 94 | optopt = '-'; |
| 95 | } |
| 96 | } else |
| 97 | optopt = *place++; |
| 98 | |
| 99 | /* See if option letter is one the caller wanted... */ |
| 100 | if (optopt == ':' || (oli = strchr(ostr, optopt)) == NULL) { |
| 101 | if (*place == 0) |
| 102 | ++optind; |
| 103 | if (opterr && *ostr != ':') |
| 104 | (void)fprintf(stderr, |
| 105 | #ifndef FSTACK |
| 106 | "%s: illegal option -- %c\n", _getprogname(), |
| 107 | #else |
| 108 | "%s: illegal option -- %c\n", getprogname(), |
| 109 | #endif |
| 110 | optopt); |
| 111 | return (BADCH); |
| 112 | } |
| 113 | |
| 114 | /* Does this option need an argument? */ |
| 115 | if (oli[1] != ':') { |
| 116 | /* don't need argument */ |
| 117 | optarg = NULL; |
| 118 | if (*place == 0) |
| 119 | ++optind; |
| 120 | } else { |
| 121 | /* Option-argument is either the rest of this argument or the |
| 122 | entire next argument. */ |
| 123 | if (*place) |
| 124 | optarg = place; |