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 void *ptr
= va_arg(ap
, void *);
330 if ((ptr
== NULL
) || (flags
& SUPPRESS
)) /* ??? */
332 else if (flags
& SHORTSHORT
)
333 *(char *)ptr
= nread
;
334 else if (flags
& SHORT
)
335 *(short *)ptr
= nread
;
336 else if (flags
& LONG
)
337 *(long *)ptr
= nread
;
338 else if (flags
& LONGLONG
)
339 *(long long *)ptr
= nread
;
340 else if (flags
& INTMAXT
)
341 *(intmax_t *)ptr
= nread
;
342 else if (flags
& SIZET
)
343 *(size_t *)ptr
= nread
;
344 else if (flags
& PTRDIFFT
)
345 *(ptrdiff_t *)ptr
= nread
;
354 * Disgusting backwards compatibility hack. XXX
356 case '\0': /* compat */
361 * We have a conversion that requires input.
363 if (fp
->_r
<= 0 && __srefill(fp
))
367 * Consume leading white space, except for formats
368 * that suppress this.
370 if ((flags
& NOSKIP
) == 0) {
371 while (isspace_l(*fp
->_p
, loc
)) {
375 else if (__srefill(fp
))
379 * Note that there is at least one character in
380 * the buffer, so conversions that do not set NOSKIP
381 * ca no longer result in an input failure.
391 /* scan arbitrary characters (sets NOSKIP) */
395 if ((flags
& SUPPRESS
) == 0)
396 wcp
= va_arg(ap
, wchar_t *);
401 if (n
== mb_cur_max
) {
402 fp
->_flags
|= __SERR
;
409 nconv
= mbrtowc_l(wcp
, buf
, n
, &mbs
, loc
);
410 if (nconv
== (size_t)-1) {
411 fp
->_flags
|= __SERR
;
414 if (nconv
== 0 && !(flags
& SUPPRESS
))
416 if (nconv
!= (size_t)-2) {
419 if (!(flags
& SUPPRESS
))
423 if (fp
->_r
<= 0 && __srefill(fp
)) {
425 fp
->_flags
|= __SERR
;
431 if (!(flags
& SUPPRESS
))
433 } else if (flags
& SUPPRESS
) {
436 if ((n
= fp
->_r
) < width
) {
454 size_t r
= __fread((void *)va_arg(ap
, char *), 1,
465 /* scan a (nonempty) character class (sets NOSKIP) */
467 width
= (size_t)~0; /* `infinity' */
468 /* take only those things in the class */
473 if ((flags
& SUPPRESS
) == 0)
474 wcp
= va_arg(ap
, wchar_t *);
480 if (n
== mb_cur_max
) {
481 fp
->_flags
|= __SERR
;
488 nconv
= mbrtowc_l(wcp
, buf
, n
, &mbs
, loc
);
489 if (nconv
== (size_t)-1) {
490 fp
->_flags
|= __SERR
;
495 if (nconv
!= (size_t)-2) {
496 if (wctob_l(*wcp
, loc
) != EOF
&&
497 !ccltab
[wctob_l(*wcp
, loc
)]) {
507 if (!(flags
& SUPPRESS
))
512 if (fp
->_r
<= 0 && __srefill(fp
)) {
514 fp
->_flags
|= __SERR
;
521 fp
->_flags
|= __SERR
;
527 if (!(flags
& SUPPRESS
)) {
531 } else if (flags
& SUPPRESS
) {
533 while (ccltab
[*fp
->_p
]) {
534 n
++, fp
->_r
--, fp
->_p
++;
537 if (fp
->_r
<= 0 && __srefill(fp
)) {
546 p0
= p
= va_arg(ap
, char *);
547 while (ccltab
[*fp
->_p
]) {
552 if (fp
->_r
<= 0 && __srefill(fp
)) {
568 /* like CCL, but zero-length string OK, & no NOSKIP */
574 if ((flags
& SUPPRESS
) == 0)
575 wcp
= va_arg(ap
, wchar_t *);
580 if (n
== mb_cur_max
) {
581 fp
->_flags
|= __SERR
;
588 nconv
= mbrtowc_l(wcp
, buf
, n
, &mbs
, loc
);
589 if (nconv
== (size_t)-1) {
590 fp
->_flags
|= __SERR
;
595 if (nconv
!= (size_t)-2) {
596 if (iswspace_l(*wcp
, loc
)) {
606 if (!(flags
& SUPPRESS
))
610 if (fp
->_r
<= 0 && __srefill(fp
)) {
612 fp
->_flags
|= __SERR
;
618 if (!(flags
& SUPPRESS
)) {
622 } else if (flags
& SUPPRESS
) {
624 while (!isspace_l(*fp
->_p
, loc
)) {
625 n
++, fp
->_r
--, fp
->_p
++;
628 if (fp
->_r
<= 0 && __srefill(fp
))
633 p0
= p
= va_arg(ap
, char *);
634 while (!isspace_l(*fp
->_p
, loc
)) {
639 if (fp
->_r
<= 0 && __srefill(fp
))
649 /* scan an integer as if by the conversion function */
651 if (width
== 0 || width
> sizeof(buf
) - 1)
652 width
= sizeof(buf
) - 1;
654 /* size_t is unsigned, hence this optimisation */
655 if (--width
> sizeof(buf
) - 2)
656 width
= sizeof(buf
) - 2;
659 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
660 for (p
= buf
; width
; width
--) {
663 * Switch on the character; `goto ok'
664 * if we accept it as a part of number.
669 * The digit 0 is always legal, but is
670 * special. For %i conversions, if no
671 * digits (zero or nonzero) have been
672 * scanned (only signs), we will have
673 * base==0. In that case, we should set
674 * it to 8 and enable 0x prefixing.
675 * Also, if we have not scanned zero digits
676 * before this, do not turn off prefixing
677 * (someone else will turn it off if we
678 * have scanned any nonzero digits).
685 if (flags
& NZDIGITS
)
686 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
688 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
691 /* 1 through 7 always legal */
692 case '1': case '2': case '3':
693 case '4': case '5': case '6': case '7':
694 base
= basefix
[base
];
695 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
698 /* digits 8 and 9 ok iff decimal or hex */
700 base
= basefix
[base
];
702 break; /* not legal here */
703 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
706 /* letters ok iff hex */
707 case 'A': case 'B': case 'C':
708 case 'D': case 'E': case 'F':
709 case 'a': case 'b': case 'c':
710 case 'd': case 'e': case 'f':
711 /* no need to fix base here */
713 break; /* not legal here */
714 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
717 /* sign ok only as first character */
719 if (flags
& SIGNOK
) {
727 * x ok iff flag still set & 2nd char (or
728 * 3rd char if we have a sign).
731 if (flags
& PFXOK
&& p
==
732 buf
+ 1 + !!(flags
& HAVESIGN
)) {
733 base
= 16; /* if %i */
741 * If we got here, c is not a legal character
742 * for a number. Stop accumulating digits.
747 * c is legal: store it and look at the next.
752 else if (__srefill(fp
))
756 * If we had only a sign, it is no good; push
757 * back the sign. If the number ends in `x',
758 * it was [sign] '0' 'x', so push back the x
759 * and treat it as [sign] '0'.
761 if (flags
& NDIGITS
) {
763 (void) __ungetc(*(u_char
*)--p
, fp
);
766 c
= ((u_char
*)p
)[-1];
767 if (c
== 'x' || c
== 'X') {
769 (void) __ungetc(c
, fp
);
771 if ((flags
& SUPPRESS
) == 0) {
775 if ((flags
& UNSIGNED
) == 0)
776 res
= strtoimax_l(buf
, (char **)NULL
, base
, loc
);
778 res
= strtoumax_l(buf
, (char **)NULL
, base
, loc
);
780 *va_arg(ap
, void **) =
781 (void *)(uintptr_t)res
;
782 else if (flags
& SHORTSHORT
)
783 *va_arg(ap
, char *) = res
;
784 else if (flags
& SHORT
)
785 *va_arg(ap
, short *) = res
;
786 else if (flags
& LONG
)
787 *va_arg(ap
, long *) = res
;
788 else if (flags
& LONGLONG
)
789 *va_arg(ap
, long long *) = res
;
790 else if (flags
& INTMAXT
)
791 *va_arg(ap
, intmax_t *) = res
;
792 else if (flags
& PTRDIFFT
)
793 *va_arg(ap
, ptrdiff_t *) = res
;
794 else if (flags
& SIZET
)
795 *va_arg(ap
, size_t *) = res
;
797 *va_arg(ap
, int *) = res
;
803 #ifndef NO_FLOATING_POINT
807 /* scan a floating point number as if by strtod */
808 if ((width
= parsefloat(fp
, &pbuf
, width
, loc
)) == 0)
810 if ((flags
& SUPPRESS
) == 0) {
811 if (flags
& LONGDBL
) {
812 long double res
= strtold_l(pbuf
, &p
, loc
);
813 *va_arg(ap
, long double *) = res
;
814 } else if (flags
& LONG
) {
815 double res
= strtod_l(pbuf
, &p
, loc
);
816 *va_arg(ap
, double *) = res
;
818 float res
= strtof_l(pbuf
, &p
, loc
);
819 *va_arg(ap
, float *) = res
;
826 #endif /* !NO_FLOATING_POINT */
830 return (nassigned
? nassigned
: EOF
);
836 __svfscanf(FILE * __restrict fp
, const char * __restrict fmt0
, va_list ap
)
838 return __svfscanf_l(fp
, __current_locale(), fmt0
, ap
);
842 * Fill in the given table from the scanset at the given format
843 * (just after `['). Return a pointer to the character past the
844 * closing `]'. The table has a 1 wherever characters should be
845 * considered part of the scanset.
847 static const u_char
*
848 __sccl(tab
, fmt
, loc
)
855 /* first `clear' the whole table */
856 c
= *fmt
++; /* first char hat => negated scanset */
858 v
= 1; /* default => accept */
859 c
= *fmt
++; /* get new first char */
861 v
= 0; /* default => reject */
863 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
864 (void) memset(tab
, v
, 256);
867 return (fmt
- 1);/* format ended before closing ] */
870 * Now set the entries corresponding to the actual scanset
871 * to the opposite of the above.
873 * The first character may be ']' (or '-') without being special;
874 * the last character may be '-'.
878 tab
[c
] = v
; /* take character c */
880 n
= *fmt
++; /* and examine the next */
883 case 0: /* format ended too soon */
889 * A scanset of the form
891 * is defined as `the digit 0, the digit 1,
892 * the character +, the character -', but
893 * the effect of a scanset such as
895 * is implementation defined. The V7 Unix
896 * scanf treats `a-z' as `the letters a through
897 * z', but treats `a-a' as `the letter a, the
898 * character -, and the letter a'.
900 * For compatibility, the `-' is not considerd
901 * to define a range if the character following
902 * it is either a close bracket (required by ANSI)
903 * or is not numerically greater than the character
904 * we just stored in the table (c).
908 || (loc
->__collate_load_error
? n
< c
:
909 __collate_range_cmp (n
, c
, loc
) < 0
913 break; /* resume the for(;;) */
916 /* fill in the range */
917 if (loc
->__collate_load_error
) {
922 for (i
= 0; i
< 256; i
++)
923 if ( __collate_range_cmp (c
, i
, loc
) < 0
924 && __collate_range_cmp (i
, n
, loc
) <= 0
928 #if 1 /* XXX another disgusting compatibility hack */
931 * Alas, the V7 Unix scanf also treats formats
932 * such as [a-c-e] as `the letters a through e'.
933 * This too is permitted by the standard....
945 case ']': /* end of scanset */
948 default: /* just another character */
956 #ifndef NO_FLOATING_POINT
958 * Maintain a per-thread parsefloat buffer, shared by __svfscanf_l and
961 #ifdef BUILDING_VARIANT
962 extern char *__parsefloat_buf(size_t s
);
963 #else /* !BUILDING_VARIANT */
964 __private_extern__
char *
965 __parsefloat_buf(size_t s
)
968 static pthread_key_t parsefloat_tsd_key
= (pthread_key_t
)-1;
969 static pthread_mutex_t parsefloat_tsd_lock
= PTHREAD_MUTEX_INITIALIZER
;
970 static size_t bsiz
= 0;
972 if (parsefloat_tsd_key
== (pthread_key_t
)-1) {
973 pthread_mutex_lock(&parsefloat_tsd_lock
);
974 if (parsefloat_tsd_key
== (pthread_key_t
)-1) {
975 parsefloat_tsd_key
= __LIBC_PTHREAD_KEY_PARSEFLOAT
;
976 pthread_key_init_np(parsefloat_tsd_key
, free
);
978 pthread_mutex_unlock(&parsefloat_tsd_lock
);
980 if ((b
= (char *)pthread_getspecific(parsefloat_tsd_key
)) == NULL
) {
981 bsiz
= s
> BUF
? s
: BUF
;
982 b
= (char *)malloc(bsiz
);
987 pthread_setspecific(parsefloat_tsd_key
, b
);
991 b
= (char *)reallocf(b
, s
);
992 pthread_setspecific(parsefloat_tsd_key
, b
);
1001 #endif /* BUILDING_VARIANT */
1004 parsefloat(FILE *fp
, char **buf
, size_t width
, locale_t loc
)
1007 int infnanpos
= 0, decptpos
= 0;
1009 S_START
, S_GOTSIGN
, S_INF
, S_NAN
, S_DONE
, S_MAYBEHEX
,
1010 S_DIGITS
, S_DECPT
, S_FRAC
, S_EXP
, S_EXPDIGITS
1013 const char *decpt
= localeconv_l(loc
)->decimal_point
;
1014 _Bool gotmantdig
= 0, ishex
= 0;
1019 s
= (width
== 0 ? BUF
: (width
+ 1));
1020 if ((b
= __parsefloat_buf(s
)) == NULL
) {
1026 * We set commit = p whenever the string we have read so far
1027 * constitutes a valid representation of a floating point
1028 * number by itself. At some point, the parse will complete
1029 * or fail, and we will ungetc() back to the last commit point.
1030 * To ensure that the file offset gets updated properly, it is
1031 * always necessary to read at least one character that doesn't
1032 * match; thus, we can't short-circuit "infinity" or "nan(...)".
1035 for (p
= b
; width
== 0 || p
< e
; ) {
1041 if (c
== '-' || c
== '+')
1065 if (infnanpos
> 6 ||
1066 (c
!= "nfinity"[infnanpos
] &&
1067 c
!= "NFINITY"[infnanpos
]))
1069 if (infnanpos
== 1 || infnanpos
== 6)
1070 commit
= p
; /* inf or infinity */
1074 switch (infnanpos
) {
1076 if (c
!= 'A' && c
!= 'a')
1080 if (c
!= 'N' && c
!= 'n')
1093 } else if (!isalnum_l(c
, loc
) && c
!= '_')
1103 if (c
== 'X' || c
== 'x') {
1106 } else { /* we saw a '0', but no 'x' */
1111 if ((ishex
&& isxdigit_l(c
, loc
)) || isdigit_l(c
, loc
)) {
1120 if (c
== decpt
[decptpos
]) {
1121 if (decpt
[++decptpos
] == '\0') {
1122 /* We read the complete decpt seq. */
1128 } else if (!decptpos
) {
1129 /* We didn't read any decpt characters. */
1134 * We read part of a multibyte decimal point,
1135 * but the rest is invalid, so bail.
1140 if (((c
== 'E' || c
== 'e') && !ishex
) ||
1141 ((c
== 'P' || c
== 'p') && ishex
)) {
1146 } else if ((ishex
&& isxdigit_l(c
, loc
)) || isdigit_l(c
, loc
)) {
1153 state
= S_EXPDIGITS
;
1154 if (c
== '-' || c
== '+')
1159 if (isdigit_l(c
, loc
))
1165 LIBC_ABORT("unknown state %d", state
);
1168 ssize_t diff
= (p
- b
);
1169 ssize_t com
= (commit
- b
);
1171 b
= __parsefloat_buf(s
);
1183 else if (__srefill(fp
))
1188 while (commit
< --p
)
1189 __ungetc(*(u_char
*)p
, fp
);
1192 return (commit
- b
);