1 /* $NetBSD: vfscanf.c,v 1.45 2013/05/17 12:55:57 joerg Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
39 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfscanf.c,v 1.41 2007/01/09 00:28:07 imp Exp $");
41 __RCSID("$NetBSD: vfscanf.c,v 1.45 2013/05/17 12:55:57 joerg Exp $");
43 #endif /* LIBC_SCCS and not lint */
45 #include "namespace.h"
57 #include "reentrant.h"
61 #include "setlocale_local.h"
64 * Provide an external name for vfscanf. Note, we don't use the normal
65 * namespace.h method; stdio routines explicitly use the internal name
69 __weak_alias(vfscanf
,__svfscanf
)
70 __weak_alias(vfscanf_l
,__svfscanf_l
)
73 #define BUF 513 /* Maximum length of numeric string. */
76 * Flags used during conversion.
78 #define LONG 0x0001 /* l: long or double */
79 #define LONGDBL 0x0002 /* L: long double */
80 #define SHORT 0x0004 /* h: short */
81 #define SUPPRESS 0x0008 /* *: suppress assignment */
82 #define POINTER 0x0010 /* p: void * (as hex) */
83 #define NOSKIP 0x0020 /* [ or c: do not skip blanks */
84 #define LONGLONG 0x0400 /* ll: long long (+ deprecated q: quad) */
85 #define INTMAXT 0x0800 /* j: intmax_t */
86 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
87 #define SIZET 0x2000 /* z: size_t */
88 #define SHORTSHORT 0x4000 /* hh: char */
89 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
92 * The following are used in integral conversions only:
93 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
95 #define SIGNOK 0x00040 /* +/- is (still) legal */
96 #define NDIGITS 0x00080 /* no digits detected */
97 #define PFXOK 0x00100 /* 0x prefix is (still) legal */
98 #define NZDIGITS 0x00200 /* no zero digits detected */
99 #define HAVESIGN 0x10000 /* sign detected */
104 #define CT_CHAR 0 /* %c conversion */
105 #define CT_CCL 1 /* %[...] conversion */
106 #define CT_STRING 2 /* %s conversion */
107 #define CT_INT 3 /* %[dioupxX] conversion */
108 #define CT_FLOAT 4 /* %[efgEFG] conversion */
110 static const u_char
*__sccl(char *, const u_char
*, locale_t
);
111 #ifndef NO_FLOATING_POINT
112 static size_t parsefloat(FILE *, char *, char *, locale_t
);
115 int __scanfdebug
= 0;
117 #define __collate_load_error /*CONSTCOND*/0
119 __collate_range_cmp(int c1
, int c2
, locale_t loc
)
121 static char s1
[2], s2
[2];
125 return strcoll_l(s1
, s2
, loc
);
130 * __svfscanf - MT-safe version
133 __svfscanf(FILE *fp
, char const *fmt0
, va_list ap
)
135 return __svfscanf_l(fp
, _current_locale(), fmt0
, ap
);
139 __svfscanf_l(FILE *fp
, locale_t loc
, char const *fmt0
, va_list ap
)
144 ret
= __svfscanf_unlocked_l(fp
, loc
, fmt0
, ap
);
149 #define SCANF_SKIP_SPACE() \
151 while ((fp->_r > 0 || __srefill(fp) == 0) && isspace_l(*fp->_p, loc)) \
152 nread++, fp->_r--, fp->_p++; \
153 } while (/*CONSTCOND*/ 0)
156 * __svfscanf_unlocked - non-MT-safe version of __svfscanf
159 __svfscanf_unlocked_l(FILE *fp
, locale_t loc
, const char *fmt0
, va_list ap
)
161 const u_char
*fmt
= (const u_char
*)fmt0
;
162 int c
; /* character from format, or conversion */
163 size_t width
; /* field width, or 0 */
164 char *p
; /* points into all kinds of strings */
165 size_t n
; /* handy size_t */
166 int flags
; /* flags as defined above */
167 char *p0
; /* saves original value of p when necessary */
168 int nassigned
; /* number of fields assigned */
169 int nconversions
; /* number of conversions */
170 size_t nread
; /* number of characters consumed from fp */
171 int base
; /* base argument to conversion function */
172 char ccltab
[256]; /* character class table for %[...] */
173 char buf
[BUF
]; /* buffer for numeric and mb conversions */
174 wchar_t *wcp
; /* handy wide-character pointer */
175 size_t nconv
; /* length of multibyte sequence converted */
176 static const mbstate_t initial
;
179 /* `basefix' is used to avoid `if' tests in the integer scanner */
180 static const short basefix
[17] =
181 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
183 _DIAGASSERT(fp
!= NULL
);
184 _DIAGASSERT(fmt0
!= NULL
);
186 _SET_ORIENTATION(fp
, -1);
193 c
= (unsigned char)*fmt
++;
196 if (isspace_l(c
, loc
)) {
197 while ((fp
->_r
> 0 || __srefill(fp
) == 0) &&
198 isspace_l(*fp
->_p
, loc
))
199 nread
++, fp
->_r
--, fp
->_p
++;
207 * switch on the format. continue if done;
208 * break once format type is derived.
215 if (fp
->_r
<= 0 && __srefill(fp
))
237 flags
|= LONGLONG
; /* not quite */
256 case '0': case '1': case '2': case '3': case '4':
257 case '5': case '6': case '7': case '8': case '9':
258 width
= width
* 10 + c
- '0';
288 flags
|= PFXOK
; /* enable 0x prefixing */
294 #ifndef NO_FLOATING_POINT
295 case 'A': case 'E': case 'F': case 'G':
296 case 'a': case 'e': case 'f': case 'g':
309 fmt
= __sccl(ccltab
, fmt
, loc
);
322 case 'p': /* pointer format is like hex */
323 flags
|= POINTER
| PFXOK
;
324 c
= CT_INT
; /* assumes sizeof(uintmax_t) */
325 flags
|= UNSIGNED
; /* >= sizeof(uintptr_t) */
331 if (flags
& SUPPRESS
) /* ??? */
333 if (flags
& SHORTSHORT
)
334 *va_arg(ap
, char *) = (char)nread
;
335 else if (flags
& SHORT
)
336 *va_arg(ap
, short *) = (short)nread
;
337 else if (flags
& LONG
)
338 *va_arg(ap
, long *) = nread
;
339 else if (flags
& LONGLONG
)
340 *va_arg(ap
, long long *) = nread
;
341 else if (flags
& INTMAXT
)
342 *va_arg(ap
, intmax_t *) = nread
;
343 else if (flags
& SIZET
)
344 *va_arg(ap
, size_t *) = nread
;
345 else if (flags
& PTRDIFFT
)
346 *va_arg(ap
, ptrdiff_t *) = nread
;
348 *va_arg(ap
, int *) = (int)nread
;
355 * Disgusting backwards compatibility hack. XXX
357 case '\0': /* compat */
362 * We have a conversion that requires input.
364 if (fp
->_r
<= 0 && __srefill(fp
))
368 * Consume leading white space, except for formats
369 * that suppress this.
371 if ((flags
& NOSKIP
) == 0) {
372 while (isspace_l(*fp
->_p
, loc
)) {
376 else if (__srefill(fp
))
380 * Note that there is at least one character in
381 * the buffer, so conversions that do not set NOSKIP
382 * ca no longer result in an input failure.
392 /* scan arbitrary characters (sets NOSKIP) */
396 if ((flags
& SUPPRESS
) == 0)
397 wcp
= va_arg(ap
, wchar_t *);
402 if (n
== MB_CUR_MAX_L(loc
)) {
403 fp
->_flags
|= __SERR
;
410 nconv
= mbrtowc_l(wcp
, buf
, n
, &mbs
,
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
) {
449 _DIAGASSERT(__type_fit(int,
451 fp
->_r
-= (int)width
;
458 size_t r
= fread(va_arg(ap
, char *), 1,
470 /* scan a (nonempty) character class (sets NOSKIP) */
472 width
= (size_t)~0; /* `infinity' */
473 /* take only those things in the class */
478 if ((flags
& SUPPRESS
) == 0)
479 wcp
= va_arg(ap
, wchar_t *);
485 if (n
== MB_CUR_MAX_L(loc
)) {
486 fp
->_flags
|= __SERR
;
493 nconv
= mbrtowc_l(wcp
, buf
, n
, &mbs
,
495 if (nconv
== (size_t)-1) {
496 fp
->_flags
|= __SERR
;
501 if (nconv
!= (size_t)-2) {
502 if (wctob_l(*wcp
, loc
) != EOF
&&
503 !ccltab
[wctob_l(*wcp
, loc
)]) {
513 if (!(flags
& SUPPRESS
))
518 if (fp
->_r
<= 0 && __srefill(fp
)) {
520 fp
->_flags
|= __SERR
;
527 fp
->_flags
|= __SERR
;
533 if (!(flags
& SUPPRESS
)) {
537 } else if (flags
& SUPPRESS
) {
539 while (ccltab
[*fp
->_p
]) {
540 n
++, fp
->_r
--, fp
->_p
++;
543 if (fp
->_r
<= 0 && __srefill(fp
)) {
552 p0
= p
= va_arg(ap
, char *);
553 while (ccltab
[*fp
->_p
]) {
558 if (fp
->_r
<= 0 && __srefill(fp
)) {
575 /* like CCL, but zero-length string OK, & no NOSKIP */
581 if ((flags
& SUPPRESS
) == 0)
582 wcp
= va_arg(ap
, wchar_t *);
586 while (!isspace_l(*fp
->_p
, loc
) && width
!= 0) {
587 if (n
== MB_CUR_MAX_L(loc
)) {
588 fp
->_flags
|= __SERR
;
595 nconv
= mbrtowc_l(wcp
, buf
, n
, &mbs
,
597 if (nconv
== (size_t)-1) {
598 fp
->_flags
|= __SERR
;
603 if (nconv
!= (size_t)-2) {
604 if (iswspace_l(*wcp
, loc
)) {
614 if (!(flags
& SUPPRESS
))
618 if (fp
->_r
<= 0 && __srefill(fp
)) {
620 fp
->_flags
|= __SERR
;
626 if (!(flags
& SUPPRESS
)) {
630 } else if (flags
& SUPPRESS
) {
632 while (!isspace_l(*fp
->_p
, loc
)) {
633 n
++, fp
->_r
--, fp
->_p
++;
636 if (fp
->_r
<= 0 && __srefill(fp
))
641 p0
= p
= va_arg(ap
, char *);
642 while (!isspace_l(*fp
->_p
, loc
)) {
647 if (fp
->_r
<= 0 && __srefill(fp
))
658 /* scan an integer as if by the conversion function */
660 if (width
== 0 || width
> sizeof(buf
) - 1)
661 width
= sizeof(buf
) - 1;
663 /* size_t is unsigned, hence this optimisation */
664 if (--width
> sizeof(buf
) - 2)
665 width
= sizeof(buf
) - 2;
668 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
669 for (p
= buf
; width
; width
--) {
672 * Switch on the character; `goto ok'
673 * if we accept it as a part of number.
678 * The digit 0 is always legal, but is
679 * special. For %i conversions, if no
680 * digits (zero or nonzero) have been
681 * scanned (only signs), we will have
682 * base==0. In that case, we should set
683 * it to 8 and enable 0x prefixing.
684 * Also, if we have not scanned zero digits
685 * before this, do not turn off prefixing
686 * (someone else will turn it off if we
687 * have scanned any nonzero digits).
694 if (flags
& NZDIGITS
)
695 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
697 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
700 /* 1 through 7 always legal */
701 case '1': case '2': case '3':
702 case '4': case '5': case '6': case '7':
703 base
= basefix
[base
];
704 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
707 /* digits 8 and 9 ok iff decimal or hex */
709 base
= basefix
[base
];
711 break; /* not legal here */
712 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
715 /* letters ok iff hex */
716 case 'A': case 'B': case 'C':
717 case 'D': case 'E': case 'F':
718 case 'a': case 'b': case 'c':
719 case 'd': case 'e': case 'f':
720 /* no need to fix base here */
722 break; /* not legal here */
723 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
726 /* sign ok only as first character */
728 if (flags
& SIGNOK
) {
736 * x ok iff flag still set & 2nd char (or
737 * 3rd char if we have a sign).
740 if (flags
& PFXOK
&& p
==
741 buf
+ 1 + !!(flags
& HAVESIGN
)) {
742 base
= 16; /* if %i */
750 * If we got here, c is not a legal character
751 * for a number. Stop accumulating digits.
756 * c is legal: store it and look at the next.
761 else if (__srefill(fp
))
765 * If we had only a sign, it is no good; push
766 * back the sign. If the number ends in `x',
767 * it was [sign] '0' 'x', so push back the x
768 * and treat it as [sign] '0'.
770 if (flags
& NDIGITS
) {
772 (void)ungetc(*(u_char
*)--p
, fp
);
775 c
= ((u_char
*)p
)[-1];
776 if (c
== 'x' || c
== 'X') {
780 if ((flags
& SUPPRESS
) == 0) {
784 if ((flags
& UNSIGNED
) == 0)
785 res
= strtoimax_l(buf
, (char **)NULL
, base
,
788 res
= strtoumax_l(buf
, (char **)NULL
, base
,
791 *va_arg(ap
, void **) =
792 (void *)(uintptr_t)res
;
793 else if (flags
& SHORTSHORT
)
794 *va_arg(ap
, char *) = (char)res
;
795 else if (flags
& SHORT
)
796 *va_arg(ap
, short *) = (short)res
;
797 else if (flags
& LONG
)
798 *va_arg(ap
, long *) = (long)res
;
799 else if (flags
& LONGLONG
)
800 *va_arg(ap
, long long *) = res
;
801 else if (flags
& INTMAXT
)
802 *va_arg(ap
, intmax_t *) = res
;
803 else if (flags
& PTRDIFFT
)
804 *va_arg(ap
, ptrdiff_t *) =
806 else if (flags
& SIZET
)
807 *va_arg(ap
, size_t *) = (size_t)res
;
809 *va_arg(ap
, int *) = (int)res
;
816 #ifndef NO_FLOATING_POINT
818 /* scan a floating point number as if by strtod */
819 if (width
== 0 || width
> sizeof(buf
) - 1)
820 width
= sizeof(buf
) - 1;
821 if ((width
= parsefloat(fp
, buf
, buf
+ width
, loc
)) == 0)
823 if ((flags
& SUPPRESS
) == 0) {
824 if (flags
& LONGDBL
) {
825 long double res
= strtold_l(buf
, &p
,
827 *va_arg(ap
, long double *) = res
;
828 } else if (flags
& LONG
) {
829 double res
= strtod_l(buf
, &p
, loc
);
830 *va_arg(ap
, double *) = res
;
832 float res
= strtof_l(buf
, &p
, loc
);
833 *va_arg(ap
, float *) = res
;
835 if (__scanfdebug
&& (size_t)(p
- buf
) != width
)
842 #endif /* !NO_FLOATING_POINT */
846 return nconversions
!= 0 ? nassigned
: EOF
;
852 * Fill in the given table from the scanset at the given format
853 * (just after `['). Return a pointer to the character past the
854 * closing `]'. The table has a 1 wherever characters should be
855 * considered part of the scanset.
857 static const u_char
*
858 __sccl(char *tab
, const u_char
*fmt
, locale_t loc
)
862 _DIAGASSERT(tab
!= NULL
);
863 _DIAGASSERT(fmt
!= NULL
);
864 /* first `clear' the whole table */
865 c
= *fmt
++; /* first char hat => negated scanset */
867 v
= 1; /* default => accept */
868 c
= *fmt
++; /* get new first char */
870 v
= 0; /* default => reject */
872 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
873 (void)memset(tab
, v
, 256);
876 return fmt
- 1;/* format ended before closing ] */
879 * Now set the entries corresponding to the actual scanset
880 * to the opposite of the above.
882 * The first character may be ']' (or '-') without being special;
883 * the last character may be '-'.
887 tab
[c
] = v
; /* take character c */
889 n
= *fmt
++; /* and examine the next */
892 case 0: /* format ended too soon */
897 * A scanset of the form
899 * is defined as `the digit 0, the digit 1,
900 * the character +, the character -', but
901 * the effect of a scanset such as
903 * is implementation defined. The V7 Unix
904 * scanf treats `a-z' as `the letters a through
905 * z', but treats `a-a' as `the letter a, the
906 * character -, and the letter a'.
908 * For compatibility, the `-' is not considerd
909 * to define a range if the character following
910 * it is either a close bracket (required by ANSI)
911 * or is not numerically greater than the character
912 * we just stored in the table (c).
915 if (n
== ']' || (__collate_load_error
? n
< c
:
916 __collate_range_cmp(n
, c
, loc
) < 0)) {
918 break; /* resume the for(;;) */
921 /* fill in the range */
922 if (__collate_load_error
) {
927 for (i
= 0; i
< 256; i
++)
928 if (__collate_range_cmp(c
, i
, loc
) < 0 &&
929 __collate_range_cmp(i
, n
, loc
) <= 0)
932 #if 1 /* XXX another disgusting compatibility hack */
935 * Alas, the V7 Unix scanf also treats formats
936 * such as [a-c-e] as `the letters a through e'.
937 * This too is permitted by the standard....
948 case ']': /* end of scanset */
951 default: /* just another character */
959 #ifndef NO_FLOATING_POINT
961 parsefloat(FILE *fp
, char *buf
, char *end
, locale_t loc
)
966 S_START
, S_GOTSIGN
, S_INF
, S_NAN
, S_MAYBEHEX
,
967 S_DIGITS
, S_FRAC
, S_EXP
, S_EXPDIGITS
970 char decpt
= *localeconv_l(loc
)->decimal_point
;
971 _Bool gotmantdig
= 0, ishex
= 0;
974 * We set commit = p whenever the string we have read so far
975 * constitutes a valid representation of a floating point
976 * number by itself. At some point, the parse will complete
977 * or fail, and we will ungetc() back to the last commit point.
978 * To ensure that the file offset gets updated properly, it is
979 * always necessary to read at least one character that doesn't
980 * match; thus, we can't short-circuit "infinity" or "nan(...)".
983 for (p
= buf
; p
< end
; ) {
989 if (c
== '-' || c
== '+')
1013 if (infnanpos
> 6 ||
1014 (c
!= "nfinity"[infnanpos
] &&
1015 c
!= "NFINITY"[infnanpos
]))
1017 if (infnanpos
== 1 || infnanpos
== 6)
1018 commit
= p
; /* inf or infinity */
1022 switch (infnanpos
) {
1023 case -1: /* XXX kludge to deal with nan(...) */
1026 if (c
!= 'A' && c
!= 'a')
1030 if (c
!= 'N' && c
!= 'n')
1043 } else if (!isalnum_l(c
, loc
) && c
!= '_')
1051 if (c
== 'X' || c
== 'x') {
1054 } else { /* we saw a '0', but no 'x' */
1059 if ((ishex
&& isxdigit_l(c
, loc
)) || isdigit_l(c
, loc
))
1070 if (((c
== 'E' || c
== 'e') && !ishex
) ||
1071 ((c
== 'P' || c
== 'p') && ishex
)) {
1076 } else if ((ishex
&& isxdigit_l(c
, loc
)) || isdigit_l(c
, loc
)) {
1083 state
= S_EXPDIGITS
;
1084 if (c
== '-' || c
== '+')
1089 if (isdigit_l(c
, loc
))
1100 else if (__srefill(fp
))
1105 while (commit
< --p
)
1106 (void)ungetc(*(u_char
*)p
, fp
);
1108 return commit
- buf
;