13 #include "stdio_impl.h"
16 #include "floatscan.h"
25 static void store_int(void *dest, int size, unsigned long long i)
42 *(long long *)dest = i;
47 static void *arg_n(va_list ap, unsigned int n)
53 for (i=n; i>1; i--) va_arg(ap2, void *);
54 p = va_arg(ap2, void *);
59 static int readwc(int c, wchar_t **wcs, mbstate_t *st)
63 switch (mbrtowc(&wc, &ch, 1, st)) {
69 if (*wcs) *(*wcs)++ = wc;
74 int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap)
80 const unsigned char *p;
91 unsigned char scanset[257];
95 for (p=(const unsigned char *)fmt; *p; p++) {
98 while (isspace(p[1])) p++;
100 while (isspace(shgetc(f)));
105 if (*p != '%' || p[1] == '%') {
111 if (c<0) goto input_fail;
121 } else if (isdigit(*p) && p[1]=='$') {
122 dest = arg_n(ap, *p-'0'); p+=2;
124 dest = va_arg(ap, void *);
127 for (width=0; isdigit(*p); p++) {
128 width = 10*width + *p - '0';
141 if (*p == 'h') p++, size = SIZE_hh;
145 if (*p == 'l') p++, size = SIZE_ll;
158 case 'd': case 'i': case 'o': case 'u': case 'x':
159 case 'a': case 'e': case 'f': case 'g':
160 case 'A': case 'E': case 'F': case 'G': case 'X':
161 case 's': case 'c': case '[':
180 if (width < 1) width = 1;
184 store_int(dest, size, pos);
185 /* do not increment match count, etc! */
189 while (isspace(shgetc(f)));
195 if (shgetc(f) < 0) goto input_fail;
202 if (t == 'c' || t == 's') {
203 memset(scanset, -1, sizeof scanset);
214 if (*++p == '^') p++, invert = 1;
216 memset(scanset, invert, sizeof scanset);
218 if (*p == '-') p++, scanset[1+'-'] = 1-invert;
219 else if (*p == ']') p++, scanset[1+']'] = 1-invert;
220 for (; *p != ']'; p++) {
221 if (!*p) goto fmt_fail;
222 if (*p=='-' && p[1] && p[1] != ']')
223 for (c=p++[-1]; c<*p; c++)
224 scanset[1+c] = 1-invert;
225 scanset[1+*p] = 1-invert;
230 if (size == SIZE_l) {
233 while (scanset[(c=shgetc(f))+1]) {
234 if (readwc(c, &wcs, &st) < 0)
237 if (!mbsinit(&st)) goto input_fail;
238 } else if ((s = dest)) {
239 while (scanset[(c=shgetc(f))+1])
242 while (scanset[(c=shgetc(f))+1]);
245 if (!shcnt(f)) goto match_fail;
246 if (t == 'c' && shcnt(f) != width) goto match_fail;
265 x = __intscan(f, base, 0, ULLONG_MAX);
266 if (!shcnt(f)) goto match_fail;
267 if (t=='p' && dest) *(void **)dest = (void *)(uintptr_t)x;
268 else store_int(dest, size, x);
274 y = __floatscan(f, size, 0);
275 if (!shcnt(f)) goto match_fail;
276 if (dest) switch (size) {
284 *(long double *)dest = y;
296 if (!matches) matches--;