blob: 3df4a87f20769bea8dced1aa02e1bbd160865bfe [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++;
zijunzhaocc475cf2023-06-13 00:34:13 +0000226 if (c == 'f') {
227 fast = true;
228 c = *fmt++;
229 }
zijunzhao78904842023-05-09 00:54:00 +0000230 while (is_digit(c)) {
231 APPEND_DIGIT(size, c);
232 c = *fmt++;
233 }
234 flags |= w_to_flag(size, fast);
235 goto reswitch;
236 }
237
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700238 case 'X':
239 case 'x':
240 flags |= PFXOK; /* enable 0x prefixing */
241 c = CT_INT;
242 flags |= UNSIGNED;
243 base = 16;
244 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700245
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700246 case 'e':
247 case 'E':
248 case 'f':
249 case 'F':
250 case 'g':
251 case 'G':
252 case 'a':
253 case 'A':
254 c = CT_FLOAT;
255 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700256
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700257 case 's':
258 c = CT_STRING;
259 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700260
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700261 case '[':
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800262 ccl = fmt;
263 if (*fmt == '^') fmt++;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700264 if (*fmt == ']') fmt++;
265 while (*fmt != '\0' && *fmt != ']') fmt++;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700266 fmt++;
267 flags |= NOSKIP;
268 c = CT_CCL;
269 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700270
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700271 case 'c':
272 flags |= NOSKIP;
273 c = CT_CHAR;
274 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700275
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700276 case 'p': /* pointer format is like hex */
277 flags |= POINTER | PFXOK;
278 c = CT_INT;
279 flags |= UNSIGNED;
280 base = 16;
281 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700282
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700283 case 'n':
284 nconversions++;
285 if (flags & SUPPRESS) continue;
286 if (flags & SHORTSHORT)
287 *va_arg(ap, signed char*) = nread;
288 else if (flags & SHORT)
289 *va_arg(ap, short*) = nread;
290 else if (flags & LONG)
291 *va_arg(ap, long*) = nread;
292 else if (flags & SIZEINT)
293 *va_arg(ap, ssize_t*) = nread;
294 else if (flags & PTRINT)
295 *va_arg(ap, ptrdiff_t*) = nread;
296 else if (flags & LLONG)
297 *va_arg(ap, long long*) = nread;
298 else if (flags & MAXINT)
299 *va_arg(ap, intmax_t*) = nread;
300 else
301 *va_arg(ap, int*) = nread;
302 continue;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700303
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700304 /*
305 * Disgusting backwards compatibility hacks. XXX
306 */
307 case '\0': /* compat */
308 return (EOF);
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700309
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700310 default: /* compat */
311 if (iswupper(c)) flags |= LONG;
312 c = CT_INT;
313 base = 10;
314 break;
315 }
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700316
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700317 /*
318 * Consume leading white space, except for formats
319 * that suppress this.
320 */
321 if ((flags & NOSKIP) == 0) {
322 while ((wi = __fgetwc_unlock(fp)) != WEOF && iswspace(wi)) nread++;
323 if (wi == WEOF) goto input_failure;
324 __ungetwc(wi, fp);
325 }
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700326
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700327 /*
328 * Do the conversion.
329 */
330 switch (c) {
331 case CT_CHAR:
332 /* scan arbitrary characters (sets NOSKIP) */
333 if (width == 0) width = 1;
334 if (flags & LONG) {
335 if (!(flags & SUPPRESS)) p = va_arg(ap, wchar_t*);
336 n = 0;
337 while (width-- != 0 && (wi = __fgetwc_unlock(fp)) != WEOF) {
338 if (!(flags & SUPPRESS)) *p++ = (wchar_t)wi;
339 n++;
340 }
341 if (n == 0) goto input_failure;
342 nread += n;
343 if (!(flags & SUPPRESS)) nassigned++;
344 } else {
345 if (!(flags & SUPPRESS)) mbp = va_arg(ap, char*);
346 n = 0;
347 memset(&mbs, 0, sizeof(mbs));
348 while (width != 0 && (wi = __fgetwc_unlock(fp)) != WEOF) {
349 if (width >= MB_CUR_MAX && !(flags & SUPPRESS)) {
350 nconv = wcrtomb(mbp, wi, &mbs);
351 if (nconv == (size_t)-1) goto input_failure;
352 } else {
353 nconv = wcrtomb(mbbuf, wi, &mbs);
354 if (nconv == (size_t)-1) goto input_failure;
355 if (nconv > width) {
356 __ungetwc(wi, fp);
357 break;
358 }
359 if (!(flags & SUPPRESS)) memcpy(mbp, mbbuf, nconv);
360 }
361 if (!(flags & SUPPRESS)) mbp += nconv;
362 width -= nconv;
363 n++;
364 }
365 if (n == 0) goto input_failure;
366 nread += n;
367 if (!(flags & SUPPRESS)) nassigned++;
368 }
369 nconversions++;
370 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700371
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700372 case CT_CCL:
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -0800373 case CT_STRING:
374 // CT_CCL: scan a (nonempty) character class (sets NOSKIP).
375 // CT_STRING: like CCL, but zero-length string OK, & no NOSKIP.
376 if (width == 0) width = (size_t)~0; // 'infinity'.
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700377 if ((flags & SUPPRESS) && (flags & LONG)) {
378 n = 0;
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)))) n++;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700380 if (wi != WEOF) __ungetwc(wi, fp);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700381 } else if (flags & LONG) {
382 p0 = p = va_arg(ap, wchar_t*);
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -0800383 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 -0700384 *p++ = (wchar_t)wi;
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -0800385 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700386 if (wi != WEOF) __ungetwc(wi, fp);
387 n = p - p0;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700388 } else {
389 if (!(flags & SUPPRESS)) mbp = va_arg(ap, char*);
390 n = 0;
391 memset(&mbs, 0, sizeof(mbs));
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -0800392 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 -0700393 if (width >= MB_CUR_MAX && !(flags & SUPPRESS)) {
394 nconv = wcrtomb(mbp, wi, &mbs);
395 if (nconv == (size_t)-1) goto input_failure;
396 } else {
397 nconv = wcrtomb(mbbuf, wi, &mbs);
398 if (nconv == (size_t)-1) goto input_failure;
399 if (nconv > width) break;
400 if (!(flags & SUPPRESS)) memcpy(mbp, mbbuf, nconv);
401 }
402 if (!(flags & SUPPRESS)) mbp += nconv;
403 width -= nconv;
404 n++;
405 }
406 if (wi != WEOF) __ungetwc(wi, fp);
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -0800407 }
408 if (c == CT_CCL && n == 0) goto match_failure;
409 if (!(flags & SUPPRESS)) {
410 if (flags & LONG) {
411 *p = L'\0';
412 } else {
413 *mbp = '\0';
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700414 }
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -0800415 ++nassigned;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700416 }
417 nread += n;
418 nconversions++;
419 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700420
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700421 case CT_INT:
422 /* scan an integer as if by strtoimax/strtoumax */
423 if (width == 0 || width > sizeof(buf) / sizeof(*buf) - 1)
424 width = sizeof(buf) / sizeof(*buf) - 1;
425 flags |= SIGNOK | NDIGITS | NZDIGITS;
426 for (p = buf; width; width--) {
427 c = __fgetwc_unlock(fp);
428 /*
429 * Switch on the character; `goto ok'
430 * if we accept it as a part of number.
431 */
432 switch (c) {
433 /*
434 * The digit 0 is always legal, but is
435 * special. For %i conversions, if no
436 * digits (zero or nonzero) have been
437 * scanned (only signs), we will have
438 * base==0. In that case, we should set
Elliott Hughes1f462de2022-08-05 22:51:05 +0000439 * it to 8 and enable 0b/0x prefixing.
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700440 * Also, if we have not scanned zero digits
441 * before this, do not turn off prefixing
442 * (someone else will turn it off if we
443 * have scanned any nonzero digits).
444 */
445 case '0':
446 if (base == 0) {
447 base = 8;
Elliott Hughes1f462de2022-08-05 22:51:05 +0000448 flags |= PFBOK | PFXOK;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700449 }
Elliott Hughes1f462de2022-08-05 22:51:05 +0000450 if (flags & NZDIGITS) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700451 flags &= ~(SIGNOK | NZDIGITS | NDIGITS);
Elliott Hughes1f462de2022-08-05 22:51:05 +0000452 } else {
453 flags &= ~(SIGNOK | PFBOK | PFXOK | NDIGITS);
454 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700455 goto ok;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700456
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700457 /* 1 through 7 always legal */
Elliott Hughes1f462de2022-08-05 22:51:05 +0000458 case 'B':
459 case 'b':
460 // Is this 'b' potentially part of an "0b" prefix?
461 if ((flags & PFBOK) && p == buf + 1 + !!(flags & HAVESIGN)) {
462 base = 2;
463 flags &= ~PFBOK;
464 goto ok;
465 }
466 // No? Fall through and see if it's a hex digit instead then...
467 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700468 case '1':
469 case '2':
470 case '3':
471 case '4':
472 case '5':
473 case '6':
474 case '7':
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700475 case '8':
476 case '9':
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700477 case 'A':
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700478 case 'C':
479 case 'D':
480 case 'E':
481 case 'F':
482 case 'a':
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700483 case 'c':
484 case 'd':
485 case 'e':
486 case 'f':
Elliott Hughes1f462de2022-08-05 22:51:05 +0000487 if (base == 0) base = 10;
488 if (base != 16 && (int)(c - '0') >= base) break; /* not legal here */
489 flags &= ~(SIGNOK | PFBOK | PFXOK | NDIGITS);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700490 goto ok;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700491
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700492 /* sign ok only as first character */
493 case '+':
494 case '-':
495 if (flags & SIGNOK) {
496 flags &= ~SIGNOK;
497 flags |= HAVESIGN;
498 goto ok;
499 }
500 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700501
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700502 /*
503 * x ok iff flag still set and 2nd char (or
504 * 3rd char if we have a sign).
505 */
506 case 'x':
507 case 'X':
508 if ((flags & PFXOK) && p == buf + 1 + !!(flags & HAVESIGN)) {
509 base = 16; /* if %i */
510 flags &= ~PFXOK;
511 goto ok;
512 }
513 break;
514 }
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700515
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700516 /*
517 * If we got here, c is not a legal character
518 * for a number. Stop accumulating digits.
519 */
520 if (c != WEOF) __ungetwc(c, fp);
521 break;
522 ok:
523 /*
524 * c is legal: store it and look at the next.
525 */
526 *p++ = (wchar_t)c;
527 }
528 /*
Elliott Hughes1f462de2022-08-05 22:51:05 +0000529 * If we had only a sign, it is no good; push back the sign.
530 * If the number was `[-+]0[BbXx]`, push back and treat it
531 * as `[-+]0`.
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700532 */
533 if (flags & NDIGITS) {
534 if (p > buf) __ungetwc(*--p, fp);
535 goto match_failure;
536 }
537 c = p[-1];
Elliott Hughes1f462de2022-08-05 22:51:05 +0000538 if ((base == 2 && (c == 'b' || c == 'B')) || c == 'x' || c == 'X') {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700539 --p;
540 __ungetwc(c, fp);
541 }
542 if ((flags & SUPPRESS) == 0) {
543 uintmax_t res;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700544
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700545 *p = '\0';
546 if (flags & UNSIGNED)
547 res = wcstoimax(buf, NULL, base);
548 else
549 res = wcstoumax(buf, NULL, base);
550 if (flags & POINTER)
551 *va_arg(ap, void**) = (void*)(uintptr_t)res;
552 else if (flags & MAXINT)
553 *va_arg(ap, intmax_t*) = res;
554 else if (flags & LLONG)
555 *va_arg(ap, long long*) = res;
556 else if (flags & SIZEINT)
557 *va_arg(ap, ssize_t*) = res;
558 else if (flags & PTRINT)
559 *va_arg(ap, ptrdiff_t*) = res;
560 else if (flags & LONG)
561 *va_arg(ap, long*) = res;
562 else if (flags & SHORT)
563 *va_arg(ap, short*) = res;
564 else if (flags & SHORTSHORT)
565 *va_arg(ap, signed char*) = res;
566 else
567 *va_arg(ap, int*) = res;
568 nassigned++;
569 }
570 nread += p - buf;
571 nconversions++;
572 break;
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700573
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700574 case CT_FLOAT:
575 /* scan a floating point number as if by strtod */
576 if (width == 0 || width > sizeof(buf) / sizeof(*buf) - 1)
577 width = sizeof(buf) / sizeof(*buf) - 1;
578 if ((width = wparsefloat(fp, buf, buf + width)) == 0) goto match_failure;
579 if ((flags & SUPPRESS) == 0) {
580 if (flags & LONGDBL) {
581 long double res = wcstold(buf, &p);
582 *va_arg(ap, long double*) = res;
583 } else if (flags & LONG) {
584 double res = wcstod(buf, &p);
585 *va_arg(ap, double*) = res;
586 } else {
587 float res = wcstof(buf, &p);
588 *va_arg(ap, float*) = res;
589 }
590 if (p - buf != (ptrdiff_t)width) abort();
591 nassigned++;
592 }
593 nread += width;
594 nconversions++;
595 break;
596 }
597 }
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700598input_failure:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700599 return (nconversions != 0 ? nassigned : EOF);
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700600match_failure:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700601 return (nassigned);
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700602}
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700603#pragma GCC diagnostic pop