8 int optind=1, opterr=1, optopt;
11 int getopt(int argc, char * const argv[], const char *optstring)
18 if (optind >= argc || !argv[optind] || argv[optind][0] != '-' || !argv[optind][1])
20 if (argv[optind][1] == '-' && !argv[optind][2])
23 if (!optpos) optpos++;
24 if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) {
26 c = 0xfffd; /* replacement char */
28 optchar = argv[optind]+optpos;
32 if (!argv[optind][optpos]) {
37 for (i=0; (l = mbtowc(&d, optstring+i, MB_LEN_MAX)) && d!=c; i+=l>0?l:1);
40 if (optstring[0] != ':' && opterr) {
41 write(2, argv[0], strlen(argv[0]));
42 write(2, ": illegal option: ", 18);
48 if (optstring[i+1] == ':') {
50 if (optstring[0] == ':') return ':';
52 write(2, argv[0], strlen(argv[0]));
53 write(2, ": option requires an argument: ", 31);
59 optarg = argv[optind++] + optpos;