OLD | NEW |
1 #include <unistd.h> | 1 #include <unistd.h> |
2 #include <wchar.h> | 2 #include <wchar.h> |
3 #include <string.h> | 3 #include <string.h> |
4 #include <limits.h> | 4 #include <limits.h> |
5 #include <stdlib.h> | 5 #include <stdlib.h> |
6 #include "libc.h" | 6 #include "libc.h" |
7 #include "locale_impl.h" | 7 #include "locale_impl.h" |
8 | 8 |
9 char *optarg; | 9 char* optarg; |
10 int optind=1, opterr=1, optopt, __optpos, __optreset=0; | 10 int optind = 1, opterr = 1, optopt, __optpos, __optreset = 0; |
11 | 11 |
12 #define optpos __optpos | 12 #define optpos __optpos |
13 weak_alias(__optreset, optreset); | 13 weak_alias(__optreset, optreset); |
14 | 14 |
15 void __getopt_msg(const char *a, const char *b, const char *c, size_t l) | 15 void __getopt_msg(const char* a, const char* b, const char* c, size_t l) { |
16 { | 16 FILE* f = stderr; |
17 » FILE *f = stderr; | 17 b = __lctrans_cur(b); |
18 » b = __lctrans_cur(b); | 18 flockfile(f); |
19 » flockfile(f); | 19 fwrite(a, strlen(a), 1, f) && fwrite(b, strlen(b), 1, f) && |
20 » fwrite(a, strlen(a), 1, f) | 20 fwrite(c, l, 1, f) && putc('\n', f); |
21 » && fwrite(b, strlen(b), 1, f) | 21 funlockfile(f); |
22 » && fwrite(c, l, 1, f) | |
23 » && putc('\n', f); | |
24 » funlockfile(f); | |
25 } | 22 } |
26 | 23 |
27 int getopt(int argc, char * const argv[], const char *optstring) | 24 int getopt(int argc, char* const argv[], const char* optstring) { |
28 { | 25 int i; |
29 » int i; | 26 wchar_t c, d; |
30 » wchar_t c, d; | 27 int k, l; |
31 » int k, l; | 28 char* optchar; |
32 » char *optchar; | |
33 | 29 |
34 » if (!optind || __optreset) { | 30 if (!optind || __optreset) { |
35 » » __optreset = 0; | 31 __optreset = 0; |
36 » » __optpos = 0; | 32 __optpos = 0; |
37 » » optind = 1; | 33 optind = 1; |
38 » } | 34 } |
39 | 35 |
40 » if (optind >= argc || !argv[optind]) | 36 if (optind >= argc || !argv[optind]) |
41 » » return -1; | 37 return -1; |
42 | 38 |
43 » if (argv[optind][0] != '-') { | 39 if (argv[optind][0] != '-') { |
44 » » if (optstring[0] == '-') { | 40 if (optstring[0] == '-') { |
45 » » » optarg = argv[optind++]; | 41 optarg = argv[optind++]; |
46 » » » return 1; | 42 return 1; |
47 » » } | 43 } |
48 » » return -1; | 44 return -1; |
49 » } | 45 } |
50 | 46 |
51 » if (!argv[optind][1]) | 47 if (!argv[optind][1]) |
52 » » return -1; | 48 return -1; |
53 | 49 |
54 » if (argv[optind][1] == '-' && !argv[optind][2]) | 50 if (argv[optind][1] == '-' && !argv[optind][2]) |
55 » » return optind++, -1; | 51 return optind++, -1; |
56 | 52 |
57 » if (!optpos) optpos++; | 53 if (!optpos) |
58 » if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) { | 54 optpos++; |
59 » » k = 1; | 55 if ((k = mbtowc(&c, argv[optind] + optpos, MB_LEN_MAX)) < 0) { |
60 » » c = 0xfffd; /* replacement char */ | 56 k = 1; |
61 » } | 57 c = 0xfffd; /* replacement char */ |
62 » optchar = argv[optind]+optpos; | 58 } |
63 » optopt = c; | 59 optchar = argv[optind] + optpos; |
64 » optpos += k; | 60 optopt = c; |
| 61 optpos += k; |
65 | 62 |
66 » if (!argv[optind][optpos]) { | 63 if (!argv[optind][optpos]) { |
67 » » optind++; | 64 optind++; |
68 » » optpos = 0; | 65 optpos = 0; |
69 » } | 66 } |
70 | 67 |
71 » if (optstring[0] == '-' || optstring[0] == '+') | 68 if (optstring[0] == '-' || optstring[0] == '+') |
72 » » optstring++; | 69 optstring++; |
73 | 70 |
74 » i = 0; | 71 i = 0; |
75 » d = 0; | 72 d = 0; |
76 » do { | 73 do { |
77 » » l = mbtowc(&d, optstring+i, MB_LEN_MAX); | 74 l = mbtowc(&d, optstring + i, MB_LEN_MAX); |
78 » » if (l>0) i+=l; else i++; | 75 if (l > 0) |
79 » } while (l && d != c); | 76 i += l; |
| 77 else |
| 78 i++; |
| 79 } while (l && d != c); |
80 | 80 |
81 » if (d != c) { | 81 if (d != c) { |
82 » » if (optstring[0] != ':' && opterr) | 82 if (optstring[0] != ':' && opterr) |
83 » » » __getopt_msg(argv[0], ": unrecognized option: ", optchar
, k); | 83 __getopt_msg(argv[0], ": unrecognized option: ", optchar, k); |
84 » » return '?'; | 84 return '?'; |
85 » } | 85 } |
86 » if (optstring[i] == ':') { | 86 if (optstring[i] == ':') { |
87 » » if (optstring[i+1] == ':') optarg = 0; | 87 if (optstring[i + 1] == ':') |
88 » » else if (optind >= argc) { | 88 optarg = 0; |
89 » » » if (optstring[0] == ':') return ':'; | 89 else if (optind >= argc) { |
90 » » » if (opterr) __getopt_msg(argv[0], | 90 if (optstring[0] == ':') |
91 » » » » ": option requires an argument: ", | 91 return ':'; |
92 » » » » optchar, k); | 92 if (opterr) |
93 » » » return '?'; | 93 __getopt_msg(argv[0], ": option requires an argument: ", optchar, k); |
94 » » } | 94 return '?'; |
95 » » if (optstring[i+1] != ':' || optpos) { | 95 } |
96 » » » optarg = argv[optind++] + optpos; | 96 if (optstring[i + 1] != ':' || optpos) { |
97 » » » optpos = 0; | 97 optarg = argv[optind++] + optpos; |
98 » » } | 98 optpos = 0; |
99 » } | 99 } |
100 » return c; | 100 } |
| 101 return c; |
101 } | 102 } |
102 | 103 |
103 weak_alias(getopt, __posix_getopt); | 104 weak_alias(getopt, __posix_getopt); |
OLD | NEW |