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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid
[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfscanf.c,v 1.43 2009/01/19 06:19:51 das Exp $");
39 #include "xlocale_private.h"
41 #include "namespace.h"
52 #include "un-namespace.h"
55 #include "libc_private.h"
58 #ifndef NO_FLOATING_POINT
62 #define BUF 513 /* Maximum length of numeric string. */
65 * Flags used during conversion.
67 #define LONG 0x01 /* l: long or double */
68 #define LONGDBL 0x02 /* L: long double */
69 #define SHORT 0x04 /* h: short */
70 #define SUPPRESS 0x08 /* *: suppress assignment */
71 #define POINTER 0x10 /* p: void * (as hex) */
72 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
73 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
74 #define INTMAXT 0x800 /* j: intmax_t */
75 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
76 #define SIZET 0x2000 /* z: size_t */
77 #define SHORTSHORT 0x4000 /* hh: char */
78 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
81 * The following are used in integral conversions only:
82 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
84 #define SIGNOK 0x40 /* +/- is (still) legal */
85 #define NDIGITS 0x80 /* no digits detected */
86 #define PFXOK 0x100 /* 0x prefix is (still) legal */
87 #define NZDIGITS 0x200 /* no zero digits detected */
88 #define HAVESIGN 0x10000 /* sign detected */
93 #define CT_CHAR 0 /* %c conversion */
94 #define CT_CCL 1 /* %[...] conversion */
95 #define CT_STRING 2 /* %s conversion */
96 #define CT_INT 3 /* %[dioupxX] conversion */
97 #define CT_FLOAT 4 /* %[efgEFG] conversion */
99 static const u_char
*__sccl(char *, const u_char
*, locale_t
);
100 #ifndef NO_FLOATING_POINT
101 static int parsefloat(FILE *, char **, size_t, locale_t
);
104 __weak_reference(__vfscanf
, vfscanf
);
107 * __vfscanf - MT-safe version
110 __vfscanf(FILE * __restrict fp
, char const * __restrict fmt0
, va_list ap
)
115 ret
= __svfscanf_l(fp
, __current_locale(), fmt0
, ap
);
121 vfscanf_l(FILE * __restrict fp
, locale_t loc
, char const * __restrict fmt0
, va_list ap
)
125 NORMALIZE_LOCALE(loc
);
127 ret
= __svfscanf_l(fp
, loc
, fmt0
, ap
);
133 * __svfscanf - non-MT-safe version of __vfscanf
135 __private_extern__
int
136 __svfscanf_l(FILE * __restrict fp
, locale_t loc
, const char * __restrict fmt0
, va_list ap
)
138 const u_char
*fmt
= (const u_char
*)fmt0
;
139 int c
; /* character from format, or conversion */
140 size_t width
; /* field width, or 0 */
141 char *p
; /* points into all kinds of strings */
142 int n
; /* handy integer */
143 int flags
; /* flags as defined above */
144 char *p0
; /* saves original value of p when necessary */
145 int nassigned
; /* number of fields assigned */
146 int nread
; /* number of characters consumed from fp */
147 int base
; /* base argument to conversion function */
148 char ccltab
[256]; /* character class table for %[...] */
149 char buf
[BUF
]; /* buffer for numeric and mb conversions */
150 wchar_t *wcp
; /* handy wide character pointer */
151 size_t nconv
; /* length of multibyte sequence converted */
152 int index
; /* %index$, zero if unset */
153 va_list ap_orig
; /* to reset ap to first argument */
154 static const mbstate_t initial
;
158 /* `basefix' is used to avoid `if' tests in the integer scanner */
159 static const short basefix
[17] =
160 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
162 NORMALIZE_LOCALE(loc
);
163 mb_cur_max
= MB_CUR_MAX_L(loc
);
168 va_copy(ap_orig
, ap
);
173 if (isspace_l(c
, loc
)) {
174 while ((fp
->_r
> 0 || __srefill(fp
) == 0) && isspace_l(*fp
->_p
, loc
))
175 nread
++, fp
->_r
--, fp
->_p
++;
179 if (fp
->_r
<= 0 && __srefill(fp
))
186 * switch on the format. continue if done;
187 * break once format type is derived.
192 /* Consume leading white space */
194 if (fp
->_r
<= 0 && __srefill(fp
))
196 if (!isspace_l(*fp
->_p
, loc
))
211 if (index
< 1 || index
> NL_ARGMAX
|| fmt
[-3] != '%') {
216 va_copy(ap
, ap_orig
); /* reset to %1$ */
217 for (; index
> 1; index
--) {
235 flags
|= LONGLONG
; /* not quite */
254 case '0': case '1': case '2': case '3': case '4':
255 case '5': case '6': case '7': case '8': case '9':
256 width
= width
* 10 + c
- '0';
286 flags
|= PFXOK
; /* enable 0x prefixing */
292 #ifndef NO_FLOATING_POINT
293 case 'A': case 'E': case 'F': case 'G':
294 case 'a': case 'e': case 'f': case 'g':
307 fmt
= __sccl(ccltab
, fmt
, loc
);
320 case 'p': /* pointer format is like hex */
321 flags
|= POINTER
| PFXOK
;
322 c
= CT_INT
; /* assumes sizeof(uintmax_t) */
323 flags
|= UNSIGNED
; /* >= sizeof(uintptr_t) */
329 if (flags
& SUPPRESS
) /* ??? */
331 void *ptr
= va_arg(ap
, void *);
334 else if (flags
& SHORTSHORT
)
335 *(char *)ptr
= nread
;
336 else if (flags
& SHORT
)
337 *(short *)ptr
= nread
;
338 else if (flags
& LONG
)
339 *(long *)ptr
= nread
;
340 else if (flags
& LONGLONG
)
341 *(long long *)ptr
= nread
;
342 else if (flags
& INTMAXT
)
343 *(intmax_t *)ptr
= nread
;
344 else if (flags
& SIZET
)
345 *(size_t *)ptr
= nread
;
346 else if (flags
& PTRDIFFT
)
347 *(ptrdiff_t *)ptr
= nread
;
356 * Disgusting backwards compatibility hack. XXX
358 case '\0': /* compat */
363 * We have a conversion that requires input.
365 if (fp
->_r
<= 0 && __srefill(fp
))
369 * Consume leading white space, except for formats
370 * that suppress this.
372 if ((flags
& NOSKIP
) == 0) {
373 while (isspace_l(*fp
->_p
, loc
)) {
377 else if (__srefill(fp
))
381 * Note that there is at least one character in
382 * the buffer, so conversions that do not set NOSKIP
383 * ca no longer result in an input failure.
393 /* scan arbitrary characters (sets NOSKIP) */
397 if ((flags
& SUPPRESS
) == 0)
398 wcp
= va_arg(ap
, wchar_t *);
403 if (n
== mb_cur_max
) {
404 fp
->_flags
|= __SERR
;
411 nconv
= mbrtowc_l(wcp
, buf
, n
, &mbs
, loc
);
412 if (nconv
== (size_t)-1) {
413 fp
->_flags
|= __SERR
;
416 if (nconv
== 0 && !(flags
& SUPPRESS
))
418 if (nconv
!= (size_t)-2) {
421 if (!(flags
& SUPPRESS
))
425 if (fp
->_r
<= 0 && __srefill(fp
)) {
427 fp
->_flags
|= __SERR
;
433 if (!(flags
& SUPPRESS
))
435 } else if (flags
& SUPPRESS
) {
438 if ((n
= fp
->_r
) < width
) {
456 size_t r
= __fread((void *)va_arg(ap
, char *), 1,
467 /* scan a (nonempty) character class (sets NOSKIP) */
469 width
= (size_t)~0; /* `infinity' */
470 /* take only those things in the class */
475 if ((flags
& SUPPRESS
) == 0)
476 wcp
= va_arg(ap
, wchar_t *);
482 if (n
== mb_cur_max
) {
483 fp
->_flags
|= __SERR
;
490 nconv
= mbrtowc_l(wcp
, buf
, n
, &mbs
, loc
);
491 if (nconv
== (size_t)-1) {
492 fp
->_flags
|= __SERR
;
497 if (nconv
!= (size_t)-2) {
498 if (wctob_l(*wcp
, loc
) != EOF
&&
499 !ccltab
[wctob_l(*wcp
, loc
)]) {
509 if (!(flags
& SUPPRESS
))
514 if (fp
->_r
<= 0 && __srefill(fp
)) {
516 fp
->_flags
|= __SERR
;
523 fp
->_flags
|= __SERR
;
529 if (!(flags
& SUPPRESS
)) {
533 } else if (flags
& SUPPRESS
) {
535 while (ccltab
[*fp
->_p
]) {
536 n
++, fp
->_r
--, fp
->_p
++;
539 if (fp
->_r
<= 0 && __srefill(fp
)) {
548 p0
= p
= va_arg(ap
, char *);
549 while (ccltab
[*fp
->_p
]) {
554 if (fp
->_r
<= 0 && __srefill(fp
)) {
570 /* like CCL, but zero-length string OK, & no NOSKIP */
576 if ((flags
& SUPPRESS
) == 0)
577 wcp
= va_arg(ap
, wchar_t *);
582 if (n
== mb_cur_max
) {
583 fp
->_flags
|= __SERR
;
590 nconv
= mbrtowc_l(wcp
, buf
, n
, &mbs
, loc
);
591 if (nconv
== (size_t)-1) {
592 fp
->_flags
|= __SERR
;
597 if (nconv
!= (size_t)-2) {
598 if (iswspace_l(*wcp
, loc
)) {
608 if (!(flags
& SUPPRESS
))
612 if (fp
->_r
<= 0 && __srefill(fp
)) {
614 fp
->_flags
|= __SERR
;
620 if (!(flags
& SUPPRESS
)) {
624 } else if (flags
& SUPPRESS
) {
626 while (!isspace_l(*fp
->_p
, loc
)) {
627 n
++, fp
->_r
--, fp
->_p
++;
630 if (fp
->_r
<= 0 && __srefill(fp
))
635 p0
= p
= va_arg(ap
, char *);
636 while (!isspace_l(*fp
->_p
, loc
)) {
641 if (fp
->_r
<= 0 && __srefill(fp
))
651 /* scan an integer as if by the conversion function */
653 if (width
== 0 || width
> sizeof(buf
) - 1)
654 width
= sizeof(buf
) - 1;
656 /* size_t is unsigned, hence this optimisation */
657 if (--width
> sizeof(buf
) - 2)
658 width
= sizeof(buf
) - 2;
661 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
662 for (p
= buf
; width
; width
--) {
665 * Switch on the character; `goto ok'
666 * if we accept it as a part of number.
671 * The digit 0 is always legal, but is
672 * special. For %i conversions, if no
673 * digits (zero or nonzero) have been
674 * scanned (only signs), we will have
675 * base==0. In that case, we should set
676 * it to 8 and enable 0x prefixing.
677 * Also, if we have not scanned zero digits
678 * before this, do not turn off prefixing
679 * (someone else will turn it off if we
680 * have scanned any nonzero digits).
687 if (flags
& NZDIGITS
)
688 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
690 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
693 /* 1 through 7 always legal */
694 case '1': case '2': case '3':
695 case '4': case '5': case '6': case '7':
696 base
= basefix
[base
];
697 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
700 /* digits 8 and 9 ok iff decimal or hex */
702 base
= basefix
[base
];
704 break; /* not legal here */
705 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
708 /* letters ok iff hex */
709 case 'A': case 'B': case 'C':
710 case 'D': case 'E': case 'F':
711 case 'a': case 'b': case 'c':
712 case 'd': case 'e': case 'f':
713 /* no need to fix base here */
715 break; /* not legal here */
716 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
719 /* sign ok only as first character */
721 if (flags
& SIGNOK
) {
729 * x ok iff flag still set & 2nd char (or
730 * 3rd char if we have a sign).
733 if (flags
& PFXOK
&& p
==
734 buf
+ 1 + !!(flags
& HAVESIGN
)) {
735 base
= 16; /* if %i */
743 * If we got here, c is not a legal character
744 * for a number. Stop accumulating digits.
749 * c is legal: store it and look at the next.
754 else if (__srefill(fp
))
758 * If we had only a sign, it is no good; push
759 * back the sign. If the number ends in `x',
760 * it was [sign] '0' 'x', so push back the x
761 * and treat it as [sign] '0'.
763 if (flags
& NDIGITS
) {
765 (void) __ungetc(*(u_char
*)--p
, fp
);
768 c
= ((u_char
*)p
)[-1];
769 if (c
== 'x' || c
== 'X') {
771 (void) __ungetc(c
, fp
);
773 if ((flags
& SUPPRESS
) == 0) {
777 if ((flags
& UNSIGNED
) == 0)
778 res
= strtoimax_l(buf
, (char **)NULL
, base
, loc
);
780 res
= strtoumax_l(buf
, (char **)NULL
, base
, loc
);
782 *va_arg(ap
, void **) =
783 (void *)(uintptr_t)res
;
784 else if (flags
& SHORTSHORT
)
785 *va_arg(ap
, char *) = res
;
786 else if (flags
& SHORT
)
787 *va_arg(ap
, short *) = res
;
788 else if (flags
& LONG
)
789 *va_arg(ap
, long *) = res
;
790 else if (flags
& LONGLONG
)
791 *va_arg(ap
, long long *) = res
;
792 else if (flags
& INTMAXT
)
793 *va_arg(ap
, intmax_t *) = res
;
794 else if (flags
& PTRDIFFT
)
795 *va_arg(ap
, ptrdiff_t *) = res
;
796 else if (flags
& SIZET
)
797 *va_arg(ap
, size_t *) = res
;
799 *va_arg(ap
, int *) = res
;
805 #ifndef NO_FLOATING_POINT
809 /* scan a floating point number as if by strtod */
810 if ((width
= parsefloat(fp
, &pbuf
, width
, loc
)) == 0)
812 if ((flags
& SUPPRESS
) == 0) {
813 if (flags
& LONGDBL
) {
814 long double res
= strtold_l(pbuf
, &p
, loc
);
815 *va_arg(ap
, long double *) = res
;
816 } else if (flags
& LONG
) {
817 double res
= strtod_l(pbuf
, &p
, loc
);
818 *va_arg(ap
, double *) = res
;
820 float res
= strtof_l(pbuf
, &p
, loc
);
821 *va_arg(ap
, float *) = res
;
828 #endif /* !NO_FLOATING_POINT */
832 return (nassigned
? nassigned
: EOF
);
838 __svfscanf(FILE * __restrict fp
, const char * __restrict fmt0
, va_list ap
)
840 return __svfscanf_l(fp
, __current_locale(), fmt0
, ap
);
844 * Fill in the given table from the scanset at the given format
845 * (just after `['). Return a pointer to the character past the
846 * closing `]'. The table has a 1 wherever characters should be
847 * considered part of the scanset.
849 static const u_char
*
850 __sccl(tab
, fmt
, loc
)
857 /* first `clear' the whole table */
858 c
= *fmt
++; /* first char hat => negated scanset */
860 v
= 1; /* default => accept */
861 c
= *fmt
++; /* get new first char */
863 v
= 0; /* default => reject */
865 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
866 (void) memset(tab
, v
, 256);
869 return (fmt
- 1);/* format ended before closing ] */
872 * Now set the entries corresponding to the actual scanset
873 * to the opposite of the above.
875 * The first character may be ']' (or '-') without being special;
876 * the last character may be '-'.
880 tab
[c
] = v
; /* take character c */
882 n
= *fmt
++; /* and examine the next */
885 case 0: /* format ended too soon */
891 * A scanset of the form
893 * is defined as `the digit 0, the digit 1,
894 * the character +, the character -', but
895 * the effect of a scanset such as
897 * is implementation defined. The V7 Unix
898 * scanf treats `a-z' as `the letters a through
899 * z', but treats `a-a' as `the letter a, the
900 * character -, and the letter a'.
902 * For compatibility, the `-' is not considerd
903 * to define a range if the character following
904 * it is either a close bracket (required by ANSI)
905 * or is not numerically greater than the character
906 * we just stored in the table (c).
910 || (loc
->__collate_load_error
? n
< c
:
911 __collate_range_cmp (n
, c
, loc
) < 0
915 break; /* resume the for(;;) */
918 /* fill in the range */
919 if (loc
->__collate_load_error
) {
924 for (i
= 0; i
< 256; i
++)
925 if ( __collate_range_cmp (c
, i
, loc
) < 0
926 && __collate_range_cmp (i
, n
, loc
) <= 0
930 #if 1 /* XXX another disgusting compatibility hack */
933 * Alas, the V7 Unix scanf also treats formats
934 * such as [a-c-e] as `the letters a through e'.
935 * This too is permitted by the standard....
947 case ']': /* end of scanset */
950 default: /* just another character */
958 #ifndef NO_FLOATING_POINT
960 * Maintain a per-thread parsefloat buffer, shared by __svfscanf_l and
963 #ifdef BUILDING_VARIANT
964 extern char *__parsefloat_buf(size_t s
);
965 #else /* !BUILDING_VARIANT */
966 __private_extern__
char *
967 __parsefloat_buf(size_t s
)
970 static pthread_key_t parsefloat_tsd_key
= (pthread_key_t
)-1;
971 static pthread_mutex_t parsefloat_tsd_lock
= PTHREAD_MUTEX_INITIALIZER
;
972 static size_t bsiz
= 0;
974 if (parsefloat_tsd_key
== (pthread_key_t
)-1) {
975 pthread_mutex_lock(&parsefloat_tsd_lock
);
976 if (parsefloat_tsd_key
== (pthread_key_t
)-1) {
977 parsefloat_tsd_key
= __LIBC_PTHREAD_KEY_PARSEFLOAT
;
978 pthread_key_init_np(parsefloat_tsd_key
, free
);
980 pthread_mutex_unlock(&parsefloat_tsd_lock
);
982 if ((b
= (char *)pthread_getspecific(parsefloat_tsd_key
)) == NULL
) {
983 bsiz
= s
> BUF
? s
: BUF
;
984 b
= (char *)malloc(bsiz
);
989 pthread_setspecific(parsefloat_tsd_key
, b
);
993 b
= (char *)reallocf(b
, s
);
994 pthread_setspecific(parsefloat_tsd_key
, b
);
1003 #endif /* BUILDING_VARIANT */
1006 parsefloat(FILE *fp
, char **buf
, size_t width
, locale_t loc
)
1009 int infnanpos
= 0, decptpos
= 0;
1011 S_START
, S_GOTSIGN
, S_INF
, S_NAN
, S_DONE
, S_MAYBEHEX
,
1012 S_DIGITS
, S_DECPT
, S_FRAC
, S_EXP
, S_EXPDIGITS
1015 const char *decpt
= localeconv_l(loc
)->decimal_point
;
1016 _Bool gotmantdig
= 0, ishex
= 0;
1021 s
= (width
== 0 ? BUF
: (width
+ 1));
1022 if ((b
= __parsefloat_buf(s
)) == NULL
) {
1028 * We set commit = p whenever the string we have read so far
1029 * constitutes a valid representation of a floating point
1030 * number by itself. At some point, the parse will complete
1031 * or fail, and we will ungetc() back to the last commit point.
1032 * To ensure that the file offset gets updated properly, it is
1033 * always necessary to read at least one character that doesn't
1034 * match; thus, we can't short-circuit "infinity" or "nan(...)".
1037 for (p
= b
; width
== 0 || p
< e
; ) {
1043 if (c
== '-' || c
== '+')
1067 if (infnanpos
> 6 ||
1068 (c
!= "nfinity"[infnanpos
] &&
1069 c
!= "NFINITY"[infnanpos
]))
1071 if (infnanpos
== 1 || infnanpos
== 6)
1072 commit
= p
; /* inf or infinity */
1076 switch (infnanpos
) {
1078 if (c
!= 'A' && c
!= 'a')
1082 if (c
!= 'N' && c
!= 'n')
1095 } else if (!isalnum_l(c
, loc
) && c
!= '_')
1105 if (c
== 'X' || c
== 'x') {
1108 } else { /* we saw a '0', but no 'x' */
1113 if ((ishex
&& isxdigit_l(c
, loc
)) || isdigit_l(c
, loc
)) {
1122 if (c
== decpt
[decptpos
]) {
1123 if (decpt
[++decptpos
] == '\0') {
1124 /* We read the complete decpt seq. */
1130 } else if (!decptpos
) {
1131 /* We didn't read any decpt characters. */
1136 * We read part of a multibyte decimal point,
1137 * but the rest is invalid, so bail.
1142 if (((c
== 'E' || c
== 'e') && !ishex
) ||
1143 ((c
== 'P' || c
== 'p') && ishex
)) {
1148 } else if ((ishex
&& isxdigit_l(c
, loc
)) || isdigit_l(c
, loc
)) {
1155 state
= S_EXPDIGITS
;
1156 if (c
== '-' || c
== '+')
1161 if (isdigit_l(c
, loc
))
1167 LIBC_ABORT("unknown state %d", state
);
1170 ssize_t diff
= (p
- b
);
1171 ssize_t com
= (commit
- b
);
1173 b
= __parsefloat_buf(s
);
1185 else if (__srefill(fp
))
1190 while (commit
< --p
)
1191 __ungetc(*(u_char
*)p
, fp
);
1194 return (commit
- b
);