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 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include "namespace.h"
53 #include "un-namespace.h"
56 #include "libc_private.h"
59 #ifndef NO_FLOATING_POINT
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 static const u_char *__sccl(char *, const u_char *);
101 #ifndef NO_FLOATING_POINT
102 static int parsefloat(FILE *, char *, char *);
105 int __scanfdebug = 0;
107 __weak_reference(__vfscanf, vfscanf);
110 * __vfscanf - MT-safe version
113 __vfscanf(FILE *fp, char const *fmt0, va_list ap)
118 ret = __svfscanf(fp, fmt0, ap);
124 * __svfscanf - non-MT-safe version of __vfscanf
127 __svfscanf(FILE *fp, const char *fmt0, va_list ap)
129 const u_char *fmt = (const u_char *)fmt0;
130 int c; /* character from format, or conversion */
131 size_t width; /* field width, or 0 */
132 char *p; /* points into all kinds of strings */
133 int n; /* handy integer */
134 int flags; /* flags as defined above */
135 char *p0; /* saves original value of p when necessary */
136 int nassigned; /* number of fields assigned */
137 int nconversions; /* number of conversions */
138 int nread; /* number of characters consumed from fp */
139 int base; /* base argument to conversion function */
140 char ccltab[256]; /* character class table for %[...] */
141 char buf[BUF]; /* buffer for numeric and mb conversions */
142 wchar_t *wcp; /* handy wide character pointer */
143 size_t nconv; /* length of multibyte sequence converted */
144 static const mbstate_t initial;
147 /* `basefix' is used to avoid `if' tests in the integer scanner */
148 static short basefix[17] =
149 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
161 while ((fp->_r > 0 || __srefill(fp) == 0) && isspace(*fp->_p))
162 nread++, fp->_r--, fp->_p++;
170 * switch on the format. continue if done;
171 * break once format type is derived.
177 if (fp->_r <= 0 && __srefill(fp))
199 flags |= LONGLONG; /* not quite */
218 case '0': case '1': case '2': case '3': case '4':
219 case '5': case '6': case '7': case '8': case '9':
220 width = width * 10 + c - '0';
250 flags |= PFXOK; /* enable 0x prefixing */
256 #ifndef NO_FLOATING_POINT
257 case 'A': case 'E': case 'F': case 'G':
258 case 'a': case 'e': case 'f': case 'g':
271 fmt = __sccl(ccltab, fmt);
284 case 'p': /* pointer format is like hex */
285 flags |= POINTER | PFXOK;
286 c = CT_INT; /* assumes sizeof(uintmax_t) */
287 flags |= UNSIGNED; /* >= sizeof(uintptr_t) */
293 if (flags & SUPPRESS) /* ??? */
295 if (flags & SHORTSHORT)
296 *va_arg(ap, char *) = nread;
297 else if (flags & SHORT)
298 *va_arg(ap, short *) = nread;
299 else if (flags & LONG)
300 *va_arg(ap, long *) = nread;
301 else if (flags & LONGLONG)
302 *va_arg(ap, long long *) = nread;
303 else if (flags & INTMAXT)
304 *va_arg(ap, intmax_t *) = nread;
305 else if (flags & SIZET)
306 *va_arg(ap, size_t *) = nread;
307 else if (flags & PTRDIFFT)
308 *va_arg(ap, ptrdiff_t *) = nread;
310 *va_arg(ap, int *) = nread;
317 * Disgusting backwards compatibility hack. XXX
319 case '\0': /* compat */
324 * We have a conversion that requires input.
326 if (fp->_r <= 0 && __srefill(fp))
330 * Consume leading white space, except for formats
331 * that suppress this.
333 if ((flags & NOSKIP) == 0) {
334 while (isspace(*fp->_p)) {
338 else if (__srefill(fp))
342 * Note that there is at least one character in
343 * the buffer, so conversions that do not set NOSKIP
344 * ca no longer result in an input failure.
354 /* scan arbitrary characters (sets NOSKIP) */
358 if ((flags & SUPPRESS) == 0)
359 wcp = va_arg(ap, wchar_t *);
364 if (n == MB_CUR_MAX) {
365 fp->_flags |= __SERR;
372 nconv = mbrtowc(wcp, buf, n, &mbs);
373 if (nconv == (size_t)-1) {
374 fp->_flags |= __SERR;
377 if (nconv == 0 && !(flags & SUPPRESS))
379 if (nconv != (size_t)-2) {
382 if (!(flags & SUPPRESS))
386 if (fp->_r <= 0 && __srefill(fp)) {
388 fp->_flags |= __SERR;
394 if (!(flags & SUPPRESS))
396 } else if (flags & SUPPRESS) {
399 if ((n = fp->_r) < width) {
417 size_t r = __fread((void *)va_arg(ap, char *), 1,
429 /* scan a (nonempty) character class (sets NOSKIP) */
431 width = (size_t)~0; /* `infinity' */
432 /* take only those things in the class */
437 if ((flags & SUPPRESS) == 0)
438 wcp = va_arg(ap, wchar_t *);
444 if (n == MB_CUR_MAX) {
445 fp->_flags |= __SERR;
452 nconv = mbrtowc(wcp, buf, n, &mbs);
453 if (nconv == (size_t)-1) {
454 fp->_flags |= __SERR;
459 if (nconv != (size_t)-2) {
460 if (wctob(*wcp) != EOF &&
461 !ccltab[wctob(*wcp)]) {
471 if (!(flags & SUPPRESS))
476 if (fp->_r <= 0 && __srefill(fp)) {
478 fp->_flags |= __SERR;
485 fp->_flags |= __SERR;
491 if (!(flags & SUPPRESS)) {
495 } else if (flags & SUPPRESS) {
497 while (ccltab[*fp->_p]) {
498 n++, fp->_r--, fp->_p++;
501 if (fp->_r <= 0 && __srefill(fp)) {
510 p0 = p = va_arg(ap, char *);
511 while (ccltab[*fp->_p]) {
516 if (fp->_r <= 0 && __srefill(fp)) {
533 /* like CCL, but zero-length string OK, & no NOSKIP */
539 if ((flags & SUPPRESS) == 0)
540 wcp = va_arg(ap, wchar_t *);
544 while (!isspace(*fp->_p) && width != 0) {
545 if (n == MB_CUR_MAX) {
546 fp->_flags |= __SERR;
553 nconv = mbrtowc(wcp, buf, n, &mbs);
554 if (nconv == (size_t)-1) {
555 fp->_flags |= __SERR;
560 if (nconv != (size_t)-2) {
561 if (iswspace(*wcp)) {
571 if (!(flags & SUPPRESS))
575 if (fp->_r <= 0 && __srefill(fp)) {
577 fp->_flags |= __SERR;
583 if (!(flags & SUPPRESS)) {
587 } else if (flags & SUPPRESS) {
589 while (!isspace(*fp->_p)) {
590 n++, fp->_r--, fp->_p++;
593 if (fp->_r <= 0 && __srefill(fp))
598 p0 = p = va_arg(ap, char *);
599 while (!isspace(*fp->_p)) {
604 if (fp->_r <= 0 && __srefill(fp))
615 /* scan an integer as if by the conversion function */
617 if (width == 0 || width > sizeof(buf) - 1)
618 width = sizeof(buf) - 1;
620 /* size_t is unsigned, hence this optimisation */
621 if (--width > sizeof(buf) - 2)
622 width = sizeof(buf) - 2;
625 flags |= SIGNOK | NDIGITS | NZDIGITS;
626 for (p = buf; width; width--) {
629 * Switch on the character; `goto ok'
630 * if we accept it as a part of number.
635 * The digit 0 is always legal, but is
636 * special. For %i conversions, if no
637 * digits (zero or nonzero) have been
638 * scanned (only signs), we will have
639 * base==0. In that case, we should set
640 * it to 8 and enable 0x prefixing.
641 * Also, if we have not scanned zero digits
642 * before this, do not turn off prefixing
643 * (someone else will turn it off if we
644 * have scanned any nonzero digits).
651 if (flags & NZDIGITS)
652 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
654 flags &= ~(SIGNOK|PFXOK|NDIGITS);
657 /* 1 through 7 always legal */
658 case '1': case '2': case '3':
659 case '4': case '5': case '6': case '7':
660 base = basefix[base];
661 flags &= ~(SIGNOK | PFXOK | NDIGITS);
664 /* digits 8 and 9 ok iff decimal or hex */
666 base = basefix[base];
668 break; /* not legal here */
669 flags &= ~(SIGNOK | PFXOK | NDIGITS);
672 /* letters ok iff hex */
673 case 'A': case 'B': case 'C':
674 case 'D': case 'E': case 'F':
675 case 'a': case 'b': case 'c':
676 case 'd': case 'e': case 'f':
677 /* no need to fix base here */
679 break; /* not legal here */
680 flags &= ~(SIGNOK | PFXOK | NDIGITS);
683 /* sign ok only as first character */
685 if (flags & SIGNOK) {
693 * x ok iff flag still set & 2nd char (or
694 * 3rd char if we have a sign).
697 if (flags & PFXOK && p ==
698 buf + 1 + !!(flags & HAVESIGN)) {
699 base = 16; /* if %i */
707 * If we got here, c is not a legal character
708 * for a number. Stop accumulating digits.
713 * c is legal: store it and look at the next.
718 else if (__srefill(fp))
722 * If we had only a sign, it is no good; push
723 * back the sign. If the number ends in `x',
724 * it was [sign] '0' 'x', so push back the x
725 * and treat it as [sign] '0'.
727 if (flags & NDIGITS) {
729 (void) __ungetc(*(u_char *)--p, fp);
732 c = ((u_char *)p)[-1];
733 if (c == 'x' || c == 'X') {
735 (void) __ungetc(c, fp);
737 if ((flags & SUPPRESS) == 0) {
741 if ((flags & UNSIGNED) == 0)
742 res = strtoimax(buf, (char **)NULL, base);
744 res = strtoumax(buf, (char **)NULL, base);
746 *va_arg(ap, void **) =
747 (void *)(uintptr_t)res;
748 else if (flags & SHORTSHORT)
749 *va_arg(ap, char *) = res;
750 else if (flags & SHORT)
751 *va_arg(ap, short *) = res;
752 else if (flags & LONG)
753 *va_arg(ap, long *) = res;
754 else if (flags & LONGLONG)
755 *va_arg(ap, long long *) = res;
756 else if (flags & INTMAXT)
757 *va_arg(ap, intmax_t *) = res;
758 else if (flags & PTRDIFFT)
759 *va_arg(ap, ptrdiff_t *) = res;
760 else if (flags & SIZET)
761 *va_arg(ap, size_t *) = res;
763 *va_arg(ap, int *) = res;
770 #ifndef NO_FLOATING_POINT
772 /* scan a floating point number as if by strtod */
773 if (width == 0 || width > sizeof(buf) - 1)
774 width = sizeof(buf) - 1;
775 if ((width = parsefloat(fp, buf, buf + width)) == 0)
777 if ((flags & SUPPRESS) == 0) {
778 if (flags & LONGDBL) {
779 long double res = strtold(buf, &p);
780 *va_arg(ap, long double *) = res;
781 } else if (flags & LONG) {
782 double res = strtod(buf, &p);
783 *va_arg(ap, double *) = res;
785 float res = strtof(buf, &p);
786 *va_arg(ap, float *) = res;
788 if (__scanfdebug && p - buf != width)
795 #endif /* !NO_FLOATING_POINT */
799 return (nconversions != 0 ? nassigned : EOF);
805 * Fill in the given table from the scanset at the given format
806 * (just after `['). Return a pointer to the character past the
807 * closing `]'. The table has a 1 wherever characters should be
808 * considered part of the scanset.
810 static const u_char *
817 /* first `clear' the whole table */
818 c = *fmt++; /* first char hat => negated scanset */
820 v = 1; /* default => accept */
821 c = *fmt++; /* get new first char */
823 v = 0; /* default => reject */
825 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
826 (void) memset(tab, v, 256);
829 return (fmt - 1);/* format ended before closing ] */
832 * Now set the entries corresponding to the actual scanset
833 * to the opposite of the above.
835 * The first character may be ']' (or '-') without being special;
836 * the last character may be '-'.
840 tab[c] = v; /* take character c */
842 n = *fmt++; /* and examine the next */
845 case 0: /* format ended too soon */
850 * A scanset of the form
852 * is defined as `the digit 0, the digit 1,
853 * the character +, the character -', but
854 * the effect of a scanset such as
856 * is implementation defined. The V7 Unix
857 * scanf treats `a-z' as `the letters a through
858 * z', but treats `a-a' as `the letter a, the
859 * character -, and the letter a'.
861 * For compatibility, the `-' is not considerd
862 * to define a range if the character following
863 * it is either a close bracket (required by ANSI)
864 * or is not numerically greater than the character
865 * we just stored in the table (c).
869 || (__collate_load_error ? n < c :
870 __collate_range_cmp (n, c) < 0
874 break; /* resume the for(;;) */
877 /* fill in the range */
878 if (__collate_load_error) {
883 for (i = 0; i < 256; i ++)
884 if ( __collate_range_cmp (c, i) < 0
885 && __collate_range_cmp (i, n) <= 0
889 #if 1 /* XXX another disgusting compatibility hack */
892 * Alas, the V7 Unix scanf also treats formats
893 * such as [a-c-e] as `the letters a through e'.
894 * This too is permitted by the standard....
906 case ']': /* end of scanset */
909 default: /* just another character */
917 #ifndef NO_FLOATING_POINT
919 parsefloat(FILE *fp, char *buf, char *end)
924 S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
925 S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
928 char decpt = *localeconv()->decimal_point;
929 _Bool gotmantdig = 0, ishex = 0;
932 * We set commit = p whenever the string we have read so far
933 * constitutes a valid representation of a floating point
934 * number by itself. At some point, the parse will complete
935 * or fail, and we will ungetc() back to the last commit point.
936 * To ensure that the file offset gets updated properly, it is
937 * always necessary to read at least one character that doesn't
938 * match; thus, we can't short-circuit "infinity" or "nan(...)".
941 for (p = buf; p < end; ) {
947 if (c == '-' || c == '+')
972 (c != "nfinity"[infnanpos] &&
973 c != "NFINITY"[infnanpos]))
975 if (infnanpos == 1 || infnanpos == 6)
976 commit = p; /* inf or infinity */
981 case -1: /* XXX kludge to deal with nan(...) */
984 if (c != 'A' && c != 'a')
988 if (c != 'N' && c != 'n')
1001 } else if (!isalnum(c) && c != '_')
1009 if (c == 'X' || c == 'x') {
1012 } else { /* we saw a '0', but no 'x' */
1017 if ((ishex && isxdigit(c)) || isdigit(c))
1028 if (((c == 'E' || c == 'e') && !ishex) ||
1029 ((c == 'P' || c == 'p') && ishex)) {
1034 } else if ((ishex && isxdigit(c)) || isdigit(c)) {
1041 state = S_EXPDIGITS;
1042 if (c == '-' || c == '+')
1058 else if (__srefill(fp))
1063 while (commit < --p)
1064 __ungetc(*(u_char *)p, fp);
1066 return (commit - buf);