1 /* $NetBSD: vfscanf.c,v 1.43 2012/03/15 18:22:30 christos 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.43 2012/03/15 18:22:30 christos Exp $");
43 #endif /* LIBC_SCCS and not lint */
45 #include "namespace.h"
57 #include "reentrant.h"
60 #ifndef NO_FLOATING_POINT
65 * Provide an external name for vfscanf. Note, we don't use the normal
66 * namespace.h method; stdio routines explicitly use the internal name
70 __weak_alias(vfscanf
,__svfscanf
)
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
*);
111 #ifndef NO_FLOATING_POINT
112 static size_t parsefloat(FILE *, char *, char *);
115 int __scanfdebug
= 0;
117 #define __collate_load_error /*CONSTCOND*/0
119 __collate_range_cmp(int c1
, int c2
)
121 static char s1
[2], s2
[2];
125 return strcoll(s1
, s2
);
130 * __svfscanf - MT-safe version
133 __svfscanf(FILE *fp
, char const *fmt0
, va_list ap
)
138 ret
= __svfscanf_unlocked(fp
, fmt0
, ap
);
143 #define SCANF_SKIP_SPACE() \
145 while ((fp->_r > 0 || __srefill(fp) == 0) && isspace(*fp->_p)) \
146 nread++, fp->_r--, fp->_p++; \
147 } while (/*CONSTCOND*/ 0)
150 * __svfscanf_unlocked - non-MT-safe version of __svfscanf
153 __svfscanf_unlocked(FILE *fp
, const char *fmt0
, va_list ap
)
155 const u_char
*fmt
= (const u_char
*)fmt0
;
156 int c
; /* character from format, or conversion */
157 size_t width
; /* field width, or 0 */
158 char *p
; /* points into all kinds of strings */
159 size_t n
; /* handy size_t */
160 int flags
; /* flags as defined above */
161 char *p0
; /* saves original value of p when necessary */
162 int nassigned
; /* number of fields assigned */
163 int nconversions
; /* number of conversions */
164 size_t nread
; /* number of characters consumed from fp */
165 int base
; /* base argument to conversion function */
166 char ccltab
[256]; /* character class table for %[...] */
167 char buf
[BUF
]; /* buffer for numeric and mb conversions */
168 wchar_t *wcp
; /* handy wide-character pointer */
169 size_t nconv
; /* length of multibyte sequence converted */
170 static const mbstate_t initial
;
173 /* `basefix' is used to avoid `if' tests in the integer scanner */
174 static const short basefix
[17] =
175 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
177 _DIAGASSERT(fp
!= NULL
);
178 _DIAGASSERT(fmt0
!= NULL
);
180 _SET_ORIENTATION(fp
, -1);
187 c
= (unsigned char)*fmt
++;
191 while ((fp
->_r
> 0 || __srefill(fp
) == 0) &&
193 nread
++, fp
->_r
--, fp
->_p
++;
201 * switch on the format. continue if done;
202 * break once format type is derived.
209 if (fp
->_r
<= 0 && __srefill(fp
))
231 flags
|= LONGLONG
; /* not quite */
250 case '0': case '1': case '2': case '3': case '4':
251 case '5': case '6': case '7': case '8': case '9':
252 width
= width
* 10 + c
- '0';
282 flags
|= PFXOK
; /* enable 0x prefixing */
288 #ifndef NO_FLOATING_POINT
289 case 'A': case 'E': case 'F': case 'G':
290 case 'a': case 'e': case 'f': case 'g':
303 fmt
= __sccl(ccltab
, fmt
);
316 case 'p': /* pointer format is like hex */
317 flags
|= POINTER
| PFXOK
;
318 c
= CT_INT
; /* assumes sizeof(uintmax_t) */
319 flags
|= UNSIGNED
; /* >= sizeof(uintptr_t) */
325 if (flags
& SUPPRESS
) /* ??? */
327 if (flags
& SHORTSHORT
)
328 *va_arg(ap
, char *) = (char)nread
;
329 else if (flags
& SHORT
)
330 *va_arg(ap
, short *) = (short)nread
;
331 else if (flags
& LONG
)
332 *va_arg(ap
, long *) = nread
;
333 else if (flags
& LONGLONG
)
334 *va_arg(ap
, long long *) = nread
;
335 else if (flags
& INTMAXT
)
336 *va_arg(ap
, intmax_t *) = nread
;
337 else if (flags
& SIZET
)
338 *va_arg(ap
, size_t *) = nread
;
339 else if (flags
& PTRDIFFT
)
340 *va_arg(ap
, ptrdiff_t *) = nread
;
342 *va_arg(ap
, int *) = (int)nread
;
349 * Disgusting backwards compatibility hack. XXX
351 case '\0': /* compat */
356 * We have a conversion that requires input.
358 if (fp
->_r
<= 0 && __srefill(fp
))
362 * Consume leading white space, except for formats
363 * that suppress this.
365 if ((flags
& NOSKIP
) == 0) {
366 while (isspace(*fp
->_p
)) {
370 else if (__srefill(fp
))
374 * Note that there is at least one character in
375 * the buffer, so conversions that do not set NOSKIP
376 * ca no longer result in an input failure.
386 /* scan arbitrary characters (sets NOSKIP) */
390 if ((flags
& SUPPRESS
) == 0)
391 wcp
= va_arg(ap
, wchar_t *);
396 if (n
== MB_CUR_MAX
) {
397 fp
->_flags
|= __SERR
;
404 nconv
= mbrtowc(wcp
, buf
, n
, &mbs
);
405 if (nconv
== (size_t)-1) {
406 fp
->_flags
|= __SERR
;
409 if (nconv
== 0 && !(flags
& SUPPRESS
))
411 if (nconv
!= (size_t)-2) {
414 if (!(flags
& SUPPRESS
))
418 if (fp
->_r
<= 0 && __srefill(fp
)) {
420 fp
->_flags
|= __SERR
;
426 if (!(flags
& SUPPRESS
))
428 } else if (flags
& SUPPRESS
) {
431 if ((n
= fp
->_r
) < width
) {
442 _DIAGASSERT(__type_fit(int,
444 fp
->_r
-= (int)width
;
451 size_t r
= fread(va_arg(ap
, char *), 1,
463 /* scan a (nonempty) character class (sets NOSKIP) */
465 width
= (size_t)~0; /* `infinity' */
466 /* take only those things in the class */
471 if ((flags
& SUPPRESS
) == 0)
472 wcp
= va_arg(ap
, wchar_t *);
478 if (n
== MB_CUR_MAX
) {
479 fp
->_flags
|= __SERR
;
486 nconv
= mbrtowc(wcp
, buf
, n
, &mbs
);
487 if (nconv
== (size_t)-1) {
488 fp
->_flags
|= __SERR
;
493 if (nconv
!= (size_t)-2) {
494 if (wctob(*wcp
) != EOF
&&
495 !ccltab
[wctob(*wcp
)]) {
505 if (!(flags
& SUPPRESS
))
510 if (fp
->_r
<= 0 && __srefill(fp
)) {
512 fp
->_flags
|= __SERR
;
519 fp
->_flags
|= __SERR
;
525 if (!(flags
& SUPPRESS
)) {
529 } else if (flags
& SUPPRESS
) {
531 while (ccltab
[*fp
->_p
]) {
532 n
++, fp
->_r
--, fp
->_p
++;
535 if (fp
->_r
<= 0 && __srefill(fp
)) {
544 p0
= p
= va_arg(ap
, char *);
545 while (ccltab
[*fp
->_p
]) {
550 if (fp
->_r
<= 0 && __srefill(fp
)) {
567 /* like CCL, but zero-length string OK, & no NOSKIP */
573 if ((flags
& SUPPRESS
) == 0)
574 wcp
= va_arg(ap
, wchar_t *);
578 while (!isspace(*fp
->_p
) && width
!= 0) {
579 if (n
== MB_CUR_MAX
) {
580 fp
->_flags
|= __SERR
;
587 nconv
= mbrtowc(wcp
, buf
, n
, &mbs
);
588 if (nconv
== (size_t)-1) {
589 fp
->_flags
|= __SERR
;
594 if (nconv
!= (size_t)-2) {
595 if (iswspace(*wcp
)) {
605 if (!(flags
& SUPPRESS
))
609 if (fp
->_r
<= 0 && __srefill(fp
)) {
611 fp
->_flags
|= __SERR
;
617 if (!(flags
& SUPPRESS
)) {
621 } else if (flags
& SUPPRESS
) {
623 while (!isspace(*fp
->_p
)) {
624 n
++, fp
->_r
--, fp
->_p
++;
627 if (fp
->_r
<= 0 && __srefill(fp
))
632 p0
= p
= va_arg(ap
, char *);
633 while (!isspace(*fp
->_p
)) {
638 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') {
771 if ((flags
& SUPPRESS
) == 0) {
775 if ((flags
& UNSIGNED
) == 0)
776 res
= strtoimax(buf
, (char **)NULL
, base
);
778 res
= strtoumax(buf
, (char **)NULL
, base
);
780 *va_arg(ap
, void **) =
781 (void *)(uintptr_t)res
;
782 else if (flags
& SHORTSHORT
)
783 *va_arg(ap
, char *) = (char)res
;
784 else if (flags
& SHORT
)
785 *va_arg(ap
, short *) = (short)res
;
786 else if (flags
& LONG
)
787 *va_arg(ap
, long *) = (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 *) =
795 else if (flags
& SIZET
)
796 *va_arg(ap
, size_t *) = (size_t)res
;
798 *va_arg(ap
, int *) = (int)res
;
805 #ifndef NO_FLOATING_POINT
807 /* scan a floating point number as if by strtod */
808 if (width
== 0 || width
> sizeof(buf
) - 1)
809 width
= sizeof(buf
) - 1;
810 if ((width
= parsefloat(fp
, buf
, buf
+ width
)) == 0)
812 if ((flags
& SUPPRESS
) == 0) {
813 if (flags
& LONGDBL
) {
814 long double res
= strtold(buf
, &p
);
815 *va_arg(ap
, long double *) = res
;
816 } else if (flags
& LONG
) {
817 double res
= strtod(buf
, &p
);
818 *va_arg(ap
, double *) = res
;
820 float res
= strtof(buf
, &p
);
821 *va_arg(ap
, float *) = res
;
823 if (__scanfdebug
&& (size_t)(p
- buf
) != width
)
830 #endif /* !NO_FLOATING_POINT */
834 return nconversions
!= 0 ? nassigned
: EOF
;
840 * Fill in the given table from the scanset at the given format
841 * (just after `['). Return a pointer to the character past the
842 * closing `]'. The table has a 1 wherever characters should be
843 * considered part of the scanset.
845 static const u_char
*
846 __sccl(char *tab
, const u_char
*fmt
)
850 _DIAGASSERT(tab
!= NULL
);
851 _DIAGASSERT(fmt
!= NULL
);
852 /* first `clear' the whole table */
853 c
= *fmt
++; /* first char hat => negated scanset */
855 v
= 1; /* default => accept */
856 c
= *fmt
++; /* get new first char */
858 v
= 0; /* default => reject */
860 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
861 (void)memset(tab
, v
, 256);
864 return fmt
- 1;/* format ended before closing ] */
867 * Now set the entries corresponding to the actual scanset
868 * to the opposite of the above.
870 * The first character may be ']' (or '-') without being special;
871 * the last character may be '-'.
875 tab
[c
] = v
; /* take character c */
877 n
= *fmt
++; /* and examine the next */
880 case 0: /* format ended too soon */
885 * A scanset of the form
887 * is defined as `the digit 0, the digit 1,
888 * the character +, the character -', but
889 * the effect of a scanset such as
891 * is implementation defined. The V7 Unix
892 * scanf treats `a-z' as `the letters a through
893 * z', but treats `a-a' as `the letter a, the
894 * character -, and the letter a'.
896 * For compatibility, the `-' is not considerd
897 * to define a range if the character following
898 * it is either a close bracket (required by ANSI)
899 * or is not numerically greater than the character
900 * we just stored in the table (c).
903 if (n
== ']' || (__collate_load_error
? n
< c
:
904 __collate_range_cmp(n
, c
) < 0)) {
906 break; /* resume the for(;;) */
909 /* fill in the range */
910 if (__collate_load_error
) {
915 for (i
= 0; i
< 256; i
++)
916 if (__collate_range_cmp(c
, i
) < 0 &&
917 __collate_range_cmp(i
, n
) <= 0)
920 #if 1 /* XXX another disgusting compatibility hack */
923 * Alas, the V7 Unix scanf also treats formats
924 * such as [a-c-e] as `the letters a through e'.
925 * This too is permitted by the standard....
936 case ']': /* end of scanset */
939 default: /* just another character */
947 #ifndef NO_FLOATING_POINT
949 parsefloat(FILE *fp
, char *buf
, char *end
)
954 S_START
, S_GOTSIGN
, S_INF
, S_NAN
, S_MAYBEHEX
,
955 S_DIGITS
, S_FRAC
, S_EXP
, S_EXPDIGITS
958 char decpt
= *localeconv()->decimal_point
;
959 _Bool gotmantdig
= 0, ishex
= 0;
962 * We set commit = p whenever the string we have read so far
963 * constitutes a valid representation of a floating point
964 * number by itself. At some point, the parse will complete
965 * or fail, and we will ungetc() back to the last commit point.
966 * To ensure that the file offset gets updated properly, it is
967 * always necessary to read at least one character that doesn't
968 * match; thus, we can't short-circuit "infinity" or "nan(...)".
971 for (p
= buf
; p
< end
; ) {
977 if (c
== '-' || c
== '+')
1001 if (infnanpos
> 6 ||
1002 (c
!= "nfinity"[infnanpos
] &&
1003 c
!= "NFINITY"[infnanpos
]))
1005 if (infnanpos
== 1 || infnanpos
== 6)
1006 commit
= p
; /* inf or infinity */
1010 switch (infnanpos
) {
1011 case -1: /* XXX kludge to deal with nan(...) */
1014 if (c
!= 'A' && c
!= 'a')
1018 if (c
!= 'N' && c
!= 'n')
1031 } else if (!isalnum(c
) && c
!= '_')
1039 if (c
== 'X' || c
== 'x') {
1042 } else { /* we saw a '0', but no 'x' */
1047 if ((ishex
&& isxdigit(c
)) || isdigit(c
))
1058 if (((c
== 'E' || c
== 'e') && !ishex
) ||
1059 ((c
== 'P' || c
== 'p') && ishex
)) {
1064 } else if ((ishex
&& isxdigit(c
)) || isdigit(c
)) {
1071 state
= S_EXPDIGITS
;
1072 if (c
== '-' || c
== '+')
1088 else if (__srefill(fp
))
1093 while (commit
< --p
)
1094 (void)ungetc(*(u_char
*)p
, fp
);
1096 return commit
- buf
;