2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Copyright (c) 2011 The FreeBSD Foundation
10 * Portions of this software were developed by David Chisnall
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
41 #endif /* LIBC_SCCS and not lint */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include "namespace.h"
57 #include "un-namespace.h"
59 #include "libc_private.h"
61 #include "xlocale_private.h"
63 #define BUF 513 /* Maximum length of numeric string. */
66 * Flags used during conversion.
68 #define LONG 0x01 /* l: long or double */
69 #define LONGDBL 0x02 /* L: long double */
70 #define SHORT 0x04 /* h: short */
71 #define SUPPRESS 0x08 /* *: suppress assignment */
72 #define POINTER 0x10 /* p: void * (as hex) */
73 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
74 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
75 #define INTMAXT 0x800 /* j: intmax_t */
76 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
77 #define SIZET 0x2000 /* z: size_t */
78 #define SHORTSHORT 0x4000 /* hh: char */
79 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
82 * The following are used in integral conversions only:
83 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
85 #define SIGNOK 0x40 /* +/- is (still) legal */
86 #define NDIGITS 0x80 /* no digits detected */
87 #define PFXOK 0x100 /* 0x prefix is (still) legal */
88 #define NZDIGITS 0x200 /* no zero digits detected */
89 #define HAVESIGN 0x10000 /* sign detected */
94 #define CT_CHAR 0 /* %c conversion */
95 #define CT_CCL 1 /* %[...] conversion */
96 #define CT_STRING 2 /* %s conversion */
97 #define CT_INT 3 /* %[dioupxX] conversion */
98 #define CT_FLOAT 4 /* %[efgEFG] conversion */
100 #ifndef NO_FLOATING_POINT
101 static int parsefloat(FILE *, wchar_t *, wchar_t *, locale_t);
105 const wchar_t *start; /* character class start */
106 const wchar_t *end; /* character class end */
107 int compl; /* ccl is complemented? */
111 inccl(const struct ccl *ccl, wint_t wi)
115 return (wmemchr(ccl->start, wi, ccl->end - ccl->start)
118 return (wmemchr(ccl->start, wi, ccl->end - ccl->start) != NULL);
123 * Conversion functions are passed a pointer to this object instead of
124 * a real parameter to indicate that the assignment-suppression (*)
125 * flag was specified. We could use a NULL pointer to indicate this,
126 * but that would mask bugs in applications that call scanf() with a
129 static const int suppress;
130 #define SUPPRESS_PTR ((void *)&suppress)
132 static const mbstate_t initial_mbs;
135 * The following conversion functions return the number of characters consumed,
136 * or -1 on input failure. Character class conversion returns 0 on match
141 convert_char(FILE *fp, char * mbp, int width, locale_t locale)
150 while (width-- != 0 && (wi = __fgetwc(fp, locale)) != WEOF) {
151 if (mbp != SUPPRESS_PTR) {
152 nconv = wcrtomb(mbp, wi, &mbs);
153 if (nconv == (size_t)-1)
165 convert_wchar(FILE *fp, wchar_t *wcp, int width, locale_t locale)
171 while (width-- != 0 && (wi = __fgetwc(fp, locale)) != WEOF) {
172 if (wcp != SUPPRESS_PTR)
173 *wcp++ = (wchar_t)wi;
182 convert_ccl(FILE *fp, char * mbp, int width, const struct ccl *ccl,
192 while ((wi = __fgetwc(fp, locale)) != WEOF &&
193 width-- != 0 && inccl(ccl, wi)) {
194 if (mbp != SUPPRESS_PTR) {
195 nconv = wcrtomb(mbp, wi, &mbs);
196 if (nconv == (size_t)-1)
203 __ungetwc(wi, fp, locale);
204 if (mbp != SUPPRESS_PTR)
210 convert_wccl(FILE *fp, wchar_t *wcp, int width, const struct ccl *ccl,
217 if (wcp == SUPPRESS_PTR) {
219 while ((wi = __fgetwc(fp, locale)) != WEOF &&
220 width-- != 0 && inccl(ccl, wi))
223 __ungetwc(wi, fp, locale);
226 while ((wi = __fgetwc(fp, locale)) != WEOF &&
227 width-- != 0 && inccl(ccl, wi))
228 *wcp++ = (wchar_t)wi;
230 __ungetwc(wi, fp, locale);
240 convert_string(FILE *fp, char * mbp, int width, locale_t locale)
249 while ((wi = __fgetwc(fp, locale)) != WEOF && width-- != 0 &&
251 if (mbp != SUPPRESS_PTR) {
252 nconv = wcrtomb(mbp, wi, &mbs);
253 if (nconv == (size_t)-1)
260 __ungetwc(wi, fp, locale);
261 if (mbp != SUPPRESS_PTR)
267 convert_wstring(FILE *fp, wchar_t *wcp, int width, locale_t locale)
274 if (wcp == SUPPRESS_PTR) {
275 while ((wi = __fgetwc(fp, locale)) != WEOF &&
276 width-- != 0 && !iswspace(wi))
279 __ungetwc(wi, fp, locale);
282 while ((wi = __fgetwc(fp, locale)) != WEOF &&
283 width-- != 0 && !iswspace(wi)) {
284 *wcp++ = (wchar_t)wi;
288 __ungetwc(wi, fp, locale);
295 * Read an integer, storing it in buf. The only relevant bit in the
296 * flags argument is PFXOK.
298 * Return 0 on a match failure, and the number of characters read
302 parseint(FILE *fp, wchar_t *buf, int width, int base, int flags,
305 /* `basefix' is used to avoid `if' tests */
306 static const short basefix[17] =
307 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
311 flags |= SIGNOK | NDIGITS | NZDIGITS;
312 for (wcp = buf; width; width--) {
313 c = __fgetwc(fp, locale);
315 * Switch on the character; `goto ok' if we accept it
316 * as a part of number.
321 * The digit 0 is always legal, but is special. For
322 * %i conversions, if no digits (zero or nonzero) have
323 * been scanned (only signs), we will have base==0.
324 * In that case, we should set it to 8 and enable 0x
325 * prefixing. Also, if we have not scanned zero
326 * digits before this, do not turn off prefixing
327 * (someone else will turn it off if we have scanned
328 * any nonzero digits).
335 if (flags & NZDIGITS)
336 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
338 flags &= ~(SIGNOK|PFXOK|NDIGITS);
341 /* 1 through 7 always legal */
342 case '1': case '2': case '3':
343 case '4': case '5': case '6': case '7':
344 base = basefix[base];
345 flags &= ~(SIGNOK | PFXOK | NDIGITS);
348 /* digits 8 and 9 ok iff decimal or hex */
350 base = basefix[base];
352 break; /* not legal here */
353 flags &= ~(SIGNOK | PFXOK | NDIGITS);
356 /* letters ok iff hex */
357 case 'A': case 'B': case 'C':
358 case 'D': case 'E': case 'F':
359 case 'a': case 'b': case 'c':
360 case 'd': case 'e': case 'f':
361 /* no need to fix base here */
363 break; /* not legal here */
364 flags &= ~(SIGNOK | PFXOK | NDIGITS);
367 /* sign ok only as first character */
369 if (flags & SIGNOK) {
377 * x ok iff flag still set & 2nd char (or 3rd char if
381 if (flags & PFXOK && wcp ==
382 buf + 1 + !!(flags & HAVESIGN)) {
383 base = 16; /* if %i */
391 * If we got here, c is not a legal character for a
392 * number. Stop accumulating digits.
395 __ungetwc(c, fp, locale);
399 * c is legal: store it and look at the next.
404 * If we had only a sign, it is no good; push back the sign.
405 * If the number ends in `x', it was [sign] '0' 'x', so push
406 * back the x and treat it as [sign] '0'.
408 if (flags & NDIGITS) {
410 __ungetwc(*--wcp, fp, locale);
414 if (c == 'x' || c == 'X') {
416 __ungetwc(c, fp, locale);
425 vfwscanf_l(FILE * __restrict fp, locale_t locale,
426 const wchar_t * __restrict fmt, va_list ap)
431 FLOCKFILE_CANCELSAFE(fp);
433 ret = __vfwscanf(fp, locale, fmt, ap);
434 FUNLOCKFILE_CANCELSAFE();
438 vfwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
440 return vfwscanf_l(fp, __get_locale(), fmt, ap);
444 * Non-MT-safe version.
447 __vfwscanf(FILE * __restrict fp, locale_t locale,
448 const wchar_t * __restrict fmt, va_list ap)
450 #define GETARG(type) ((flags & SUPPRESS) ? SUPPRESS_PTR : va_arg(ap, type))
451 wint_t c; /* character from format, or conversion */
452 size_t width; /* field width, or 0 */
453 int flags; /* flags as defined above */
454 int nassigned; /* number of fields assigned */
455 int nconversions; /* number of conversions */
456 int nr; /* characters read by the current conversion */
457 int nread; /* number of characters consumed from fp */
458 int base; /* base argument to conversion function */
459 struct ccl ccl; /* character class info */
460 wchar_t buf[BUF]; /* buffer for numeric conversions */
461 wint_t wi; /* handy wint_t */
466 ccl.start = ccl.end = NULL;
472 while ((c = __fgetwc(fp, locale)) != WEOF &&
473 iswspace_l(c, locale))
476 __ungetwc(c, fp, locale);
484 * switch on the format. continue if done;
485 * break once format type is derived.
491 if ((wi = __fgetwc(fp, locale)) == WEOF)
494 __ungetwc(wi, fp, locale);
514 flags |= LONGLONG; /* not quite */
533 case '0': case '1': case '2': case '3': case '4':
534 case '5': case '6': case '7': case '8': case '9':
535 width = width * 10 + c - '0';
565 flags |= PFXOK; /* enable 0x prefixing */
571 #ifndef NO_FLOATING_POINT
572 case 'A': case 'E': case 'F': case 'G':
573 case 'a': case 'e': case 'f': case 'g':
594 while (*fmt != '\0' && *fmt != ']')
610 case 'p': /* pointer format is like hex */
611 flags |= POINTER | PFXOK;
612 c = CT_INT; /* assumes sizeof(uintmax_t) */
613 flags |= UNSIGNED; /* >= sizeof(uintptr_t) */
618 if (flags & SUPPRESS) /* ??? */
620 if (flags & SHORTSHORT)
621 *va_arg(ap, char *) = nread;
622 else if (flags & SHORT)
623 *va_arg(ap, short *) = nread;
624 else if (flags & LONG)
625 *va_arg(ap, long *) = nread;
626 else if (flags & LONGLONG)
627 *va_arg(ap, long long *) = nread;
628 else if (flags & INTMAXT)
629 *va_arg(ap, intmax_t *) = nread;
630 else if (flags & SIZET)
631 *va_arg(ap, size_t *) = nread;
632 else if (flags & PTRDIFFT)
633 *va_arg(ap, ptrdiff_t *) = nread;
635 *va_arg(ap, int *) = nread;
642 * Disgusting backwards compatibility hack. XXX
644 case '\0': /* compat */
649 * Consume leading white space, except for formats
650 * that suppress this.
652 if ((flags & NOSKIP) == 0) {
653 while ((wi = __fgetwc(fp, locale)) != WEOF && iswspace(wi))
657 __ungetwc(wi, fp, locale);
666 /* scan arbitrary characters (sets NOSKIP) */
670 nr = convert_wchar(fp, GETARG(wchar_t *), width,
673 nr = convert_char(fp, GETARG(char *), width,
681 /* scan a (nonempty) character class (sets NOSKIP) */
683 width = (size_t)~0; /* `infinity' */
684 /* take only those things in the class */
686 nr = convert_wccl(fp, GETARG(wchar_t *), width,
689 nr = convert_ccl(fp, GETARG(char *), width,
701 /* like CCL, but zero-length string OK, & no NOSKIP */
705 nr = convert_wstring(fp, GETARG(wchar_t *),
708 nr = convert_string(fp, GETARG(char *), width,
716 /* scan an integer as if by the conversion function */
717 if (width == 0 || width > sizeof(buf) /
719 width = sizeof(buf) / sizeof(*buf) - 1;
721 nr = parseint(fp, buf, width, base, flags, locale);
724 if ((flags & SUPPRESS) == 0) {
728 if ((flags & UNSIGNED) == 0)
729 res = wcstoimax(buf, NULL, base);
731 res = wcstoumax(buf, NULL, base);
733 *va_arg(ap, void **) =
734 (void *)(uintptr_t)res;
735 else if (flags & SHORTSHORT)
736 *va_arg(ap, char *) = res;
737 else if (flags & SHORT)
738 *va_arg(ap, short *) = res;
739 else if (flags & LONG)
740 *va_arg(ap, long *) = res;
741 else if (flags & LONGLONG)
742 *va_arg(ap, long long *) = res;
743 else if (flags & INTMAXT)
744 *va_arg(ap, intmax_t *) = res;
745 else if (flags & PTRDIFFT)
746 *va_arg(ap, ptrdiff_t *) = res;
747 else if (flags & SIZET)
748 *va_arg(ap, size_t *) = res;
750 *va_arg(ap, int *) = res;
754 #ifndef NO_FLOATING_POINT
756 /* scan a floating point number as if by strtod */
757 if (width == 0 || width > sizeof(buf) /
759 width = sizeof(buf) / sizeof(*buf) - 1;
760 nr = parsefloat(fp, buf, buf + width, locale);
763 if ((flags & SUPPRESS) == 0) {
764 if (flags & LONGDBL) {
765 long double res = wcstold(buf, NULL);
766 *va_arg(ap, long double *) = res;
767 } else if (flags & LONG) {
768 double res = wcstod(buf, NULL);
769 *va_arg(ap, double *) = res;
771 float res = wcstof(buf, NULL);
772 *va_arg(ap, float *) = res;
776 #endif /* !NO_FLOATING_POINT */
778 if (!(flags & SUPPRESS))
784 return (nconversions != 0 ? nassigned : EOF);
789 #ifndef NO_FLOATING_POINT
791 parsefloat(FILE *fp, wchar_t *buf, wchar_t *end, locale_t locale)
798 S_START, S_GOTSIGN, S_INF, S_NAN, S_DONE, S_MAYBEHEX,
799 S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
803 _Bool gotmantdig = 0, ishex = 0;
806 nconv = mbrtowc(&decpt, localeconv()->decimal_point, MB_CUR_MAX, &mbs);
807 if (nconv == (size_t)-1 || nconv == (size_t)-2)
808 decpt = '.'; /* failsafe */
811 * We set commit = p whenever the string we have read so far
812 * constitutes a valid representation of a floating point
813 * number by itself. At some point, the parse will complete
814 * or fail, and we will ungetc() back to the last commit point.
815 * To ensure that the file offset gets updated properly, it is
816 * always necessary to read at least one character that doesn't
817 * match; thus, we can't short-circuit "infinity" or "nan(...)".
821 for (p = buf; p < end; ) {
822 if ((c = __fgetwc(fp, locale)) == WEOF)
828 if (c == '-' || c == '+')
853 (c != "nfinity"[infnanpos] &&
854 c != "NFINITY"[infnanpos]))
856 if (infnanpos == 1 || infnanpos == 6)
857 commit = p; /* inf or infinity */
863 if (c != 'A' && c != 'a')
867 if (c != 'N' && c != 'n')
880 } else if (!iswalnum(c) && c != '_')
890 if (c == 'X' || c == 'x') {
893 } else { /* we saw a '0', but no 'x' */
898 if ((ishex && iswxdigit(c)) || iswdigit(c))
909 if (((c == 'E' || c == 'e') && !ishex) ||
910 ((c == 'P' || c == 'p') && ishex)) {
915 } else if ((ishex && iswxdigit(c)) || iswdigit(c)) {
923 if (c == '-' || c == '+')
942 __ungetwc(c, fp, locale);
944 __ungetwc(*p, fp, locale);
946 return (commit - buf);