2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Copyright (c) 2011 The FreeBSD Foundation
11 * All rights reserved.
12 * Portions of this software were developed by David Chisnall
13 * under sponsorship from the FreeBSD Foundation.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #if defined(LIBC_SCCS) && !defined(lint)
42 static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
43 #endif /* LIBC_SCCS and not lint */
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include "namespace.h"
59 #include "un-namespace.h"
61 #include "libc_private.h"
63 #include "xlocale_private.h"
65 #define BUF 513 /* Maximum length of numeric string. */
68 * Flags used during conversion.
70 #define LONG 0x01 /* l: long or double */
71 #define LONGDBL 0x02 /* L: long double */
72 #define SHORT 0x04 /* h: short */
73 #define SUPPRESS 0x08 /* *: suppress assignment */
74 #define POINTER 0x10 /* p: void * (as hex) */
75 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
76 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
77 #define INTMAXT 0x800 /* j: intmax_t */
78 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
79 #define SIZET 0x2000 /* z: size_t */
80 #define SHORTSHORT 0x4000 /* hh: char */
81 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
84 * The following are used in integral conversions only:
85 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
87 #define SIGNOK 0x40 /* +/- is (still) legal */
88 #define NDIGITS 0x80 /* no digits detected */
89 #define PFXOK 0x100 /* 0x prefix is (still) legal */
90 #define NZDIGITS 0x200 /* no zero digits detected */
91 #define HAVESIGN 0x10000 /* sign detected */
96 #define CT_CHAR 0 /* %c conversion */
97 #define CT_CCL 1 /* %[...] conversion */
98 #define CT_STRING 2 /* %s conversion */
99 #define CT_INT 3 /* %[dioupxX] conversion */
100 #define CT_FLOAT 4 /* %[efgEFG] conversion */
102 #ifndef NO_FLOATING_POINT
103 static int parsefloat(FILE *, wchar_t *, wchar_t *, locale_t);
107 const wchar_t *start; /* character class start */
108 const wchar_t *end; /* character class end */
109 int compl; /* ccl is complemented? */
113 inccl(const struct ccl *ccl, wint_t wi)
117 return (wmemchr(ccl->start, wi, ccl->end - ccl->start)
120 return (wmemchr(ccl->start, wi, ccl->end - ccl->start) != NULL);
125 * Conversion functions are passed a pointer to this object instead of
126 * a real parameter to indicate that the assignment-suppression (*)
127 * flag was specified. We could use a NULL pointer to indicate this,
128 * but that would mask bugs in applications that call scanf() with a
131 static const int suppress;
132 #define SUPPRESS_PTR ((void *)&suppress)
134 static const mbstate_t initial_mbs;
137 * The following conversion functions return the number of characters consumed,
138 * or -1 on input failure. Character class conversion returns 0 on match
143 convert_char(FILE *fp, char * mbp, int width, locale_t locale)
152 while (width-- != 0 && (wi = __fgetwc(fp, locale)) != WEOF) {
153 if (mbp != SUPPRESS_PTR) {
154 nconv = wcrtomb(mbp, wi, &mbs);
155 if (nconv == (size_t)-1)
167 convert_wchar(FILE *fp, wchar_t *wcp, int width, locale_t locale)
173 while (width-- != 0 && (wi = __fgetwc(fp, locale)) != WEOF) {
174 if (wcp != SUPPRESS_PTR)
175 *wcp++ = (wchar_t)wi;
184 convert_ccl(FILE *fp, char * mbp, int width, const struct ccl *ccl,
194 while ((wi = __fgetwc(fp, locale)) != WEOF &&
195 width-- != 0 && inccl(ccl, wi)) {
196 if (mbp != SUPPRESS_PTR) {
197 nconv = wcrtomb(mbp, wi, &mbs);
198 if (nconv == (size_t)-1)
205 __ungetwc(wi, fp, locale);
206 if (mbp != SUPPRESS_PTR)
212 convert_wccl(FILE *fp, wchar_t *wcp, int width, const struct ccl *ccl,
219 if (wcp == SUPPRESS_PTR) {
221 while ((wi = __fgetwc(fp, locale)) != WEOF &&
222 width-- != 0 && inccl(ccl, wi))
225 __ungetwc(wi, fp, locale);
228 while ((wi = __fgetwc(fp, locale)) != WEOF &&
229 width-- != 0 && inccl(ccl, wi))
230 *wcp++ = (wchar_t)wi;
232 __ungetwc(wi, fp, locale);
242 convert_string(FILE *fp, char * mbp, int width, locale_t locale)
251 while ((wi = __fgetwc(fp, locale)) != WEOF && width-- != 0 &&
253 if (mbp != SUPPRESS_PTR) {
254 nconv = wcrtomb(mbp, wi, &mbs);
255 if (nconv == (size_t)-1)
262 __ungetwc(wi, fp, locale);
263 if (mbp != SUPPRESS_PTR)
269 convert_wstring(FILE *fp, wchar_t *wcp, int width, locale_t locale)
276 if (wcp == SUPPRESS_PTR) {
277 while ((wi = __fgetwc(fp, locale)) != WEOF &&
278 width-- != 0 && !iswspace(wi))
281 __ungetwc(wi, fp, locale);
284 while ((wi = __fgetwc(fp, locale)) != WEOF &&
285 width-- != 0 && !iswspace(wi)) {
286 *wcp++ = (wchar_t)wi;
290 __ungetwc(wi, fp, locale);
297 * Read an integer, storing it in buf. The only relevant bit in the
298 * flags argument is PFXOK.
300 * Return 0 on a match failure, and the number of characters read
304 parseint(FILE *fp, wchar_t *buf, int width, int base, int flags,
307 /* `basefix' is used to avoid `if' tests */
308 static const short basefix[17] =
309 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
313 flags |= SIGNOK | NDIGITS | NZDIGITS;
314 for (wcp = buf; width; width--) {
315 c = __fgetwc(fp, locale);
317 * Switch on the character; `goto ok' if we accept it
318 * as a part of number.
323 * The digit 0 is always legal, but is special. For
324 * %i conversions, if no digits (zero or nonzero) have
325 * been scanned (only signs), we will have base==0.
326 * In that case, we should set it to 8 and enable 0x
327 * prefixing. Also, if we have not scanned zero
328 * digits before this, do not turn off prefixing
329 * (someone else will turn it off if we have scanned
330 * any nonzero digits).
337 if (flags & NZDIGITS)
338 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
340 flags &= ~(SIGNOK|PFXOK|NDIGITS);
343 /* 1 through 7 always legal */
344 case '1': case '2': case '3':
345 case '4': case '5': case '6': case '7':
346 base = basefix[base];
347 flags &= ~(SIGNOK | PFXOK | NDIGITS);
350 /* digits 8 and 9 ok iff decimal or hex */
352 base = basefix[base];
354 break; /* not legal here */
355 flags &= ~(SIGNOK | PFXOK | NDIGITS);
358 /* letters ok iff hex */
359 case 'A': case 'B': case 'C':
360 case 'D': case 'E': case 'F':
361 case 'a': case 'b': case 'c':
362 case 'd': case 'e': case 'f':
363 /* no need to fix base here */
365 break; /* not legal here */
366 flags &= ~(SIGNOK | PFXOK | NDIGITS);
369 /* sign ok only as first character */
371 if (flags & SIGNOK) {
379 * x ok iff flag still set & 2nd char (or 3rd char if
383 if (flags & PFXOK && wcp ==
384 buf + 1 + !!(flags & HAVESIGN)) {
385 base = 16; /* if %i */
393 * If we got here, c is not a legal character for a
394 * number. Stop accumulating digits.
397 __ungetwc(c, fp, locale);
401 * c is legal: store it and look at the next.
406 * If we had only a sign, it is no good; push back the sign.
407 * If the number ends in `x', it was [sign] '0' 'x', so push
408 * back the x and treat it as [sign] '0'.
410 if (flags & NDIGITS) {
412 __ungetwc(*--wcp, fp, locale);
416 if (c == 'x' || c == 'X') {
418 __ungetwc(c, fp, locale);
427 vfwscanf_l(FILE * __restrict fp, locale_t locale,
428 const wchar_t * __restrict fmt, va_list ap)
433 FLOCKFILE_CANCELSAFE(fp);
435 ret = __vfwscanf(fp, locale, fmt, ap);
436 FUNLOCKFILE_CANCELSAFE();
440 vfwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
442 return vfwscanf_l(fp, __get_locale(), fmt, ap);
446 * Non-MT-safe version.
449 __vfwscanf(FILE * __restrict fp, locale_t locale,
450 const wchar_t * __restrict fmt, va_list ap)
452 #define GETARG(type) ((flags & SUPPRESS) ? SUPPRESS_PTR : va_arg(ap, type))
453 wint_t c; /* character from format, or conversion */
454 size_t width; /* field width, or 0 */
455 int flags; /* flags as defined above */
456 int nassigned; /* number of fields assigned */
457 int nconversions; /* number of conversions */
458 int nr; /* characters read by the current conversion */
459 int nread; /* number of characters consumed from fp */
460 int base; /* base argument to conversion function */
461 struct ccl ccl; /* character class info */
462 wchar_t buf[BUF]; /* buffer for numeric conversions */
463 wint_t wi; /* handy wint_t */
468 ccl.start = ccl.end = NULL;
474 while ((c = __fgetwc(fp, locale)) != WEOF &&
475 iswspace_l(c, locale))
478 __ungetwc(c, fp, locale);
486 * switch on the format. continue if done;
487 * break once format type is derived.
493 if ((wi = __fgetwc(fp, locale)) == WEOF)
496 __ungetwc(wi, fp, locale);
516 flags |= LONGLONG; /* not quite */
535 case '0': case '1': case '2': case '3': case '4':
536 case '5': case '6': case '7': case '8': case '9':
537 width = width * 10 + c - '0';
567 flags |= PFXOK; /* enable 0x prefixing */
573 #ifndef NO_FLOATING_POINT
574 case 'A': case 'E': case 'F': case 'G':
575 case 'a': case 'e': case 'f': case 'g':
596 while (*fmt != '\0' && *fmt != ']')
612 case 'p': /* pointer format is like hex */
613 flags |= POINTER | PFXOK;
614 c = CT_INT; /* assumes sizeof(uintmax_t) */
615 flags |= UNSIGNED; /* >= sizeof(uintptr_t) */
620 if (flags & SUPPRESS) /* ??? */
622 if (flags & SHORTSHORT)
623 *va_arg(ap, char *) = nread;
624 else if (flags & SHORT)
625 *va_arg(ap, short *) = nread;
626 else if (flags & LONG)
627 *va_arg(ap, long *) = nread;
628 else if (flags & LONGLONG)
629 *va_arg(ap, long long *) = nread;
630 else if (flags & INTMAXT)
631 *va_arg(ap, intmax_t *) = nread;
632 else if (flags & SIZET)
633 *va_arg(ap, size_t *) = nread;
634 else if (flags & PTRDIFFT)
635 *va_arg(ap, ptrdiff_t *) = nread;
637 *va_arg(ap, int *) = nread;
644 * Disgusting backwards compatibility hack. XXX
646 case '\0': /* compat */
651 * Consume leading white space, except for formats
652 * that suppress this.
654 if ((flags & NOSKIP) == 0) {
655 while ((wi = __fgetwc(fp, locale)) != WEOF && iswspace(wi))
659 __ungetwc(wi, fp, locale);
668 /* scan arbitrary characters (sets NOSKIP) */
672 nr = convert_wchar(fp, GETARG(wchar_t *), width,
675 nr = convert_char(fp, GETARG(char *), width,
683 /* scan a (nonempty) character class (sets NOSKIP) */
685 width = (size_t)~0; /* `infinity' */
686 /* take only those things in the class */
688 nr = convert_wccl(fp, GETARG(wchar_t *), width,
691 nr = convert_ccl(fp, GETARG(char *), width,
703 /* like CCL, but zero-length string OK, & no NOSKIP */
707 nr = convert_wstring(fp, GETARG(wchar_t *),
710 nr = convert_string(fp, GETARG(char *), width,
718 /* scan an integer as if by the conversion function */
719 if (width == 0 || width > sizeof(buf) /
721 width = sizeof(buf) / sizeof(*buf) - 1;
723 nr = parseint(fp, buf, width, base, flags, locale);
726 if ((flags & SUPPRESS) == 0) {
730 if ((flags & UNSIGNED) == 0)
731 res = wcstoimax(buf, NULL, base);
733 res = wcstoumax(buf, NULL, base);
735 *va_arg(ap, void **) =
736 (void *)(uintptr_t)res;
737 else if (flags & SHORTSHORT)
738 *va_arg(ap, char *) = res;
739 else if (flags & SHORT)
740 *va_arg(ap, short *) = res;
741 else if (flags & LONG)
742 *va_arg(ap, long *) = res;
743 else if (flags & LONGLONG)
744 *va_arg(ap, long long *) = res;
745 else if (flags & INTMAXT)
746 *va_arg(ap, intmax_t *) = res;
747 else if (flags & PTRDIFFT)
748 *va_arg(ap, ptrdiff_t *) = res;
749 else if (flags & SIZET)
750 *va_arg(ap, size_t *) = res;
752 *va_arg(ap, int *) = res;
756 #ifndef NO_FLOATING_POINT
758 /* scan a floating point number as if by strtod */
759 if (width == 0 || width > sizeof(buf) /
761 width = sizeof(buf) / sizeof(*buf) - 1;
762 nr = parsefloat(fp, buf, buf + width, locale);
765 if ((flags & SUPPRESS) == 0) {
766 if (flags & LONGDBL) {
767 long double res = wcstold(buf, NULL);
768 *va_arg(ap, long double *) = res;
769 } else if (flags & LONG) {
770 double res = wcstod(buf, NULL);
771 *va_arg(ap, double *) = res;
773 float res = wcstof(buf, NULL);
774 *va_arg(ap, float *) = res;
778 #endif /* !NO_FLOATING_POINT */
780 if (!(flags & SUPPRESS))
786 return (nconversions != 0 ? nassigned : EOF);
791 #ifndef NO_FLOATING_POINT
793 parsefloat(FILE *fp, wchar_t *buf, wchar_t *end, locale_t locale)
800 S_START, S_GOTSIGN, S_INF, S_NAN, S_DONE, S_MAYBEHEX,
801 S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
805 _Bool gotmantdig = 0, ishex = 0;
808 nconv = mbrtowc(&decpt, localeconv()->decimal_point, MB_CUR_MAX, &mbs);
809 if (nconv == (size_t)-1 || nconv == (size_t)-2)
810 decpt = '.'; /* failsafe */
813 * We set commit = p whenever the string we have read so far
814 * constitutes a valid representation of a floating point
815 * number by itself. At some point, the parse will complete
816 * or fail, and we will ungetc() back to the last commit point.
817 * To ensure that the file offset gets updated properly, it is
818 * always necessary to read at least one character that doesn't
819 * match; thus, we can't short-circuit "infinity" or "nan(...)".
823 for (p = buf; p < end; ) {
824 if ((c = __fgetwc(fp, locale)) == WEOF)
830 if (c == '-' || c == '+')
855 (c != "nfinity"[infnanpos] &&
856 c != "NFINITY"[infnanpos]))
858 if (infnanpos == 1 || infnanpos == 6)
859 commit = p; /* inf or infinity */
865 if (c != 'A' && c != 'a')
869 if (c != 'N' && c != 'n')
882 } else if (!iswalnum(c) && c != '_')
892 if (c == 'X' || c == 'x') {
895 } else { /* we saw a '0', but no 'x' */
900 if ((ishex && iswxdigit(c)) || iswdigit(c))
911 if (((c == 'E' || c == 'e') && !ishex) ||
912 ((c == 'P' || c == 'p') && ishex)) {
917 } else if ((ishex && iswxdigit(c)) || iswdigit(c)) {
925 if (c == '-' || c == '+')
944 __ungetwc(c, fp, locale);
946 __ungetwc(*p, fp, locale);
948 return (commit - buf);