9 int optind=1, opterr=1, optopt, __optpos, __optreset=0;
11 #define optpos __optpos
12 weak_alias(__optreset, optreset);
14 int getopt(int argc, char * const argv[], const char *optstring)
21 if (!optind || __optreset) {
27 if (optind >= argc || !argv[optind] || argv[optind][0] != '-' || !argv[optind][1])
29 if (argv[optind][1] == '-' && !argv[optind][2])
32 if (!optpos) optpos++;
33 if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) {
35 c = 0xfffd; /* replacement char */
37 optchar = argv[optind]+optpos;
41 if (!argv[optind][optpos]) {
46 for (i=0; (l = mbtowc(&d, optstring+i, MB_LEN_MAX)) && d!=c; i+=l>0?l:1);
49 if (optstring[0] != ':' && opterr) {
50 write(2, argv[0], strlen(argv[0]));
51 write(2, ": illegal option: ", 18);
57 if (optstring[i+1] == ':') {
59 if (optstring[0] == ':') return ':';
61 write(2, argv[0], strlen(argv[0]));
62 write(2, ": option requires an argument: ", 31);
68 optarg = argv[optind++] + optpos;