10 #include "stdio_impl.h"
13 #include "floatscan.h"
22 static void store_int(void *dest, int size, unsigned long long i)
39 *(long long *)dest = i;
44 static void *arg_n(va_list ap, unsigned int n)
50 for (i=n; i>1; i--) va_arg(ap2, void *);
51 p = va_arg(ap2, void *);
56 int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap)
62 const unsigned char *p;
73 unsigned char scanset[257];
79 for (p=(const unsigned char *)fmt; *p; p++) {
84 while (isspace(p[1])) p++;
86 while (isspace(shgetc(f)));
91 if (*p != '%' || p[1] == '%') {
97 if (c<0) goto input_fail;
107 } else if (isdigit(*p) && p[1]=='$') {
108 dest = arg_n(ap, *p-'0'); p+=2;
110 dest = va_arg(ap, void *);
113 for (width=0; isdigit(*p); p++) {
114 width = 10*width + *p - '0';
129 if (*p == 'h') p++, size = SIZE_hh;
133 if (*p == 'l') p++, size = SIZE_ll;
146 case 'd': case 'i': case 'o': case 'u': case 'x':
147 case 'a': case 'e': case 'f': case 'g':
148 case 'A': case 'E': case 'F': case 'G': case 'X':
149 case 's': case 'c': case '[':
168 if (width < 1) width = 1;
172 store_int(dest, size, pos);
173 /* do not increment match count, etc! */
177 while (isspace(shgetc(f)));
183 if (shgetc(f) < 0) goto input_fail;
190 if (t == 'c' || t == 's') {
191 memset(scanset, -1, sizeof scanset);
202 if (*++p == '^') p++, invert = 1;
204 memset(scanset, invert, sizeof scanset);
206 if (*p == '-') p++, scanset[1+'-'] = 1-invert;
207 else if (*p == ']') p++, scanset[1+']'] = 1-invert;
208 for (; *p != ']'; p++) {
209 if (!*p) goto fmt_fail;
210 if (*p=='-' && p[1] && p[1] != ']')
211 for (c=p++[-1]; c<*p; c++)
212 scanset[1+c] = 1-invert;
213 scanset[1+*p] = 1-invert;
219 k = t=='c' ? width+1U : 31;
220 if (size == SIZE_l) {
222 wcs = malloc(k*sizeof(wchar_t));
223 if (!wcs) goto alloc_fail;
228 while (scanset[(c=shgetc(f))+1]) {
229 switch (mbrtowc(&wc, &(char){c}, 1, &st)) {
235 if (wcs) wcs[i++] = wc;
238 wchar_t *tmp = realloc(wcs, k*sizeof(wchar_t));
239 if (!tmp) goto alloc_fail;
243 if (!mbsinit(&st)) goto input_fail;
246 if (!s) goto alloc_fail;
247 while (scanset[(c=shgetc(f))+1]) {
251 char *tmp = realloc(s, k);
252 if (!tmp) goto alloc_fail;
256 } else if ((s = dest)) {
257 while (scanset[(c=shgetc(f))+1])
260 while (scanset[(c=shgetc(f))+1]);
263 if (!shcnt(f)) goto match_fail;
264 if (t == 'c' && shcnt(f) != width) goto match_fail;
266 if (size == SIZE_l) *(wchar_t **)dest = wcs;
267 else *(char **)dest = s;
289 x = __intscan(f, base, 0, ULLONG_MAX);
290 if (!shcnt(f)) goto match_fail;
291 if (t=='p' && dest) *(void **)dest = (void *)(uintptr_t)x;
292 else store_int(dest, size, x);
298 y = __floatscan(f, size, 0);
299 if (!shcnt(f)) goto match_fail;
300 if (dest) switch (size) {
308 *(long double *)dest = y;
321 if (!matches) matches--;
332 weak_alias(vfscanf,__isoc99_vfscanf);