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 *f, const char *fmt, va_list ap)
80 const unsigned char *p;
94 for (p=(const unsigned char *)fmt; *p; p++) {
97 while (isspace(p[1])) p++;
99 while (isspace(shgetc(f)));
104 if (*p != '%' || p[1] == '%') {
109 if (c<0) goto input_fail;
119 } else if (isdigit(*p) && p[1]=='$') {
120 dest = arg_n(ap, *p-'0'); p+=2;
122 dest = va_arg(ap, void *);
125 for (width=0; isdigit(*p); p++) {
126 width = 10*width + *p - '0';
139 if (*p == 'h') p++, size = SIZE_hh;
143 if (*p == 'l') p++, size = SIZE_ll;
156 case 'd': case 'i': case 'o': case 'u': case 'x':
157 case 'a': case 'e': case 'f': case 'g':
158 case 'A': case 'E': case 'F': case 'G': case 'X':
159 case 's': case 'c': case '[':
173 if (width < 1) width = 1;
175 if (size == SIZE_l) t &= ~0x20;
176 case 'd': case 'i': case 'o': case 'u': case 'x':
177 case 'a': case 'e': case 'f': case 'g':
178 case 'A': case 'E': case 'F': case 'G': case 'X':
181 if (width < 1) width = 0;
190 if (shgetc(f) < 0) goto input_fail;
196 store_int(dest, size, pos);
197 /* do not increment match count, etc! */
201 st = (mbstate_t){ 0 };
202 while ((c=shgetc(f)) >= 0) {
203 if (readwc(c, &wcs, &st) < 0)
206 if (!mbsinit(&st)) goto input_fail;
207 if (shcnt(f) != width) goto match_fail;
212 while ((c=shgetc(f)) >= 0) *s++ = c;
214 while (shgetc(f)>=0);
216 if (shcnt(f) < width) goto match_fail;
222 if (*++p == '^') p++, invert = 1;
225 unsigned char scanset[257];
226 memset(scanset, invert, sizeof scanset);
229 if (*p == '-') p++, scanset[1+'-'] = 1-invert;
230 else if (*p == ']') p++, scanset[1+']'] = 1-invert;
231 for (; *p != ']'; p++) {
232 if (!*p) goto fmt_fail;
233 if (*p=='-' && p[1] && p[1] != ']')
234 for (c=p++[-1]; c<*p; c++)
235 scanset[1+c] = 1-invert;
236 scanset[1+*p] = 1-invert;
239 if (size == SIZE_l) {
241 while (scanset[(c=shgetc(f))+1]) {
242 if (readwc(c, &wcs, &st) < 0)
245 if (!mbsinit(&st)) goto input_fail;
248 while (scanset[(c=shgetc(f))+1])
252 while (scanset[(c=shgetc(f))+1]);
255 if (!shcnt(f)) goto match_fail;
261 while (isspace(shgetc(f)));
265 if (shgetc(f) < 0) goto input_fail;
285 x = __intscan(f, base, 0, ULLONG_MAX);
286 if (!shcnt(f)) goto match_fail;
287 if (t=='p' && dest) *(void **)dest = (void *)(uintptr_t)x;
288 else store_int(dest, size, x);
294 y = __floatscan(f, size, 0);
295 if (!shcnt(f)) goto match_fail;
296 if (dest) switch (size) {
304 *(long double *)dest = y;
310 st = (mbstate_t){ 0 };
311 while (!isspace(c=shgetc(f)) && c!=EOF) {
312 if (readwc(c, &wcs, &st) < 0)
316 if (!mbsinit(&st)) goto input_fail;
317 if (dest) *wcs++ = 0;
322 while (!isspace(c=shgetc(f)) && c!=EOF)
326 while (!isspace(c=shgetc(f)) && c!=EOF);
338 if (!matches) matches--;