2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 2011 The FreeBSD Foundation
7 * Portions of this software were developed by David Chisnall
8 * under sponsorship from the FreeBSD Foundation.
10 * This code is derived from software contributed to Berkeley by
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 * 4. 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
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
40 #endif /* LIBC_SCCS and not lint */
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include "namespace.h"
54 #include "un-namespace.h"
57 #include "libc_private.h"
59 #include "xlocale_private.h"
61 #ifndef NO_FLOATING_POINT
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 static const u_char *__sccl(char *, const u_char *);
103 #ifndef NO_FLOATING_POINT
104 static int parsefloat(FILE *, char *, char *, locale_t);
107 __weak_reference(__vfscanf, vfscanf);
110 * __vfscanf - MT-safe version
113 __vfscanf(FILE *fp, char const *fmt0, va_list ap)
118 ret = __svfscanf(fp, __get_locale(), fmt0, ap);
123 vfscanf_l(FILE *fp, locale_t locale, char const *fmt0, va_list ap)
129 ret = __svfscanf(fp, locale, fmt0, ap);
135 * __svfscanf - non-MT-safe version of __vfscanf
138 __svfscanf(FILE *fp, locale_t locale, const char *fmt0, va_list ap)
140 const u_char *fmt = (const u_char *)fmt0;
141 int c; /* character from format, or conversion */
142 size_t width; /* field width, or 0 */
143 char *p; /* points into all kinds of strings */
144 int n; /* handy integer */
145 int flags; /* flags as defined above */
146 char *p0; /* saves original value of p when necessary */
147 int nassigned; /* number of fields assigned */
148 int nconversions; /* number of conversions */
149 int nread; /* number of characters consumed from fp */
150 int base; /* base argument to conversion function */
151 char ccltab[256]; /* character class table for %[...] */
152 char buf[BUF]; /* buffer for numeric and mb conversions */
153 wchar_t *wcp; /* handy wide character pointer */
154 size_t nconv; /* length of multibyte sequence converted */
155 static const mbstate_t initial;
158 /* `basefix' is used to avoid `if' tests in the integer scanner */
159 static short basefix[17] =
160 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
172 while ((fp->_r > 0 || __srefill(fp) == 0) && isspace(*fp->_p))
173 nread++, fp->_r--, fp->_p++;
181 * switch on the format. continue if done;
182 * break once format type is derived.
188 if (fp->_r <= 0 && __srefill(fp))
210 flags |= LONGLONG; /* not quite */
229 case '0': case '1': case '2': case '3': case '4':
230 case '5': case '6': case '7': case '8': case '9':
231 width = width * 10 + c - '0';
261 flags |= PFXOK; /* enable 0x prefixing */
267 #ifndef NO_FLOATING_POINT
268 case 'A': case 'E': case 'F': case 'G':
269 case 'a': case 'e': case 'f': case 'g':
282 fmt = __sccl(ccltab, fmt);
295 case 'p': /* pointer format is like hex */
296 flags |= POINTER | PFXOK;
297 c = CT_INT; /* assumes sizeof(uintmax_t) */
298 flags |= UNSIGNED; /* >= sizeof(uintptr_t) */
304 if (flags & SUPPRESS) /* ??? */
306 if (flags & SHORTSHORT)
307 *va_arg(ap, char *) = nread;
308 else if (flags & SHORT)
309 *va_arg(ap, short *) = nread;
310 else if (flags & LONG)
311 *va_arg(ap, long *) = nread;
312 else if (flags & LONGLONG)
313 *va_arg(ap, long long *) = nread;
314 else if (flags & INTMAXT)
315 *va_arg(ap, intmax_t *) = nread;
316 else if (flags & SIZET)
317 *va_arg(ap, size_t *) = nread;
318 else if (flags & PTRDIFFT)
319 *va_arg(ap, ptrdiff_t *) = nread;
321 *va_arg(ap, int *) = nread;
328 * Disgusting backwards compatibility hack. XXX
330 case '\0': /* compat */
335 * We have a conversion that requires input.
337 if (fp->_r <= 0 && __srefill(fp))
341 * Consume leading white space, except for formats
342 * that suppress this.
344 if ((flags & NOSKIP) == 0) {
345 while (isspace(*fp->_p)) {
349 else if (__srefill(fp))
353 * Note that there is at least one character in
354 * the buffer, so conversions that do not set NOSKIP
355 * ca no longer result in an input failure.
365 /* scan arbitrary characters (sets NOSKIP) */
369 if ((flags & SUPPRESS) == 0)
370 wcp = va_arg(ap, wchar_t *);
375 if (n == MB_CUR_MAX) {
376 fp->_flags |= __SERR;
383 nconv = mbrtowc(wcp, buf, n, &mbs);
384 if (nconv == (size_t)-1) {
385 fp->_flags |= __SERR;
388 if (nconv == 0 && !(flags & SUPPRESS))
390 if (nconv != (size_t)-2) {
393 if (!(flags & SUPPRESS))
397 if (fp->_r <= 0 && __srefill(fp)) {
399 fp->_flags |= __SERR;
405 if (!(flags & SUPPRESS))
407 } else if (flags & SUPPRESS) {
410 if ((n = fp->_r) < width) {
428 size_t r = __fread((void *)va_arg(ap, char *), 1,
440 /* scan a (nonempty) character class (sets NOSKIP) */
442 width = (size_t)~0; /* `infinity' */
443 /* take only those things in the class */
448 if ((flags & SUPPRESS) == 0)
449 wcp = va_arg(ap, wchar_t *);
455 if (n == MB_CUR_MAX) {
456 fp->_flags |= __SERR;
463 nconv = mbrtowc(wcp, buf, n, &mbs);
464 if (nconv == (size_t)-1) {
465 fp->_flags |= __SERR;
470 if (nconv != (size_t)-2) {
471 if (wctob(*wcp) != EOF &&
472 !ccltab[wctob(*wcp)]) {
482 if (!(flags & SUPPRESS))
487 if (fp->_r <= 0 && __srefill(fp)) {
489 fp->_flags |= __SERR;
496 fp->_flags |= __SERR;
502 if (!(flags & SUPPRESS)) {
506 } else if (flags & SUPPRESS) {
508 while (ccltab[*fp->_p]) {
509 n++, fp->_r--, fp->_p++;
512 if (fp->_r <= 0 && __srefill(fp)) {
521 p0 = p = va_arg(ap, char *);
522 while (ccltab[*fp->_p]) {
527 if (fp->_r <= 0 && __srefill(fp)) {
544 /* like CCL, but zero-length string OK, & no NOSKIP */
550 if ((flags & SUPPRESS) == 0)
551 wcp = va_arg(ap, wchar_t *);
555 while (!isspace(*fp->_p) && width != 0) {
556 if (n == MB_CUR_MAX) {
557 fp->_flags |= __SERR;
564 nconv = mbrtowc(wcp, buf, n, &mbs);
565 if (nconv == (size_t)-1) {
566 fp->_flags |= __SERR;
571 if (nconv != (size_t)-2) {
572 if (iswspace(*wcp)) {
582 if (!(flags & SUPPRESS))
586 if (fp->_r <= 0 && __srefill(fp)) {
588 fp->_flags |= __SERR;
594 if (!(flags & SUPPRESS)) {
598 } else if (flags & SUPPRESS) {
600 while (!isspace(*fp->_p)) {
601 n++, fp->_r--, fp->_p++;
604 if (fp->_r <= 0 && __srefill(fp))
609 p0 = p = va_arg(ap, char *);
610 while (!isspace(*fp->_p)) {
615 if (fp->_r <= 0 && __srefill(fp))
626 /* scan an integer as if by the conversion function */
628 if (width == 0 || width > sizeof(buf) - 1)
629 width = sizeof(buf) - 1;
631 /* size_t is unsigned, hence this optimisation */
632 if (--width > sizeof(buf) - 2)
633 width = sizeof(buf) - 2;
636 flags |= SIGNOK | NDIGITS | NZDIGITS;
637 for (p = buf; width; width--) {
640 * Switch on the character; `goto ok'
641 * if we accept it as a part of number.
646 * The digit 0 is always legal, but is
647 * special. For %i conversions, if no
648 * digits (zero or nonzero) have been
649 * scanned (only signs), we will have
650 * base==0. In that case, we should set
651 * it to 8 and enable 0x prefixing.
652 * Also, if we have not scanned zero digits
653 * before this, do not turn off prefixing
654 * (someone else will turn it off if we
655 * have scanned any nonzero digits).
662 if (flags & NZDIGITS)
663 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
665 flags &= ~(SIGNOK|PFXOK|NDIGITS);
668 /* 1 through 7 always legal */
669 case '1': case '2': case '3':
670 case '4': case '5': case '6': case '7':
671 base = basefix[base];
672 flags &= ~(SIGNOK | PFXOK | NDIGITS);
675 /* digits 8 and 9 ok iff decimal or hex */
677 base = basefix[base];
679 break; /* not legal here */
680 flags &= ~(SIGNOK | PFXOK | NDIGITS);
683 /* letters ok iff hex */
684 case 'A': case 'B': case 'C':
685 case 'D': case 'E': case 'F':
686 case 'a': case 'b': case 'c':
687 case 'd': case 'e': case 'f':
688 /* no need to fix base here */
690 break; /* not legal here */
691 flags &= ~(SIGNOK | PFXOK | NDIGITS);
694 /* sign ok only as first character */
696 if (flags & SIGNOK) {
704 * x ok iff flag still set & 2nd char (or
705 * 3rd char if we have a sign).
708 if (flags & PFXOK && p ==
709 buf + 1 + !!(flags & HAVESIGN)) {
710 base = 16; /* if %i */
718 * If we got here, c is not a legal character
719 * for a number. Stop accumulating digits.
724 * c is legal: store it and look at the next.
729 else if (__srefill(fp))
733 * If we had only a sign, it is no good; push
734 * back the sign. If the number ends in `x',
735 * it was [sign] '0' 'x', so push back the x
736 * and treat it as [sign] '0'.
738 if (flags & NDIGITS) {
740 (void) __ungetc(*(u_char *)--p, fp);
743 c = ((u_char *)p)[-1];
744 if (c == 'x' || c == 'X') {
746 (void) __ungetc(c, fp);
748 if ((flags & SUPPRESS) == 0) {
752 if ((flags & UNSIGNED) == 0)
753 res = strtoimax_l(buf, (char **)NULL, base, locale);
755 res = strtoumax_l(buf, (char **)NULL, base, locale);
757 *va_arg(ap, void **) =
758 (void *)(uintptr_t)res;
759 else if (flags & SHORTSHORT)
760 *va_arg(ap, char *) = res;
761 else if (flags & SHORT)
762 *va_arg(ap, short *) = res;
763 else if (flags & LONG)
764 *va_arg(ap, long *) = res;
765 else if (flags & LONGLONG)
766 *va_arg(ap, long long *) = res;
767 else if (flags & INTMAXT)
768 *va_arg(ap, intmax_t *) = res;
769 else if (flags & PTRDIFFT)
770 *va_arg(ap, ptrdiff_t *) = res;
771 else if (flags & SIZET)
772 *va_arg(ap, size_t *) = res;
774 *va_arg(ap, int *) = res;
781 #ifndef NO_FLOATING_POINT
783 /* scan a floating point number as if by strtod */
784 if (width == 0 || width > sizeof(buf) - 1)
785 width = sizeof(buf) - 1;
786 if ((width = parsefloat(fp, buf, buf + width, locale)) == 0)
788 if ((flags & SUPPRESS) == 0) {
789 if (flags & LONGDBL) {
790 long double res = strtold_l(buf, &p, locale);
791 *va_arg(ap, long double *) = res;
792 } else if (flags & LONG) {
793 double res = strtod_l(buf, &p, locale);
794 *va_arg(ap, double *) = res;
796 float res = strtof_l(buf, &p, locale);
797 *va_arg(ap, float *) = res;
804 #endif /* !NO_FLOATING_POINT */
808 return (nconversions != 0 ? nassigned : EOF);
814 * Fill in the given table from the scanset at the given format
815 * (just after `['). Return a pointer to the character past the
816 * closing `]'. The table has a 1 wherever characters should be
817 * considered part of the scanset.
819 static const u_char *
825 struct xlocale_collate *table =
826 (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
828 /* first `clear' the whole table */
829 c = *fmt++; /* first char hat => negated scanset */
831 v = 1; /* default => accept */
832 c = *fmt++; /* get new first char */
834 v = 0; /* default => reject */
836 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
837 (void) memset(tab, v, 256);
840 return (fmt - 1);/* format ended before closing ] */
843 * Now set the entries corresponding to the actual scanset
844 * to the opposite of the above.
846 * The first character may be ']' (or '-') without being special;
847 * the last character may be '-'.
851 tab[c] = v; /* take character c */
853 n = *fmt++; /* and examine the next */
856 case 0: /* format ended too soon */
861 * A scanset of the form
863 * is defined as `the digit 0, the digit 1,
864 * the character +, the character -', but
865 * the effect of a scanset such as
867 * is implementation defined. The V7 Unix
868 * scanf treats `a-z' as `the letters a through
869 * z', but treats `a-a' as `the letter a, the
870 * character -, and the letter a'.
872 * For compatibility, the `-' is not considerd
873 * to define a range if the character following
874 * it is either a close bracket (required by ANSI)
875 * or is not numerically greater than the character
876 * we just stored in the table (c).
880 || (table->__collate_load_error ? n < c :
881 __collate_range_cmp (table, n, c) < 0
885 break; /* resume the for(;;) */
888 /* fill in the range */
889 if (table->__collate_load_error) {
894 for (i = 0; i < 256; i ++)
895 if ( __collate_range_cmp (table, c, i) < 0
896 && __collate_range_cmp (table, i, n) <= 0
900 #if 1 /* XXX another disgusting compatibility hack */
903 * Alas, the V7 Unix scanf also treats formats
904 * such as [a-c-e] as `the letters a through e'.
905 * This too is permitted by the standard....
917 case ']': /* end of scanset */
920 default: /* just another character */
928 #ifndef NO_FLOATING_POINT
930 parsefloat(FILE *fp, char *buf, char *end, locale_t locale)
933 int infnanpos = 0, decptpos = 0;
935 S_START, S_GOTSIGN, S_INF, S_NAN, S_DONE, S_MAYBEHEX,
936 S_DIGITS, S_DECPT, S_FRAC, S_EXP, S_EXPDIGITS
939 const char *decpt = localeconv_l(locale)->decimal_point;
940 _Bool gotmantdig = 0, ishex = 0;
943 * We set commit = p whenever the string we have read so far
944 * constitutes a valid representation of a floating point
945 * number by itself. At some point, the parse will complete
946 * or fail, and we will ungetc() back to the last commit point.
947 * To ensure that the file offset gets updated properly, it is
948 * always necessary to read at least one character that doesn't
949 * match; thus, we can't short-circuit "infinity" or "nan(...)".
952 for (p = buf; p < end; ) {
958 if (c == '-' || c == '+')
983 (c != "nfinity"[infnanpos] &&
984 c != "NFINITY"[infnanpos]))
986 if (infnanpos == 1 || infnanpos == 6)
987 commit = p; /* inf or infinity */
993 if (c != 'A' && c != 'a')
997 if (c != 'N' && c != 'n')
1010 } else if (!isalnum(c) && c != '_')
1020 if (c == 'X' || c == 'x') {
1023 } else { /* we saw a '0', but no 'x' */
1028 if ((ishex && isxdigit(c)) || isdigit(c)) {
1037 if (c == decpt[decptpos]) {
1038 if (decpt[++decptpos] == '\0') {
1039 /* We read the complete decpt seq. */
1045 } else if (!decptpos) {
1046 /* We didn't read any decpt characters. */
1051 * We read part of a multibyte decimal point,
1052 * but the rest is invalid, so bail.
1057 if (((c == 'E' || c == 'e') && !ishex) ||
1058 ((c == 'P' || c == 'p') && ishex)) {
1063 } else if ((ishex && isxdigit(c)) || isdigit(c)) {
1070 state = S_EXPDIGITS;
1071 if (c == '-' || c == '+')
1087 else if (__srefill(fp))
1092 while (commit < --p)
1093 __ungetc(*(u_char *)p, fp);
1095 return (commit - buf);