9 int optind=1, opterr=1, optopt, __optpos, __optreset=0;
11 #define optpos __optpos
13 void __getopt_msg(const char *a, const char *b, const char *c, size_t l)
17 && fwrite(b, strlen(b), 1, f)
18 && fwrite(c, 1, l, f)==l
22 int getopt(int argc, char * const argv[], const char *optstring)
29 if (!optind || __optreset) {
35 if (optind >= argc || !argv[optind])
38 if (argv[optind][0] != '-') {
39 if (optstring[0] == '-') {
40 optarg = argv[optind++];
49 if (argv[optind][1] == '-' && !argv[optind][2])
52 if (!optpos) optpos++;
53 if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) {
55 c = 0xfffd; /* replacement char */
57 optchar = argv[optind]+optpos;
60 if (!argv[optind][optpos]) {
65 if (optstring[0] == '-' || optstring[0] == '+')
71 l = mbtowc(&d, optstring+i, MB_LEN_MAX);
72 if (l>0) i+=l; else i++;
73 } while (l && d != c);
75 if (d != c || c == ':') {
77 if (optstring[0] != ':' && opterr)
78 __getopt_msg(argv[0], ": unrecognized option: ", optchar, k);
81 if (optstring[i] == ':') {
83 if (optstring[i+1] != ':' || optpos) {
84 optarg = argv[optind++] + optpos;
89 if (optstring[0] == ':') return ':';
90 if (opterr) __getopt_msg(argv[0],
91 ": option requires an argument: ",