7 #include "locale_impl.h"
10 int optind=1, opterr=1, optopt, __optpos, __optreset=0;
12 #define optpos __optpos
13 weak_alias(__optreset, optreset);
15 void __getopt_msg(const char *a, const char *b, const char *c, size_t l)
20 fwrite(a, strlen(a), 1, f)
21 && fwrite(b, strlen(b), 1, f)
27 int getopt(int argc, char * const argv[], const char *optstring)
34 if (!optind || __optreset) {
40 if (optind >= argc || !argv[optind])
43 if (argv[optind][0] != '-') {
44 if (optstring[0] == '-') {
45 optarg = argv[optind++];
54 if (argv[optind][1] == '-' && !argv[optind][2])
57 if (!optpos) optpos++;
58 if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) {
60 c = 0xfffd; /* replacement char */
62 optchar = argv[optind]+optpos;
66 if (!argv[optind][optpos]) {
71 if (optstring[0] == '-' || optstring[0] == '+')
77 l = mbtowc(&d, optstring+i, MB_LEN_MAX);
78 if (l>0) i+=l; else i++;
79 } while (l && d != c);
82 if (optstring[0] != ':' && opterr)
83 __getopt_msg(argv[0], ": unrecognized option: ", optchar, k);
86 if (optstring[i] == ':') {
87 if (optstring[i+1] == ':') optarg = 0;
88 else if (optind >= argc) {
89 if (optstring[0] == ':') return ':';
90 if (opterr) __getopt_msg(argv[0],
91 ": option requires an argument: ",
95 if (optstring[i+1] != ':' || optpos) {
96 optarg = argv[optind++] + optpos;
103 weak_alias(getopt, __posix_getopt);