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] == '%') {
110 if (c<0) goto input_fail;
120 } else if (isdigit(*p) && p[1]=='$') {
121 dest = arg_n(ap, *p-'0'); p+=2;
123 dest = va_arg(ap, void *);
126 for (width=0; isdigit(*p); p++) {
127 width = 10*width + *p - '0';
140 if (*p == 'h') p++, size = SIZE_hh;
144 if (*p == 'l') p++, size = SIZE_ll;
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 '[':
174 if (width < 1) width = 1;
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':
182 if (width < 1) width = 0;
191 if (shgetc(f) < 0) goto input_fail;
197 store_int(dest, size, pos);
198 /* do not increment match count, etc! */
202 st = (mbstate_t){ 0 };
203 while ((c=shgetc(f)) >= 0) {
204 if (readwc(c, &wcs, &st) < 0)
207 if (!mbsinit(&st)) goto input_fail;
208 if (shcnt(f) != width) goto match_fail;
213 while ((c=shgetc(f)) >= 0) *s++ = c;
215 while (shgetc(f)>=0);
217 if (shcnt(f) < width) goto match_fail;
223 if (*++p == '^') p++, invert = 1;
226 unsigned char scanset[257];
227 memset(scanset, invert, sizeof scanset);
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;
240 if (size == SIZE_l) {
242 while (scanset[(c=shgetc(f))+1]) {
243 if (readwc(c, &wcs, &st) < 0)
246 if (!mbsinit(&st)) goto input_fail;
249 while (scanset[(c=shgetc(f))+1])
253 while (scanset[(c=shgetc(f))+1]);
256 if (!shcnt(f)) goto match_fail;
262 while (isspace(shgetc(f)));
266 if (shgetc(f) < 0) goto input_fail;
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);
295 y = __floatscan(f, size, 0);
296 if (!shcnt(f)) goto match_fail;
297 if (dest) switch (size) {
305 *(long double *)dest = y;
311 st = (mbstate_t){ 0 };
312 while (!isspace(c=shgetc(f)) && c!=EOF) {
313 if (readwc(c, &wcs, &st) < 0)
317 if (!mbsinit(&st)) goto input_fail;
318 if (dest) *wcs++ = 0;
323 while (!isspace(c=shgetc(f)) && c!=EOF)
327 while (!isspace(c=shgetc(f)) && c!=EOF);
339 if (!matches) matches--;