]>
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 * 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 "namespace.h"
49 #include "un-namespace.h"
52 #include "libc_private.h"
55 #ifndef NO_FLOATING_POINT
59 #define BUF 513 /* Maximum length of numeric string. */
62 * Flags used during conversion.
64 #define LONG 0x01 /* l: long or double */
65 #define LONGDBL 0x02 /* L: long double */
66 #define SHORT 0x04 /* h: short */
67 #define SUPPRESS 0x08 /* *: suppress assignment */
68 #define POINTER 0x10 /* p: void * (as hex) */
69 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
70 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
71 #define INTMAXT 0x800 /* j: intmax_t */
72 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
73 #define SIZET 0x2000 /* z: size_t */
74 #define SHORTSHORT 0x4000 /* hh: char */
75 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
78 * The following are used in integral conversions only:
79 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
81 #define SIGNOK 0x40 /* +/- is (still) legal */
82 #define NDIGITS 0x80 /* no digits detected */
83 #define PFXOK 0x100 /* 0x prefix is (still) legal */
84 #define NZDIGITS 0x200 /* no zero digits detected */
85 #define HAVESIGN 0x10000 /* sign detected */
90 #define CT_CHAR 0 /* %c conversion */
91 #define CT_CCL 1 /* %[...] conversion */
92 #define CT_STRING 2 /* %s conversion */
93 #define CT_INT 3 /* %[dioupxX] conversion */
94 #define CT_FLOAT 4 /* %[efgEFG] conversion */
96 static const u_char
*__sccl(char *, const u_char
*);
97 #ifndef NO_FLOATING_POINT
98 static int parsefloat(FILE *, char *, char *);
101 __weak_reference(__vfscanf
, vfscanf
);
104 * __vfscanf - MT-safe version
107 __vfscanf(FILE *fp
, char const *fmt0
, va_list ap
)
112 ret
= __svfscanf(fp
, fmt0
, ap
);
118 * __svfscanf - non-MT-safe version of __vfscanf
121 __svfscanf(FILE *fp
, const char *fmt0
, va_list ap
)
123 const u_char
*fmt
= (const u_char
*)fmt0
;
124 int c
; /* character from format, or conversion */
125 size_t width
; /* field width, or 0 */
126 char *p
; /* points into all kinds of strings */
127 int n
; /* handy integer */
128 int flags
; /* flags as defined above */
129 char *p0
; /* saves original value of p when necessary */
130 int nassigned
; /* number of fields assigned */
131 int nconversions
; /* number of conversions */
132 int nread
; /* number of characters consumed from fp */
133 int base
; /* base argument to conversion function */
134 char ccltab
[256]; /* character class table for %[...] */
135 char buf
[BUF
]; /* buffer for numeric and mb conversions */
136 wchar_t *wcp
; /* handy wide character pointer */
137 size_t nconv
; /* length of multibyte sequence converted */
138 static const mbstate_t initial
;
141 /* `basefix' is used to avoid `if' tests in the integer scanner */
142 static short basefix
[17] =
143 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
155 while ((fp
->_r
> 0 || __srefill(fp
) == 0) && isspace(*fp
->_p
))
156 nread
++, fp
->_r
--, fp
->_p
++;
164 * switch on the format. continue if done;
165 * break once format type is derived.
171 if (fp
->_r
<= 0 && __srefill(fp
))
193 flags
|= LONGLONG
; /* not quite */
212 case '0': case '1': case '2': case '3': case '4':
213 case '5': case '6': case '7': case '8': case '9':
214 width
= width
* 10 + c
- '0';
244 flags
|= PFXOK
; /* enable 0x prefixing */
250 #ifndef NO_FLOATING_POINT
251 case 'A': case 'E': case 'F': case 'G':
252 case 'a': case 'e': case 'f': case 'g':
265 fmt
= __sccl(ccltab
, fmt
);
278 case 'p': /* pointer format is like hex */
279 flags
|= POINTER
| PFXOK
;
280 c
= CT_INT
; /* assumes sizeof(uintmax_t) */
281 flags
|= UNSIGNED
; /* >= sizeof(uintptr_t) */
287 if (flags
& SUPPRESS
) /* ??? */
289 if (flags
& SHORTSHORT
)
290 *va_arg(ap
, char *) = nread
;
291 else if (flags
& SHORT
)
292 *va_arg(ap
, short *) = nread
;
293 else if (flags
& LONG
)
294 *va_arg(ap
, long *) = nread
;
295 else if (flags
& LONGLONG
)
296 *va_arg(ap
, long long *) = nread
;
297 else if (flags
& INTMAXT
)
298 *va_arg(ap
, intmax_t *) = nread
;
299 else if (flags
& SIZET
)
300 *va_arg(ap
, size_t *) = nread
;
301 else if (flags
& PTRDIFFT
)
302 *va_arg(ap
, ptrdiff_t *) = nread
;
304 *va_arg(ap
, int *) = nread
;
311 * Disgusting backwards compatibility hack. XXX
313 case '\0': /* compat */
318 * We have a conversion that requires input.
320 if (fp
->_r
<= 0 && __srefill(fp
))
324 * Consume leading white space, except for formats
325 * that suppress this.
327 if ((flags
& NOSKIP
) == 0) {
328 while (isspace(*fp
->_p
)) {
332 else if (__srefill(fp
))
336 * Note that there is at least one character in
337 * the buffer, so conversions that do not set NOSKIP
338 * ca no longer result in an input failure.
348 /* scan arbitrary characters (sets NOSKIP) */
352 if ((flags
& SUPPRESS
) == 0)
353 wcp
= va_arg(ap
, wchar_t *);
358 if (n
== MB_CUR_MAX
) {
359 fp
->_flags
|= __SERR
;
366 nconv
= mbrtowc(wcp
, buf
, n
, &mbs
);
367 if (nconv
== (size_t)-1) {
368 fp
->_flags
|= __SERR
;
371 if (nconv
== 0 && !(flags
& SUPPRESS
))
373 if (nconv
!= (size_t)-2) {
376 if (!(flags
& SUPPRESS
))
380 if (fp
->_r
<= 0 && __srefill(fp
)) {
382 fp
->_flags
|= __SERR
;
388 if (!(flags
& SUPPRESS
))
390 } else if (flags
& SUPPRESS
) {
393 if ((n
= fp
->_r
) < width
) {
411 size_t r
= __fread((void *)va_arg(ap
, char *), 1,
423 /* scan a (nonempty) character class (sets NOSKIP) */
425 width
= (size_t)~0; /* `infinity' */
426 /* take only those things in the class */
431 if ((flags
& SUPPRESS
) == 0)
432 wcp
= va_arg(ap
, wchar_t *);
438 if (n
== MB_CUR_MAX
) {
439 fp
->_flags
|= __SERR
;
446 nconv
= mbrtowc(wcp
, buf
, n
, &mbs
);
447 if (nconv
== (size_t)-1) {
448 fp
->_flags
|= __SERR
;
453 if (nconv
!= (size_t)-2) {
454 if (wctob(*wcp
) != EOF
&&
455 !ccltab
[wctob(*wcp
)]) {
465 if (!(flags
& SUPPRESS
))
470 if (fp
->_r
<= 0 && __srefill(fp
)) {
472 fp
->_flags
|= __SERR
;
479 fp
->_flags
|= __SERR
;
485 if (!(flags
& SUPPRESS
)) {
489 } else if (flags
& SUPPRESS
) {
491 while (ccltab
[*fp
->_p
]) {
492 n
++, fp
->_r
--, fp
->_p
++;
495 if (fp
->_r
<= 0 && __srefill(fp
)) {
504 p0
= p
= va_arg(ap
, char *);
505 while (ccltab
[*fp
->_p
]) {
510 if (fp
->_r
<= 0 && __srefill(fp
)) {
527 /* like CCL, but zero-length string OK, & no NOSKIP */
533 if ((flags
& SUPPRESS
) == 0)
534 wcp
= va_arg(ap
, wchar_t *);
538 while (!isspace(*fp
->_p
) && width
!= 0) {
539 if (n
== MB_CUR_MAX
) {
540 fp
->_flags
|= __SERR
;
547 nconv
= mbrtowc(wcp
, buf
, n
, &mbs
);
548 if (nconv
== (size_t)-1) {
549 fp
->_flags
|= __SERR
;
554 if (nconv
!= (size_t)-2) {
555 if (iswspace(*wcp
)) {
565 if (!(flags
& SUPPRESS
))
569 if (fp
->_r
<= 0 && __srefill(fp
)) {
571 fp
->_flags
|= __SERR
;
577 if (!(flags
& SUPPRESS
)) {
581 } else if (flags
& SUPPRESS
) {
583 while (!isspace(*fp
->_p
)) {
584 n
++, fp
->_r
--, fp
->_p
++;
587 if (fp
->_r
<= 0 && __srefill(fp
))
592 p0
= p
= va_arg(ap
, char *);
593 while (!isspace(*fp
->_p
)) {
598 if (fp
->_r
<= 0 && __srefill(fp
))
609 /* scan an integer as if by the conversion function */
611 if (width
== 0 || width
> sizeof(buf
) - 1)
612 width
= sizeof(buf
) - 1;
614 /* size_t is unsigned, hence this optimisation */
615 if (--width
> sizeof(buf
) - 2)
616 width
= sizeof(buf
) - 2;
619 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
620 for (p
= buf
; width
; width
--) {
623 * Switch on the character; `goto ok'
624 * if we accept it as a part of number.
629 * The digit 0 is always legal, but is
630 * special. For %i conversions, if no
631 * digits (zero or nonzero) have been
632 * scanned (only signs), we will have
633 * base==0. In that case, we should set
634 * it to 8 and enable 0x prefixing.
635 * Also, if we have not scanned zero digits
636 * before this, do not turn off prefixing
637 * (someone else will turn it off if we
638 * have scanned any nonzero digits).
645 if (flags
& NZDIGITS
)
646 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
648 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
651 /* 1 through 7 always legal */
652 case '1': case '2': case '3':
653 case '4': case '5': case '6': case '7':
654 base
= basefix
[base
];
655 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
658 /* digits 8 and 9 ok iff decimal or hex */
660 base
= basefix
[base
];
662 break; /* not legal here */
663 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
666 /* letters ok iff hex */
667 case 'A': case 'B': case 'C':
668 case 'D': case 'E': case 'F':
669 case 'a': case 'b': case 'c':
670 case 'd': case 'e': case 'f':
671 /* no need to fix base here */
673 break; /* not legal here */
674 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
677 /* sign ok only as first character */
679 if (flags
& SIGNOK
) {
687 * x ok iff flag still set & 2nd char (or
688 * 3rd char if we have a sign).
691 if (flags
& PFXOK
&& p
==
692 buf
+ 1 + !!(flags
& HAVESIGN
)) {
693 base
= 16; /* if %i */
701 * If we got here, c is not a legal character
702 * for a number. Stop accumulating digits.
707 * c is legal: store it and look at the next.
712 else if (__srefill(fp
))
716 * If we had only a sign, it is no good; push
717 * back the sign. If the number ends in `x',
718 * it was [sign] '0' 'x', so push back the x
719 * and treat it as [sign] '0'.
721 if (flags
& NDIGITS
) {
723 (void) __ungetc(*(u_char
*)--p
, fp
);
726 c
= ((u_char
*)p
)[-1];
727 if (c
== 'x' || c
== 'X') {
729 (void) __ungetc(c
, fp
);
731 if ((flags
& SUPPRESS
) == 0) {
735 if ((flags
& UNSIGNED
) == 0)
736 res
= strtoimax(buf
, (char **)NULL
, base
);
738 res
= strtoumax(buf
, (char **)NULL
, base
);
740 *va_arg(ap
, void **) =
741 (void *)(uintptr_t)res
;
742 else if (flags
& SHORTSHORT
)
743 *va_arg(ap
, char *) = res
;
744 else if (flags
& SHORT
)
745 *va_arg(ap
, short *) = res
;
746 else if (flags
& LONG
)
747 *va_arg(ap
, long *) = res
;
748 else if (flags
& LONGLONG
)
749 *va_arg(ap
, long long *) = res
;
750 else if (flags
& INTMAXT
)
751 *va_arg(ap
, intmax_t *) = res
;
752 else if (flags
& PTRDIFFT
)
753 *va_arg(ap
, ptrdiff_t *) = res
;
754 else if (flags
& SIZET
)
755 *va_arg(ap
, size_t *) = res
;
757 *va_arg(ap
, int *) = res
;
764 #ifndef NO_FLOATING_POINT
766 /* scan a floating point number as if by strtod */
767 if (width
== 0 || width
> sizeof(buf
) - 1)
768 width
= sizeof(buf
) - 1;
769 if ((width
= parsefloat(fp
, buf
, buf
+ width
)) == 0)
771 if ((flags
& SUPPRESS
) == 0) {
772 if (flags
& LONGDBL
) {
773 long double res
= strtold(buf
, &p
);
774 *va_arg(ap
, long double *) = res
;
775 } else if (flags
& LONG
) {
776 double res
= strtod(buf
, &p
);
777 *va_arg(ap
, double *) = res
;
779 float res
= strtof(buf
, &p
);
780 *va_arg(ap
, float *) = res
;
787 #endif /* !NO_FLOATING_POINT */
791 return (nconversions
!= 0 ? nassigned
: EOF
);
797 * Fill in the given table from the scanset at the given format
798 * (just after `['). Return a pointer to the character past the
799 * closing `]'. The table has a 1 wherever characters should be
800 * considered part of the scanset.
802 static const u_char
*
809 /* first `clear' the whole table */
810 c
= *fmt
++; /* first char hat => negated scanset */
812 v
= 1; /* default => accept */
813 c
= *fmt
++; /* get new first char */
815 v
= 0; /* default => reject */
817 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
818 (void) memset(tab
, v
, 256);
821 return (fmt
- 1);/* format ended before closing ] */
824 * Now set the entries corresponding to the actual scanset
825 * to the opposite of the above.
827 * The first character may be ']' (or '-') without being special;
828 * the last character may be '-'.
832 tab
[c
] = v
; /* take character c */
834 n
= *fmt
++; /* and examine the next */
837 case 0: /* format ended too soon */
842 * A scanset of the form
844 * is defined as `the digit 0, the digit 1,
845 * the character +, the character -', but
846 * the effect of a scanset such as
848 * is implementation defined. The V7 Unix
849 * scanf treats `a-z' as `the letters a through
850 * z', but treats `a-a' as `the letter a, the
851 * character -, and the letter a'.
853 * For compatibility, the `-' is not considerd
854 * to define a range if the character following
855 * it is either a close bracket (required by ANSI)
856 * or is not numerically greater than the character
857 * we just stored in the table (c).
861 || (__collate_load_error
? n
< c
:
862 __collate_range_cmp (n
, c
) < 0
866 break; /* resume the for(;;) */
869 /* fill in the range */
870 if (__collate_load_error
) {
875 for (i
= 0; i
< 256; i
++)
876 if ( __collate_range_cmp (c
, i
) < 0
877 && __collate_range_cmp (i
, n
) <= 0
881 #if 1 /* XXX another disgusting compatibility hack */
884 * Alas, the V7 Unix scanf also treats formats
885 * such as [a-c-e] as `the letters a through e'.
886 * This too is permitted by the standard....
898 case ']': /* end of scanset */
901 default: /* just another character */
909 #ifndef NO_FLOATING_POINT
911 parsefloat(FILE *fp
, char *buf
, char *end
)
914 int infnanpos
= 0, decptpos
= 0;
916 S_START
, S_GOTSIGN
, S_INF
, S_NAN
, S_DONE
, S_MAYBEHEX
,
917 S_DIGITS
, S_DECPT
, S_FRAC
, S_EXP
, S_EXPDIGITS
920 const char *decpt
= localeconv()->decimal_point
;
921 _Bool gotmantdig
= 0, ishex
= 0;
924 * We set commit = p whenever the string we have read so far
925 * constitutes a valid representation of a floating point
926 * number by itself. At some point, the parse will complete
927 * or fail, and we will ungetc() back to the last commit point.
928 * To ensure that the file offset gets updated properly, it is
929 * always necessary to read at least one character that doesn't
930 * match; thus, we can't short-circuit "infinity" or "nan(...)".
933 for (p
= buf
; p
< end
; ) {
939 if (c
== '-' || c
== '+')
964 (c
!= "nfinity"[infnanpos
] &&
965 c
!= "NFINITY"[infnanpos
]))
967 if (infnanpos
== 1 || infnanpos
== 6)
968 commit
= p
; /* inf or infinity */
974 if (c
!= 'A' && c
!= 'a')
978 if (c
!= 'N' && c
!= 'n')
991 } else if (!isalnum(c
) && c
!= '_')
1001 if (c
== 'X' || c
== 'x') {
1004 } else { /* we saw a '0', but no 'x' */
1009 if ((ishex
&& isxdigit(c
)) || isdigit(c
)) {
1018 if (c
== decpt
[decptpos
]) {
1019 if (decpt
[++decptpos
] == '\0') {
1020 /* We read the complete decpt seq. */
1026 } else if (!decptpos
) {
1027 /* We didn't read any decpt characters. */
1032 * We read part of a multibyte decimal point,
1033 * but the rest is invalid, so bail.
1038 if (((c
== 'E' || c
== 'e') && !ishex
) ||
1039 ((c
== 'P' || c
== 'p') && ishex
)) {
1044 } else if ((ishex
&& isxdigit(c
)) || isdigit(c
)) {
1051 state
= S_EXPDIGITS
;
1052 if (c
== '-' || c
== '+')
1068 else if (__srefill(fp
))
1073 while (commit
< --p
)
1074 __ungetc(*(u_char
*)p
, fp
);
1076 return (commit
- buf
);