blob: 14b175415961517e6fe2daad850962e0afba6969 [file] [log] [blame]
Elliott Hughesf1ada792014-05-02 17:56:56 -07001/* $OpenBSD: vfwscanf.c,v 1.4 2014/03/19 05:17:01 guenther Exp $ */
Elliott Hughes01ae00f2014-04-29 16:28:56 -07002/*-
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
zijunzhao78904842023-05-09 00:54:00 +000034#include "scanf_common.h"
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -080035// An interpretive version of __sccl from vfscanf.c --- a table of all wchar_t values would
36// be a little too expensive, and some kind of compressed version isn't worth the trouble.
37static inline bool in_ccl(wchar_t wc, const wchar_t* ccl) {
38 // Is this a negated set?
39 bool member_result = true;
40 if (*ccl == '^') {
41 member_result = false;
42 ++ccl;
43 }
Elliott Hughes01ae00f2014-04-29 16:28:56 -070044
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -080045 // The first character may be ']' or '-' without being special.
46 if (*ccl == '-' || *ccl == ']') {
47 // A literal match?
48 if (*ccl == wc) return member_result;
49 ++ccl;
50 }
51
52 while (*ccl && *ccl != ']') {
53 // The last character may be '-' without being special.
54 if (*ccl == '-' && ccl[1] != '\0' && ccl[1] != ']') {
55 wchar_t first = *(ccl - 1);
56 wchar_t last = *(ccl + 1);
57 if (first <= last) {
58 // In the range?
59 if (wc >= first && wc <= last) return member_result;
60 ccl += 2;
61 continue;
62 }
63 // A '-' is not considered to be part of a range if the character after
64 // is not greater than the character before, so fall through...
65 }
66 // A literal match?
67 if (*ccl == wc) return member_result;
68 ++ccl;
69 }
70 return !member_result;
71}
Elliott Hughes01ae00f2014-04-29 16:28:56 -070072
Elliott Hughes7f0849f2016-08-26 16:17:17 -070073#pragma GCC diagnostic push
74#pragma GCC diagnostic ignored "-Wframe-larger-than="
75
Elliott Hughes01ae00f2014-04-29 16:28:56 -070076/*
77 * vfwscanf
78 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070079int __vfwscanf(FILE* __restrict fp, const wchar_t* __restrict fmt, __va_list ap) {
80 wint_t c; /* character from format, or conversion */
81 size_t width; /* field width, or 0 */
82 wchar_t* p; /* points into all kinds of strings */
83 int n; /* handy integer */
84 int flags; /* flags as defined above */
85 wchar_t* p0; /* saves original value of p when necessary */
86 int nassigned; /* number of fields assigned */
87 int nconversions; /* number of conversions */
88 int nread; /* number of characters consumed from fp */
89 int base; /* base argument to strtoimax/strtouimax */
90 wchar_t buf[BUF]; /* buffer for numeric conversions */
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -080091 const wchar_t* ccl;
Elliott Hughesc8f2c522017-10-31 13:07:51 -070092 wint_t wi; /* handy wint_t */
93 char* mbp; /* multibyte string pointer for %c %s %[ */
94 size_t nconv; /* number of bytes in mb. conversion */
95 char mbbuf[MB_LEN_MAX]; /* temporary mb. character buffer */
96 mbstate_t mbs;
Elliott Hughes01ae00f2014-04-29 16:28:56 -070097
Elliott Hughes531199c2023-05-09 16:11:49 -070098 _SET_ORIENTATION(fp, ORIENT_CHARS);
Elliott Hughes01ae00f2014-04-29 16:28:56 -070099
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700100 nassigned = 0;
101 nconversions = 0;
102 nread = 0;
103 base = 0; /* XXX just to keep gcc happy */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700104 for (;;) {
105 c = *fmt++;
106 if (c == 0) {
107 return (nassigned);
108 }
109 if (iswspace(c)) {
110 while ((c = __fgetwc_unlock(fp)) != WEOF && iswspace(c))
111 ;
112 if (c != WEOF) __ungetwc(c, fp);
113 continue;
114 }
115 if (c != '%') goto literal;
116 width = 0;
117 flags = 0;
118 /*
119 * switch on the format. continue if done;
120 * break once format type is derived.
121 */
122 again:
123 c = *fmt++;
zijunzhao78904842023-05-09 00:54:00 +0000124 reswitch:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700125 switch (c) {
126 case '%':
127 literal:
128 if ((wi = __fgetwc_unlock(fp)) == WEOF) goto input_failure;
129 if (wi != c) {
130 __ungetwc(wi, fp);
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -0800131 goto match_failure;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700132 }
133 nread++;
134 continue;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700135
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700136 case '*':
137 flags |= SUPPRESS;
138 goto again;
139 case 'j':
140 flags |= MAXINT;
141 goto again;
142 case 'L':
143 flags |= LONGDBL;
144 goto again;
145 case 'h':
146 if (*fmt == 'h') {
147 fmt++;
148 flags |= SHORTSHORT;
149 } else {
150 flags |= SHORT;
151 }
152 goto again;
153 case 'l':
154 if (*fmt == 'l') {
155 fmt++;
156 flags |= LLONG;
157 } else {
158 flags |= LONG;
159 }
160 goto again;
161 case 'q':
162 flags |= LLONG; /* deprecated */
163 goto again;
164 case 't':
165 flags |= PTRINT;
166 goto again;
167 case 'z':
168 flags |= SIZEINT;
169 goto again;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700170
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700171 case '0':
172 case '1':
173 case '2':
174 case '3':
175 case '4':
176 case '5':
177 case '6':
178 case '7':
179 case '8':
180 case '9':
181 width = width * 10 + c - '0';
182 goto again;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700183
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700184 /*
185 * Conversions.
186 * Those marked `compat' are for 4.[123]BSD compatibility.
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700187 */
Elliott Hughes1f462de2022-08-05 22:51:05 +0000188 case 'b':
189 c = CT_INT;
190 base = 2;
191 flags |= PFBOK; /* enable 0b prefixing */
192 break;
193
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700194 case 'D': /* compat */
195 flags |= LONG;
Elliott Hughes1f462de2022-08-05 22:51:05 +0000196 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700197 case 'd':
198 c = CT_INT;
199 base = 10;
200 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700201
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700202 case 'i':
203 c = CT_INT;
204 base = 0;
205 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700206
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700207 case 'O': /* compat */
208 flags |= LONG;
Elliott Hughes1f462de2022-08-05 22:51:05 +0000209 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700210 case 'o':
211 c = CT_INT;
212 flags |= UNSIGNED;
213 base = 8;
214 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700215
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700216 case 'u':
217 c = CT_INT;
218 flags |= UNSIGNED;
219 base = 10;
220 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700221
zijunzhao78904842023-05-09 00:54:00 +0000222 case 'w': {
223 int size = 0;
224 bool fast = false;
225 c = *fmt++;
226 while (is_digit(c)) {
227 APPEND_DIGIT(size, c);
228 c = *fmt++;
229 }
230 flags |= w_to_flag(size, fast);
231 goto reswitch;
232 }
233
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700234 case 'X':
235 case 'x':
236 flags |= PFXOK; /* enable 0x prefixing */
237 c = CT_INT;
238 flags |= UNSIGNED;
239 base = 16;
240 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700241
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700242 case 'e':
243 case 'E':
244 case 'f':
245 case 'F':
246 case 'g':
247 case 'G':
248 case 'a':
249 case 'A':
250 c = CT_FLOAT;
251 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700252
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700253 case 's':
254 c = CT_STRING;
255 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700256
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700257 case '[':
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800258 ccl = fmt;
259 if (*fmt == '^') fmt++;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700260 if (*fmt == ']') fmt++;
261 while (*fmt != '\0' && *fmt != ']') fmt++;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700262 fmt++;
263 flags |= NOSKIP;
264 c = CT_CCL;
265 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700266
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700267 case 'c':
268 flags |= NOSKIP;
269 c = CT_CHAR;
270 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700271
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700272 case 'p': /* pointer format is like hex */
273 flags |= POINTER | PFXOK;
274 c = CT_INT;
275 flags |= UNSIGNED;
276 base = 16;
277 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700278
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700279 case 'n':
280 nconversions++;
281 if (flags & SUPPRESS) continue;
282 if (flags & SHORTSHORT)
283 *va_arg(ap, signed char*) = nread;
284 else if (flags & SHORT)
285 *va_arg(ap, short*) = nread;
286 else if (flags & LONG)
287 *va_arg(ap, long*) = nread;
288 else if (flags & SIZEINT)
289 *va_arg(ap, ssize_t*) = nread;
290 else if (flags & PTRINT)
291 *va_arg(ap, ptrdiff_t*) = nread;
292 else if (flags & LLONG)
293 *va_arg(ap, long long*) = nread;
294 else if (flags & MAXINT)
295 *va_arg(ap, intmax_t*) = nread;
296 else
297 *va_arg(ap, int*) = nread;
298 continue;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700299
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700300 /*
301 * Disgusting backwards compatibility hacks. XXX
302 */
303 case '\0': /* compat */
304 return (EOF);
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700305
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700306 default: /* compat */
307 if (iswupper(c)) flags |= LONG;
308 c = CT_INT;
309 base = 10;
310 break;
311 }
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700312
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700313 /*
314 * Consume leading white space, except for formats
315 * that suppress this.
316 */
317 if ((flags & NOSKIP) == 0) {
318 while ((wi = __fgetwc_unlock(fp)) != WEOF && iswspace(wi)) nread++;
319 if (wi == WEOF) goto input_failure;
320 __ungetwc(wi, fp);
321 }
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700322
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700323 /*
324 * Do the conversion.
325 */
326 switch (c) {
327 case CT_CHAR:
328 /* scan arbitrary characters (sets NOSKIP) */
329 if (width == 0) width = 1;
330 if (flags & LONG) {
331 if (!(flags & SUPPRESS)) p = va_arg(ap, wchar_t*);
332 n = 0;
333 while (width-- != 0 && (wi = __fgetwc_unlock(fp)) != WEOF) {
334 if (!(flags & SUPPRESS)) *p++ = (wchar_t)wi;
335 n++;
336 }
337 if (n == 0) goto input_failure;
338 nread += n;
339 if (!(flags & SUPPRESS)) nassigned++;
340 } else {
341 if (!(flags & SUPPRESS)) mbp = va_arg(ap, char*);
342 n = 0;
343 memset(&mbs, 0, sizeof(mbs));
344 while (width != 0 && (wi = __fgetwc_unlock(fp)) != WEOF) {
345 if (width >= MB_CUR_MAX && !(flags & SUPPRESS)) {
346 nconv = wcrtomb(mbp, wi, &mbs);
347 if (nconv == (size_t)-1) goto input_failure;
348 } else {
349 nconv = wcrtomb(mbbuf, wi, &mbs);
350 if (nconv == (size_t)-1) goto input_failure;
351 if (nconv > width) {
352 __ungetwc(wi, fp);
353 break;
354 }
355 if (!(flags & SUPPRESS)) memcpy(mbp, mbbuf, nconv);
356 }
357 if (!(flags & SUPPRESS)) mbp += nconv;
358 width -= nconv;
359 n++;
360 }
361 if (n == 0) goto input_failure;
362 nread += n;
363 if (!(flags & SUPPRESS)) nassigned++;
364 }
365 nconversions++;
366 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700367
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700368 case CT_CCL:
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -0800369 case CT_STRING:
370 // CT_CCL: scan a (nonempty) character class (sets NOSKIP).
371 // CT_STRING: like CCL, but zero-length string OK, & no NOSKIP.
372 if (width == 0) width = (size_t)~0; // 'infinity'.
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700373 if ((flags & SUPPRESS) && (flags & LONG)) {
374 n = 0;
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -0800375 while ((wi = __fgetwc_unlock(fp)) != WEOF && width-- != 0 && ((c == CT_CCL && in_ccl(wi, ccl)) || (c == CT_STRING && !iswspace(wi)))) n++;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700376 if (wi != WEOF) __ungetwc(wi, fp);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700377 } else if (flags & LONG) {
378 p0 = p = va_arg(ap, wchar_t*);
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -0800379 while ((wi = __fgetwc_unlock(fp)) != WEOF && width-- != 0 && ((c == CT_CCL && in_ccl(wi, ccl)) || (c == CT_STRING && !iswspace(wi)))) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700380 *p++ = (wchar_t)wi;
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -0800381 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700382 if (wi != WEOF) __ungetwc(wi, fp);
383 n = p - p0;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700384 } else {
385 if (!(flags & SUPPRESS)) mbp = va_arg(ap, char*);
386 n = 0;
387 memset(&mbs, 0, sizeof(mbs));
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -0800388 while ((wi = __fgetwc_unlock(fp)) != WEOF && width != 0 && ((c == CT_CCL && in_ccl(wi, ccl)) || (c == CT_STRING && !iswspace(wi)))) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700389 if (width >= MB_CUR_MAX && !(flags & SUPPRESS)) {
390 nconv = wcrtomb(mbp, wi, &mbs);
391 if (nconv == (size_t)-1) goto input_failure;
392 } else {
393 nconv = wcrtomb(mbbuf, wi, &mbs);
394 if (nconv == (size_t)-1) goto input_failure;
395 if (nconv > width) break;
396 if (!(flags & SUPPRESS)) memcpy(mbp, mbbuf, nconv);
397 }
398 if (!(flags & SUPPRESS)) mbp += nconv;
399 width -= nconv;
400 n++;
401 }
402 if (wi != WEOF) __ungetwc(wi, fp);
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -0800403 }
404 if (c == CT_CCL && n == 0) goto match_failure;
405 if (!(flags & SUPPRESS)) {
406 if (flags & LONG) {
407 *p = L'\0';
408 } else {
409 *mbp = '\0';
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700410 }
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -0800411 ++nassigned;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700412 }
413 nread += n;
414 nconversions++;
415 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700416
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700417 case CT_INT:
418 /* scan an integer as if by strtoimax/strtoumax */
419 if (width == 0 || width > sizeof(buf) / sizeof(*buf) - 1)
420 width = sizeof(buf) / sizeof(*buf) - 1;
421 flags |= SIGNOK | NDIGITS | NZDIGITS;
422 for (p = buf; width; width--) {
423 c = __fgetwc_unlock(fp);
424 /*
425 * Switch on the character; `goto ok'
426 * if we accept it as a part of number.
427 */
428 switch (c) {
429 /*
430 * The digit 0 is always legal, but is
431 * special. For %i conversions, if no
432 * digits (zero or nonzero) have been
433 * scanned (only signs), we will have
434 * base==0. In that case, we should set
Elliott Hughes1f462de2022-08-05 22:51:05 +0000435 * it to 8 and enable 0b/0x prefixing.
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700436 * Also, if we have not scanned zero digits
437 * before this, do not turn off prefixing
438 * (someone else will turn it off if we
439 * have scanned any nonzero digits).
440 */
441 case '0':
442 if (base == 0) {
443 base = 8;
Elliott Hughes1f462de2022-08-05 22:51:05 +0000444 flags |= PFBOK | PFXOK;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700445 }
Elliott Hughes1f462de2022-08-05 22:51:05 +0000446 if (flags & NZDIGITS) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700447 flags &= ~(SIGNOK | NZDIGITS | NDIGITS);
Elliott Hughes1f462de2022-08-05 22:51:05 +0000448 } else {
449 flags &= ~(SIGNOK | PFBOK | PFXOK | NDIGITS);
450 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700451 goto ok;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700452
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700453 /* 1 through 7 always legal */
Elliott Hughes1f462de2022-08-05 22:51:05 +0000454 case 'B':
455 case 'b':
456 // Is this 'b' potentially part of an "0b" prefix?
457 if ((flags & PFBOK) && p == buf + 1 + !!(flags & HAVESIGN)) {
458 base = 2;
459 flags &= ~PFBOK;
460 goto ok;
461 }
462 // No? Fall through and see if it's a hex digit instead then...
463 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700464 case '1':
465 case '2':
466 case '3':
467 case '4':
468 case '5':
469 case '6':
470 case '7':
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700471 case '8':
472 case '9':
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700473 case 'A':
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700474 case 'C':
475 case 'D':
476 case 'E':
477 case 'F':
478 case 'a':
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700479 case 'c':
480 case 'd':
481 case 'e':
482 case 'f':
Elliott Hughes1f462de2022-08-05 22:51:05 +0000483 if (base == 0) base = 10;
484 if (base != 16 && (int)(c - '0') >= base) break; /* not legal here */
485 flags &= ~(SIGNOK | PFBOK | PFXOK | NDIGITS);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700486 goto ok;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700487
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700488 /* sign ok only as first character */
489 case '+':
490 case '-':
491 if (flags & SIGNOK) {
492 flags &= ~SIGNOK;
493 flags |= HAVESIGN;
494 goto ok;
495 }
496 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700497
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700498 /*
499 * x ok iff flag still set and 2nd char (or
500 * 3rd char if we have a sign).
501 */
502 case 'x':
503 case 'X':
504 if ((flags & PFXOK) && p == buf + 1 + !!(flags & HAVESIGN)) {
505 base = 16; /* if %i */
506 flags &= ~PFXOK;
507 goto ok;
508 }
509 break;
510 }
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700511
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700512 /*
513 * If we got here, c is not a legal character
514 * for a number. Stop accumulating digits.
515 */
516 if (c != WEOF) __ungetwc(c, fp);
517 break;
518 ok:
519 /*
520 * c is legal: store it and look at the next.
521 */
522 *p++ = (wchar_t)c;
523 }
524 /*
Elliott Hughes1f462de2022-08-05 22:51:05 +0000525 * If we had only a sign, it is no good; push back the sign.
526 * If the number was `[-+]0[BbXx]`, push back and treat it
527 * as `[-+]0`.
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700528 */
529 if (flags & NDIGITS) {
530 if (p > buf) __ungetwc(*--p, fp);
531 goto match_failure;
532 }
533 c = p[-1];
Elliott Hughes1f462de2022-08-05 22:51:05 +0000534 if ((base == 2 && (c == 'b' || c == 'B')) || c == 'x' || c == 'X') {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700535 --p;
536 __ungetwc(c, fp);
537 }
538 if ((flags & SUPPRESS) == 0) {
539 uintmax_t res;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700540
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700541 *p = '\0';
542 if (flags & UNSIGNED)
543 res = wcstoimax(buf, NULL, base);
544 else
545 res = wcstoumax(buf, NULL, base);
546 if (flags & POINTER)
547 *va_arg(ap, void**) = (void*)(uintptr_t)res;
548 else if (flags & MAXINT)
549 *va_arg(ap, intmax_t*) = res;
550 else if (flags & LLONG)
551 *va_arg(ap, long long*) = res;
552 else if (flags & SIZEINT)
553 *va_arg(ap, ssize_t*) = res;
554 else if (flags & PTRINT)
555 *va_arg(ap, ptrdiff_t*) = res;
556 else if (flags & LONG)
557 *va_arg(ap, long*) = res;
558 else if (flags & SHORT)
559 *va_arg(ap, short*) = res;
560 else if (flags & SHORTSHORT)
561 *va_arg(ap, signed char*) = res;
562 else
563 *va_arg(ap, int*) = res;
564 nassigned++;
565 }
566 nread += p - buf;
567 nconversions++;
568 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700569
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700570 case CT_FLOAT:
571 /* scan a floating point number as if by strtod */
572 if (width == 0 || width > sizeof(buf) / sizeof(*buf) - 1)
573 width = sizeof(buf) / sizeof(*buf) - 1;
574 if ((width = wparsefloat(fp, buf, buf + width)) == 0) goto match_failure;
575 if ((flags & SUPPRESS) == 0) {
576 if (flags & LONGDBL) {
577 long double res = wcstold(buf, &p);
578 *va_arg(ap, long double*) = res;
579 } else if (flags & LONG) {
580 double res = wcstod(buf, &p);
581 *va_arg(ap, double*) = res;
582 } else {
583 float res = wcstof(buf, &p);
584 *va_arg(ap, float*) = res;
585 }
586 if (p - buf != (ptrdiff_t)width) abort();
587 nassigned++;
588 }
589 nread += width;
590 nconversions++;
591 break;
592 }
593 }
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700594input_failure:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700595 return (nconversions != 0 ? nassigned : EOF);
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700596match_failure:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700597 return (nassigned);
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700598}
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700599#pragma GCC diagnostic pop