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