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 '[':
174 if (width < 1) width = 1;
180 if (width < 1) width = 1;
181 case 'd': case 'i': case 'o': case 'u': case 'x':
182 case 'a': case 'e': case 'f': case 'g':
183 case 'A': case 'E': case 'F': case 'G': case 'X':
192 store_int(dest, size, pos);
193 /* do not increment match count, etc! */
197 if (t != '[' && (t|32) != 'c') {
199 while (isspace(shgetc(f)));
205 if (shgetc(f) < 0) goto input_fail;
212 if (t == 'c' || t == 's') {
213 memset(scanset, -1, sizeof scanset);
224 if (*++p == '^') p++, invert = 1;
226 memset(scanset, invert, sizeof scanset);
228 if (*p == '-') p++, scanset[1+'-'] = 1-invert;
229 else if (*p == ']') p++, scanset[1+']'] = 1-invert;
230 for (; *p != ']'; p++) {
231 if (!*p) goto fmt_fail;
232 if (*p=='-' && p[1] && p[1] != ']')
233 for (c=p++[-1]; c<*p; c++)
234 scanset[1+c] = 1-invert;
235 scanset[1+*p] = 1-invert;
240 if (size == SIZE_l) {
243 while (scanset[(c=shgetc(f))+1]) {
244 if (readwc(c, &wcs, &st) < 0)
247 if (!mbsinit(&st)) goto input_fail;
248 } else if ((s = dest)) {
249 while (scanset[(c=shgetc(f))+1])
252 while (scanset[(c=shgetc(f))+1]);
255 if (!shcnt(f)) goto match_fail;
256 if (t == 'c' && shcnt(f) != width) goto match_fail;
275 x = __intscan(f, base, 0, ULLONG_MAX);
276 if (!shcnt(f)) goto match_fail;
277 if (t=='p' && dest) *(void **)dest = (void *)(uintptr_t)x;
278 else store_int(dest, size, x);
284 y = __floatscan(f, size, 0);
285 if (!shcnt(f)) goto match_fail;
286 if (dest) switch (size) {
294 *(long double *)dest = y;
306 if (!matches) matches--;