fix usage of locks with vfork
[musl] / src / stdio / vfscanf.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <ctype.h>
5 #include <wchar.h>
6 #include <wctype.h>
7 #include <limits.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <math.h>
11 #include <float.h>
12
13 #include "stdio_impl.h"
14 #include "shgetc.h"
15 #include "intscan.h"
16 #include "floatscan.h"
17
18 #define SIZE_hh -2
19 #define SIZE_h  -1
20 #define SIZE_def 0
21 #define SIZE_l   1
22 #define SIZE_L   2
23 #define SIZE_ll  3
24
25 static void store_int(void *dest, int size, unsigned long long i)
26 {
27         if (!dest) return;
28         switch (size) {
29         case SIZE_hh:
30                 *(char *)dest = i;
31                 break;
32         case SIZE_h:
33                 *(short *)dest = i;
34                 break;
35         case SIZE_def:
36                 *(int *)dest = i;
37                 break;
38         case SIZE_l:
39                 *(long *)dest = i;
40                 break;
41         case SIZE_ll:
42                 *(long long *)dest = i;
43                 break;
44         }
45 }
46
47 static void *arg_n(va_list ap, unsigned int n)
48 {
49         void *p;
50         unsigned int i;
51         va_list ap2;
52         va_copy(ap2, ap);
53         for (i=n; i>1; i--) va_arg(ap2, void *);
54         p = va_arg(ap2, void *);
55         va_end(ap2);
56         return p;
57 }
58
59 static int readwc(int c, wchar_t **wcs, mbstate_t *st)
60 {
61         char ch = c;
62         wchar_t wc;
63         switch (mbrtowc(&wc, &ch, 1, st)) {
64         case -1:
65                 return -1;
66         case -2:
67                 break;
68         default:
69                 if (*wcs) *(*wcs)++ = wc;
70         }
71         return 0;
72 }
73
74 int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap)
75 {
76         int width;
77         int size;
78         int alloc;
79         int base;
80         const unsigned char *p;
81         int c, t;
82         char *s;
83         wchar_t *wcs;
84         mbstate_t st;
85         void *dest=NULL;
86         int invert;
87         int matches=0;
88         unsigned long long x;
89         long double y;
90         off_t pos = 0;
91
92         FLOCK(f);
93
94         for (p=(const unsigned char *)fmt; *p; p++) {
95
96                 if (isspace(*p)) {
97                         while (isspace(p[1])) p++;
98                         shlim(f, 0);
99                         while (isspace(shgetc(f)));
100                         shunget(f);
101                         pos += shcnt(f);
102                         continue;
103                 }
104                 if (*p != '%' || p[1] == '%') {
105                         p += *p=='%';
106                         shlim(f, 0);
107                         c = shgetc(f);
108                         if (c!=*p) {
109                                 shunget(f);
110                                 if (c<0) goto input_fail;
111                                 goto match_fail;
112                         }
113                         pos++;
114                         continue;
115                 }
116
117                 p++;
118                 if (*p=='*') {
119                         dest = 0; p++;
120                 } else if (isdigit(*p) && p[1]=='$') {
121                         dest = arg_n(ap, *p-'0'); p+=2;
122                 } else {
123                         dest = va_arg(ap, void *);
124                 }
125
126                 for (width=0; isdigit(*p); p++) {
127                         width = 10*width + *p - '0';
128                 }
129
130                 if (*p=='m') {
131                         alloc = 1;
132                         p++;
133                 } else {
134                         alloc = 0;
135                 }
136
137                 size = SIZE_def;
138                 switch (*p++) {
139                 case 'h':
140                         if (*p == 'h') p++, size = SIZE_hh;
141                         else size = SIZE_h;
142                         break;
143                 case 'l':
144                         if (*p == 'l') p++, size = SIZE_ll;
145                         else size = SIZE_l;
146                         break;
147                 case 'j':
148                         size = SIZE_ll;
149                         break;
150                 case 'z':
151                 case 't':
152                         size = SIZE_l;
153                         break;
154                 case 'L':
155                         size = SIZE_L;
156                         break;
157                 case 'd': case 'i': case 'o': case 'u': case 'x':
158                 case 'a': case 'e': case 'f': case 'g':
159                 case 'A': case 'E': case 'F': case 'G': case 'X':
160                 case 's': case 'c': case '[':
161                 case 'S': case 'C':
162                 case 'p': case 'n':
163                         p--;
164                         break;
165                 default:
166                         goto fmt_fail;
167                 }
168
169                 t = *p;
170
171                 switch (t) {
172                 case 'C':
173                 case 'c':
174                         if (width < 1) width = 1;
175                 case 's':
176                         if (size == SIZE_l) t &= ~0x20;
177                 case 'd': case 'i': case 'o': case 'u': case 'x':
178                 case 'a': case 'e': case 'f': case 'g':
179                 case 'A': case 'E': case 'F': case 'G': case 'X':
180                 case '[': case 'S':
181                 case 'p': case 'n':
182                         if (width < 1) width = 0;
183                         break;
184                 default:
185                         goto fmt_fail;
186                 }
187
188                 shlim(f, width);
189
190                 if (t != 'n') {
191                         if (shgetc(f) < 0) goto input_fail;
192                         shunget(f);
193                 }
194
195                 switch (t) {
196                 case 'n':
197                         store_int(dest, size, pos);
198                         /* do not increment match count, etc! */
199                         continue;
200                 case 'C':
201                         wcs = dest;
202                         st = (mbstate_t){ 0 };
203                         while ((c=shgetc(f)) >= 0) {
204                                 if (readwc(c, &wcs, &st) < 0)
205                                         goto input_fail;
206                         }
207                         if (!mbsinit(&st)) goto input_fail;
208                         if (shcnt(f) != width) goto match_fail;
209                         break;
210                 case 'c':
211                         if (dest) {
212                                 s = dest;
213                                 while ((c=shgetc(f)) >= 0) *s++ = c;
214                         } else {
215                                 while (shgetc(f)>=0);
216                         }
217                         if (shcnt(f) < width) goto match_fail;
218                         break;
219                 case '[':
220                         s = dest;
221                         wcs = dest;
222
223                         if (*++p == '^') p++, invert = 1;
224                         else invert = 0;
225
226                         unsigned char scanset[257];
227                         memset(scanset, invert, sizeof scanset);
228
229                         scanset[0] = 0;
230                         if (*p == '-') p++, scanset[1+'-'] = 1-invert;
231                         else if (*p == ']') p++, scanset[1+']'] = 1-invert;
232                         for (; *p != ']'; p++) {
233                                 if (!*p) goto fmt_fail;
234                                 if (*p=='-' && p[1] && p[1] != ']')
235                                         for (c=p++[-1]; c<*p; c++)
236                                                 scanset[1+c] = 1-invert;
237                                 scanset[1+*p] = 1-invert;
238                         }
239
240                         if (size == SIZE_l) {
241                                 st = (mbstate_t){0};
242                                 while (scanset[(c=shgetc(f))+1]) {
243                                         if (readwc(c, &wcs, &st) < 0)
244                                                 goto input_fail;
245                                 }
246                                 if (!mbsinit(&st)) goto input_fail;
247                                 s = 0;
248                         } else if (s) {
249                                 while (scanset[(c=shgetc(f))+1])
250                                         *s++ = c;
251                                 wcs = 0;
252                         } else {
253                                 while (scanset[(c=shgetc(f))+1]);
254                         }
255                         shunget(f);
256                         if (!shcnt(f)) goto match_fail;
257                         if (s) *s = 0;
258                         if (wcs) *wcs = 0;
259                         break;
260                 default:
261                         shlim(f, 0);
262                         while (isspace(shgetc(f)));
263                         shunget(f);
264                         pos += shcnt(f);
265                         shlim(f, width);
266                         if (shgetc(f) < 0) goto input_fail;
267                         shunget(f);
268                 }
269
270                 switch (t) {
271                 case 'p':
272                 case 'X':
273                 case 'x':
274                         base = 16;
275                         goto int_common;
276                 case 'o':
277                         base = 8;
278                         goto int_common;
279                 case 'd':
280                 case 'u':
281                         base = 10;
282                         goto int_common;
283                 case 'i':
284                         base = 0;
285                 int_common:
286                         x = __intscan(f, base, 0, ULLONG_MAX);
287                         if (!shcnt(f)) goto match_fail;
288                         if (t=='p' && dest) *(void **)dest = (void *)(uintptr_t)x;
289                         else store_int(dest, size, x);
290                         break;
291                 case 'a': case 'A':
292                 case 'e': case 'E':
293                 case 'f': case 'F':
294                 case 'g': case 'G':
295                         y = __floatscan(f, size, 0);
296                         if (!shcnt(f)) goto match_fail;
297                         if (dest) switch (size) {
298                         case SIZE_def:
299                                 *(float *)dest = y;
300                                 break;
301                         case SIZE_l:
302                                 *(double *)dest = y;
303                                 break;
304                         case SIZE_L:
305                                 *(long double *)dest = y;
306                                 break;
307                         }
308                         break;
309                 case 'S':
310                         wcs = dest;
311                         st = (mbstate_t){ 0 };
312                         while (!isspace(c=shgetc(f)) && c!=EOF) {
313                                 if (readwc(c, &wcs, &st) < 0)
314                                         goto input_fail;
315                         }
316                         shunget(f);
317                         if (!mbsinit(&st)) goto input_fail;
318                         if (dest) *wcs++ = 0;
319                         break;
320                 case 's':
321                         if (dest) {
322                                 s = dest;
323                                 while (!isspace(c=shgetc(f)) && c!=EOF)
324                                         *s++ = c;
325                                 *s = 0;
326                         } else {
327                                 while (!isspace(c=shgetc(f)) && c!=EOF);
328                         }
329                         shunget(f);
330                         break;
331                 }
332
333                 pos += shcnt(f);
334                 if (dest) matches++;
335         }
336         if (0) {
337 fmt_fail:
338 input_fail:
339                 if (!matches) matches--;
340         }
341 match_fail:
342         FUNLOCK(f);
343         return matches;
344 }