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 int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap)
65 const unsigned char *p;
76 unsigned char scanset[257];
82 for (p=(const unsigned char *)fmt; *p; p++) {
85 while (isspace(p[1])) p++;
87 while (isspace(shgetc(f)));
92 if (*p != '%' || p[1] == '%') {
98 if (c<0) goto input_fail;
108 } else if (isdigit(*p) && p[1]=='$') {
109 dest = arg_n(ap, *p-'0'); p+=2;
111 dest = va_arg(ap, void *);
114 for (width=0; isdigit(*p); p++) {
115 width = 10*width + *p - '0';
128 if (*p == 'h') p++, size = SIZE_hh;
132 if (*p == 'l') p++, size = SIZE_ll;
145 case 'd': case 'i': case 'o': case 'u': case 'x':
146 case 'a': case 'e': case 'f': case 'g':
147 case 'A': case 'E': case 'F': case 'G': case 'X':
148 case 's': case 'c': case '[':
167 if (width < 1) width = 1;
171 store_int(dest, size, pos);
172 /* do not increment match count, etc! */
176 while (isspace(shgetc(f)));
182 if (shgetc(f) < 0) goto input_fail;
189 if (t == 'c' || t == 's') {
190 memset(scanset, -1, sizeof scanset);
201 if (*++p == '^') p++, invert = 1;
203 memset(scanset, invert, sizeof scanset);
205 if (*p == '-') p++, scanset[1+'-'] = 1-invert;
206 else if (*p == ']') p++, scanset[1+']'] = 1-invert;
207 for (; *p != ']'; p++) {
208 if (!*p) goto fmt_fail;
209 if (*p=='-' && p[1] && p[1] != ']')
210 for (c=p++[-1]; c<*p; c++)
211 scanset[1+c] = 1-invert;
212 scanset[1+*p] = 1-invert;
218 k = t=='c' ? width+1U : 31;
219 if (size == SIZE_l) {
221 wcs = malloc(k*sizeof(wchar_t));
222 if (!wcs) goto alloc_fail;
227 while (scanset[(c=shgetc(f))+1]) {
228 switch (mbrtowc(&wc, &(char){c}, 1, &st)) {
234 if (wcs) wcs[i++] = wc;
237 wchar_t *tmp = realloc(wcs, k*sizeof(wchar_t));
238 if (!tmp) goto alloc_fail;
242 if (!mbsinit(&st)) goto input_fail;
245 if (!s) goto alloc_fail;
246 while (scanset[(c=shgetc(f))+1]) {
250 char *tmp = realloc(s, k);
251 if (!tmp) goto alloc_fail;
255 } else if ((s = dest)) {
256 while (scanset[(c=shgetc(f))+1])
259 while (scanset[(c=shgetc(f))+1]);
262 if (!shcnt(f)) goto match_fail;
263 if (t == 'c' && shcnt(f) != width) goto match_fail;
265 if (size == SIZE_l) *(wchar_t **)dest = wcs;
266 else *(char **)dest = s;
288 x = __intscan(f, base, 0, ULLONG_MAX);
289 if (!shcnt(f)) goto match_fail;
290 if (t=='p' && dest) *(void **)dest = (void *)(uintptr_t)x;
291 else store_int(dest, size, x);
297 y = __floatscan(f, size, 0);
298 if (!shcnt(f)) goto match_fail;
299 if (dest) switch (size) {
307 *(long double *)dest = y;
320 if (!matches) matches--;