]>
git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/vfscanf.c
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: src/lib/libc/stdio/vfscanf.c,v 1.37 2004/05/02 10:55:05 das Exp $");
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 static int parsefloat(FILE *, char *, char *);
103 int __scanfdebug
= 0;
105 __weak_reference(__vfscanf
, vfscanf
);
108 * __vfscanf - MT-safe version
111 __vfscanf(FILE *fp
, char const *fmt0
, va_list ap
)
116 ret
= __svfscanf(fp
, fmt0
, ap
);
122 * __svfscanf - non-MT-safe version of __vfscanf
125 __svfscanf(FILE *fp
, const char *fmt0
, va_list ap
)
127 const u_char
*fmt
= (const u_char
*)fmt0
;
128 int c
; /* character from format, or conversion */
129 size_t width
; /* field width, or 0 */
130 char *p
; /* points into all kinds of strings */
131 int n
; /* handy integer */
132 int flags
; /* flags as defined above */
133 char *p0
; /* saves original value of p when necessary */
134 int nassigned
; /* number of fields assigned */
135 int nconversions
; /* number of conversions */
136 int nread
; /* number of characters consumed from fp */
137 int base
; /* base argument to conversion function */
138 char ccltab
[256]; /* character class table for %[...] */
139 char buf
[BUF
]; /* buffer for numeric and mb conversions */
140 wchar_t *wcp
; /* handy wide character pointer */
141 wchar_t *wcp0
; /* saves original value of wcp */
142 size_t nconv
; /* length of multibyte sequence converted */
143 static const mbstate_t initial
;
146 /* `basefix' is used to avoid `if' tests in the integer scanner */
147 static short basefix
[17] =
148 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
160 while ((fp
->_r
> 0 || __srefill(fp
) == 0) && isspace(*fp
->_p
))
161 nread
++, fp
->_r
--, fp
->_p
++;
169 * switch on the format. continue if done;
170 * break once format type is derived.
176 if (fp
->_r
<= 0 && __srefill(fp
))
198 flags
|= LONGLONG
; /* not quite */
217 case '0': case '1': case '2': case '3': case '4':
218 case '5': case '6': case '7': case '8': case '9':
219 width
= width
* 10 + c
- '0';
249 flags
|= PFXOK
; /* enable 0x prefixing */
255 #ifndef NO_FLOATING_POINT
256 case 'A': case 'E': case 'F': case 'G':
257 case 'a': case 'e': case 'f': case 'g':
270 fmt
= __sccl(ccltab
, fmt
);
283 case 'p': /* pointer format is like hex */
284 flags
|= POINTER
| PFXOK
;
285 c
= CT_INT
; /* assumes sizeof(uintmax_t) */
286 flags
|= UNSIGNED
; /* >= sizeof(uintptr_t) */
292 if (flags
& SUPPRESS
) /* ??? */
294 if (flags
& SHORTSHORT
)
295 *va_arg(ap
, char *) = nread
;
296 else if (flags
& SHORT
)
297 *va_arg(ap
, short *) = nread
;
298 else if (flags
& LONG
)
299 *va_arg(ap
, long *) = nread
;
300 else if (flags
& LONGLONG
)
301 *va_arg(ap
, long long *) = nread
;
302 else if (flags
& INTMAXT
)
303 *va_arg(ap
, intmax_t *) = nread
;
304 else if (flags
& SIZET
)
305 *va_arg(ap
, size_t *) = nread
;
306 else if (flags
& PTRDIFFT
)
307 *va_arg(ap
, ptrdiff_t *) = nread
;
309 *va_arg(ap
, int *) = nread
;
316 * Disgusting backwards compatibility hack. XXX
318 case '\0': /* compat */
323 * We have a conversion that requires input.
325 if (fp
->_r
<= 0 && __srefill(fp
))
329 * Consume leading white space, except for formats
330 * that suppress this.
332 if ((flags
& NOSKIP
) == 0) {
333 while (isspace(*fp
->_p
)) {
337 else if (__srefill(fp
))
341 * Note that there is at least one character in
342 * the buffer, so conversions that do not set NOSKIP
343 * ca no longer result in an input failure.
353 /* scan arbitrary characters (sets NOSKIP) */
357 if ((flags
& SUPPRESS
) == 0)
358 wcp
= va_arg(ap
, wchar_t *);
363 if (n
== MB_CUR_MAX
) {
364 fp
->_flags
|= __SERR
;
371 nconv
= mbrtowc(wcp
, buf
, n
, &mbs
);
372 if (nconv
== (size_t)-1) {
373 fp
->_flags
|= __SERR
;
376 if (nconv
== 0 && !(flags
& SUPPRESS
))
378 if (nconv
!= (size_t)-2) {
381 if (!(flags
& SUPPRESS
))
385 if (fp
->_r
<= 0 && __srefill(fp
)) {
387 fp
->_flags
|= __SERR
;
393 if (!(flags
& SUPPRESS
))
395 } else if (flags
& SUPPRESS
) {
398 if ((n
= fp
->_r
) < width
) {
416 size_t r
= fread((void *)va_arg(ap
, char *), 1,
428 /* scan a (nonempty) character class (sets NOSKIP) */
430 width
= (size_t)~0; /* `infinity' */
431 /* take only those things in the class */
436 if ((flags
& SUPPRESS
) == 0)
437 wcp
= wcp0
= va_arg(ap
, wchar_t *);
443 if (n
== MB_CUR_MAX
) {
444 fp
->_flags
|= __SERR
;
451 nconv
= mbrtowc(wcp
, buf
, n
, &mbs
);
452 if (nconv
== (size_t)-1) {
453 fp
->_flags
|= __SERR
;
458 if (nconv
!= (size_t)-2) {
459 if (wctob(*wcp
) != EOF
&&
460 !ccltab
[wctob(*wcp
)]) {
470 if (!(flags
& SUPPRESS
))
475 if (fp
->_r
<= 0 && __srefill(fp
)) {
477 fp
->_flags
|= __SERR
;
484 fp
->_flags
|= __SERR
;
490 if (!(flags
& SUPPRESS
)) {
494 } else if (flags
& SUPPRESS
) {
496 while (ccltab
[*fp
->_p
]) {
497 n
++, fp
->_r
--, fp
->_p
++;
500 if (fp
->_r
<= 0 && __srefill(fp
)) {
509 p0
= p
= va_arg(ap
, char *);
510 while (ccltab
[*fp
->_p
]) {
515 if (fp
->_r
<= 0 && __srefill(fp
)) {
532 /* like CCL, but zero-length string OK, & no NOSKIP */
538 if ((flags
& SUPPRESS
) == 0)
539 wcp
= va_arg(ap
, wchar_t *);
543 while (!isspace(*fp
->_p
) && width
!= 0) {
544 if (n
== MB_CUR_MAX
) {
545 fp
->_flags
|= __SERR
;
552 nconv
= mbrtowc(wcp
, buf
, n
, &mbs
);
553 if (nconv
== (size_t)-1) {
554 fp
->_flags
|= __SERR
;
559 if (nconv
!= (size_t)-2) {
560 if (iswspace(*wcp
)) {
570 if (!(flags
& SUPPRESS
))
574 if (fp
->_r
<= 0 && __srefill(fp
)) {
576 fp
->_flags
|= __SERR
;
582 if (!(flags
& SUPPRESS
)) {
586 } else if (flags
& SUPPRESS
) {
588 while (!isspace(*fp
->_p
)) {
589 n
++, fp
->_r
--, fp
->_p
++;
592 if (fp
->_r
<= 0 && __srefill(fp
))
597 p0
= p
= va_arg(ap
, char *);
598 while (!isspace(*fp
->_p
)) {
603 if (fp
->_r
<= 0 && __srefill(fp
))
614 /* scan an integer as if by the conversion function */
616 if (width
== 0 || width
> sizeof(buf
) - 1)
617 width
= sizeof(buf
) - 1;
619 /* size_t is unsigned, hence this optimisation */
620 if (--width
> sizeof(buf
) - 2)
621 width
= sizeof(buf
) - 2;
624 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
625 for (p
= buf
; width
; width
--) {
628 * Switch on the character; `goto ok'
629 * if we accept it as a part of number.
634 * The digit 0 is always legal, but is
635 * special. For %i conversions, if no
636 * digits (zero or nonzero) have been
637 * scanned (only signs), we will have
638 * base==0. In that case, we should set
639 * it to 8 and enable 0x prefixing.
640 * Also, if we have not scanned zero digits
641 * before this, do not turn off prefixing
642 * (someone else will turn it off if we
643 * have scanned any nonzero digits).
650 if (flags
& NZDIGITS
)
651 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
653 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
656 /* 1 through 7 always legal */
657 case '1': case '2': case '3':
658 case '4': case '5': case '6': case '7':
659 base
= basefix
[base
];
660 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
663 /* digits 8 and 9 ok iff decimal or hex */
665 base
= basefix
[base
];
667 break; /* not legal here */
668 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
671 /* letters ok iff hex */
672 case 'A': case 'B': case 'C':
673 case 'D': case 'E': case 'F':
674 case 'a': case 'b': case 'c':
675 case 'd': case 'e': case 'f':
676 /* no need to fix base here */
678 break; /* not legal here */
679 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
682 /* sign ok only as first character */
684 if (flags
& SIGNOK
) {
692 * x ok iff flag still set & 2nd char (or
693 * 3rd char if we have a sign).
696 if (flags
& PFXOK
&& p
==
697 buf
+ 1 + !!(flags
& HAVESIGN
)) {
698 base
= 16; /* if %i */
706 * If we got here, c is not a legal character
707 * for a number. Stop accumulating digits.
712 * c is legal: store it and look at the next.
717 else if (__srefill(fp
))
721 * If we had only a sign, it is no good; push
722 * back the sign. If the number ends in `x',
723 * it was [sign] '0' 'x', so push back the x
724 * and treat it as [sign] '0'.
726 if (flags
& NDIGITS
) {
728 (void) __ungetc(*(u_char
*)--p
, fp
);
731 c
= ((u_char
*)p
)[-1];
732 if (c
== 'x' || c
== 'X') {
734 (void) __ungetc(c
, fp
);
736 if ((flags
& SUPPRESS
) == 0) {
740 if ((flags
& UNSIGNED
) == 0)
741 res
= strtoimax(buf
, (char **)NULL
, base
);
743 res
= strtoumax(buf
, (char **)NULL
, base
);
745 *va_arg(ap
, void **) =
746 (void *)(uintptr_t)res
;
747 else if (flags
& SHORTSHORT
)
748 *va_arg(ap
, char *) = res
;
749 else if (flags
& SHORT
)
750 *va_arg(ap
, short *) = res
;
751 else if (flags
& LONG
)
752 *va_arg(ap
, long *) = res
;
753 else if (flags
& LONGLONG
)
754 *va_arg(ap
, long long *) = res
;
755 else if (flags
& INTMAXT
)
756 *va_arg(ap
, intmax_t *) = res
;
757 else if (flags
& PTRDIFFT
)
758 *va_arg(ap
, ptrdiff_t *) = res
;
759 else if (flags
& SIZET
)
760 *va_arg(ap
, size_t *) = res
;
762 *va_arg(ap
, int *) = res
;
769 #ifndef NO_FLOATING_POINT
771 /* scan a floating point number as if by strtod */
772 if (width
== 0 || width
> sizeof(buf
) - 1)
773 width
= sizeof(buf
) - 1;
774 if ((width
= parsefloat(fp
, buf
, buf
+ width
)) == 0)
776 if ((flags
& SUPPRESS
) == 0) {
777 if (flags
& LONGDBL
) {
778 long double res
= strtold(buf
, &p
);
779 *va_arg(ap
, long double *) = res
;
780 } else if (flags
& LONG
) {
781 double res
= strtod(buf
, &p
);
782 *va_arg(ap
, double *) = res
;
784 float res
= strtof(buf
, &p
);
785 *va_arg(ap
, float *) = res
;
787 if (__scanfdebug
&& p
- buf
!= width
)
794 #endif /* !NO_FLOATING_POINT */
798 return (nconversions
!= 0 ? nassigned
: EOF
);
804 * Fill in the given table from the scanset at the given format
805 * (just after `['). Return a pointer to the character past the
806 * closing `]'. The table has a 1 wherever characters should be
807 * considered part of the scanset.
809 static const u_char
*
816 /* first `clear' the whole table */
817 c
= *fmt
++; /* first char hat => negated scanset */
819 v
= 1; /* default => accept */
820 c
= *fmt
++; /* get new first char */
822 v
= 0; /* default => reject */
824 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
825 (void) memset(tab
, v
, 256);
828 return (fmt
- 1);/* format ended before closing ] */
831 * Now set the entries corresponding to the actual scanset
832 * to the opposite of the above.
834 * The first character may be ']' (or '-') without being special;
835 * the last character may be '-'.
839 tab
[c
] = v
; /* take character c */
841 n
= *fmt
++; /* and examine the next */
844 case 0: /* format ended too soon */
849 * A scanset of the form
851 * is defined as `the digit 0, the digit 1,
852 * the character +, the character -', but
853 * the effect of a scanset such as
855 * is implementation defined. The V7 Unix
856 * scanf treats `a-z' as `the letters a through
857 * z', but treats `a-a' as `the letter a, the
858 * character -, and the letter a'.
860 * For compatibility, the `-' is not considerd
861 * to define a range if the character following
862 * it is either a close bracket (required by ANSI)
863 * or is not numerically greater than the character
864 * we just stored in the table (c).
868 || (__collate_load_error
? n
< c
:
869 __collate_range_cmp (n
, c
) < 0
873 break; /* resume the for(;;) */
876 /* fill in the range */
877 if (__collate_load_error
) {
882 for (i
= 0; i
< 256; i
++)
883 if ( __collate_range_cmp (c
, i
) < 0
884 && __collate_range_cmp (i
, n
) <= 0
888 #if 1 /* XXX another disgusting compatibility hack */
891 * Alas, the V7 Unix scanf also treats formats
892 * such as [a-c-e] as `the letters a through e'.
893 * This too is permitted by the standard....
905 case ']': /* end of scanset */
908 default: /* just another character */
916 #ifndef NO_FLOATING_POINT
918 parsefloat(FILE *fp
, char *buf
, char *end
)
923 S_START
, S_GOTSIGN
, S_INF
, S_NAN
, S_MAYBEHEX
,
924 S_DIGITS
, S_FRAC
, S_EXP
, S_EXPDIGITS
927 char decpt
= *localeconv()->decimal_point
;
928 _Bool gotmantdig
= 0, ishex
= 0;
931 * We set commit = p whenever the string we have read so far
932 * constitutes a valid representation of a floating point
933 * number by itself. At some point, the parse will complete
934 * or fail, and we will ungetc() back to the last commit point.
935 * To ensure that the file offset gets updated properly, it is
936 * always necessary to read at least one character that doesn't
937 * match; thus, we can't short-circuit "infinity" or "nan(...)".
940 for (p
= buf
; p
< end
; ) {
946 if (c
== '-' || c
== '+')
971 (c
!= "nfinity"[infnanpos
] &&
972 c
!= "NFINITY"[infnanpos
]))
974 if (infnanpos
== 1 || infnanpos
== 6)
975 commit
= p
; /* inf or infinity */
980 case -1: /* XXX kludge to deal with nan(...) */
983 if (c
!= 'A' && c
!= 'a')
987 if (c
!= 'N' && c
!= 'n')
1000 } else if (!isalnum(c
) && c
!= '_')
1008 if (c
== 'X' || c
== 'x') {
1011 } else { /* we saw a '0', but no 'x' */
1016 if ((ishex
&& isxdigit(c
)) || isdigit(c
))
1027 if (((c
== 'E' || c
== 'e') && !ishex
) ||
1028 ((c
== 'P' || c
== 'p') && ishex
)) {
1033 } else if ((ishex
&& isxdigit(c
)) || isdigit(c
)) {
1040 state
= S_EXPDIGITS
;
1041 if (c
== '-' || c
== '+')
1057 else if (__srefill(fp
))
1062 while (commit
< --p
)
1063 __ungetc(*(u_char
*)p
, fp
);
1065 return (commit
- buf
);