]>
git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/vfwscanf.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
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid
[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
40 #endif /* LIBC_SCCS and not lint */
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.12 2004/05/02 20:13:29 obrien Exp $");
45 #include "namespace.h"
55 #include "un-namespace.h"
57 #include "libc_private.h"
60 #ifndef NO_FLOATING_POINT
64 #define BUF 513 /* Maximum length of numeric string. */
67 * Flags used during conversion.
69 #define LONG 0x01 /* l: long or double */
70 #define LONGDBL 0x02 /* L: long double */
71 #define SHORT 0x04 /* h: short */
72 #define SUPPRESS 0x08 /* *: suppress assignment */
73 #define POINTER 0x10 /* p: void * (as hex) */
74 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
75 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
76 #define INTMAXT 0x800 /* j: intmax_t */
77 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
78 #define SIZET 0x2000 /* z: size_t */
79 #define SHORTSHORT 0x4000 /* hh: char */
80 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
83 * The following are used in integral conversions only:
84 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
86 #define SIGNOK 0x40 /* +/- is (still) legal */
87 #define NDIGITS 0x80 /* no digits detected */
88 #define PFXOK 0x100 /* 0x prefix is (still) legal */
89 #define NZDIGITS 0x200 /* no zero digits detected */
90 #define HAVESIGN 0x10000 /* sign detected */
95 #define CT_CHAR 0 /* %c conversion */
96 #define CT_CCL 1 /* %[...] conversion */
97 #define CT_STRING 2 /* %s conversion */
98 #define CT_INT 3 /* %[dioupxX] conversion */
99 #define CT_FLOAT 4 /* %[efgEFG] conversion */
101 static int parsefloat(FILE *, wchar_t *, wchar_t *);
103 extern int __scanfdebug
;
106 (cclcompl ? (wmemchr(ccls, (_c), ccle - ccls) == NULL) : \
107 (wmemchr(ccls, (_c), ccle - ccls) != NULL))
113 vfwscanf(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
119 ret
= __vfwscanf(fp
, fmt
, ap
);
125 * Non-MT-safe version.
128 __vfwscanf(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
130 wint_t c
; /* character from format, or conversion */
131 size_t width
; /* field width, or 0 */
132 wchar_t *p
; /* points into all kinds of strings */
133 int n
; /* handy integer */
134 int flags
; /* flags as defined above */
135 wchar_t *p0
; /* saves original value of p when necessary */
136 int nassigned
; /* number of fields assigned */
137 int nconversions
; /* number of conversions */
138 int nread
; /* number of characters consumed from fp */
139 int base
; /* base argument to conversion function */
140 wchar_t buf
[BUF
]; /* buffer for numeric conversions */
141 const wchar_t *ccls
; /* character class start */
142 const wchar_t *ccle
; /* character class end */
143 int cclcompl
; /* ccl is complemented? */
144 wint_t wi
; /* handy wint_t */
145 char *mbp
; /* multibyte string pointer for %c %s %[ */
146 size_t nconv
; /* number of bytes in mb. conversion */
147 char mbbuf
[MB_LEN_MAX
]; /* temporary mb. character buffer */
148 static const mbstate_t initial
;
151 /* `basefix' is used to avoid `if' tests in the integer scanner */
152 static short basefix
[17] =
153 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
164 while ((c
= __fgetwc(fp
)) != WEOF
&&
176 * switch on the format. continue if done;
177 * break once format type is derived.
183 if ((wi
= __fgetwc(fp
)) == WEOF
)
206 flags
|= LONGLONG
; /* not quite */
225 case '0': case '1': case '2': case '3': case '4':
226 case '5': case '6': case '7': case '8': case '9':
227 width
= width
* 10 + c
- '0';
257 flags
|= PFXOK
; /* enable 0x prefixing */
263 #ifndef NO_FLOATING_POINT
264 case 'A': case 'E': case 'F': case 'G':
265 case 'a': case 'e': case 'f': case 'g':
286 while (*fmt
!= '\0' && *fmt
!= ']')
302 case 'p': /* pointer format is like hex */
303 flags
|= POINTER
| PFXOK
;
304 c
= CT_INT
; /* assumes sizeof(uintmax_t) */
305 flags
|= UNSIGNED
; /* >= sizeof(uintptr_t) */
311 if (flags
& SUPPRESS
) /* ??? */
313 if (flags
& SHORTSHORT
)
314 *va_arg(ap
, char *) = nread
;
315 else if (flags
& SHORT
)
316 *va_arg(ap
, short *) = nread
;
317 else if (flags
& LONG
)
318 *va_arg(ap
, long *) = nread
;
319 else if (flags
& LONGLONG
)
320 *va_arg(ap
, long long *) = nread
;
321 else if (flags
& INTMAXT
)
322 *va_arg(ap
, intmax_t *) = nread
;
323 else if (flags
& SIZET
)
324 *va_arg(ap
, size_t *) = nread
;
325 else if (flags
& PTRDIFFT
)
326 *va_arg(ap
, ptrdiff_t *) = nread
;
328 *va_arg(ap
, int *) = nread
;
335 * Disgusting backwards compatibility hack. XXX
337 case '\0': /* compat */
342 * Consume leading white space, except for formats
343 * that suppress this.
345 if ((flags
& NOSKIP
) == 0) {
346 while ((wi
= __fgetwc(fp
)) != WEOF
&& iswspace(wi
))
359 /* scan arbitrary characters (sets NOSKIP) */
363 if (!(flags
& SUPPRESS
))
364 p
= va_arg(ap
, wchar_t *);
366 while (width
-- != 0 &&
367 (wi
= __fgetwc(fp
)) != WEOF
) {
368 if (!(flags
& SUPPRESS
))
375 if (!(flags
& SUPPRESS
))
378 if (!(flags
& SUPPRESS
))
379 mbp
= va_arg(ap
, char *);
383 (wi
= __fgetwc(fp
)) != WEOF
) {
384 if (width
>= MB_CUR_MAX
&&
385 !(flags
& SUPPRESS
)) {
386 nconv
= wcrtomb(mbp
, wi
, &mbs
);
387 if (nconv
== (size_t)-1)
390 nconv
= wcrtomb(mbbuf
, wi
,
392 if (nconv
== (size_t)-1)
398 if (!(flags
& SUPPRESS
))
402 if (!(flags
& SUPPRESS
))
410 if (!(flags
& SUPPRESS
))
417 /* scan a (nonempty) character class (sets NOSKIP) */
419 width
= (size_t)~0; /* `infinity' */
420 /* take only those things in the class */
421 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
423 while ((wi
= __fgetwc(fp
)) != WEOF
&&
424 width
-- != 0 && INCCL(wi
))
430 } else if (flags
& LONG
) {
431 p0
= p
= va_arg(ap
, wchar_t *);
432 while ((wi
= __fgetwc(fp
)) != WEOF
&&
433 width
-- != 0 && INCCL(wi
))
443 if (!(flags
& SUPPRESS
))
444 mbp
= va_arg(ap
, char *);
447 while ((wi
= __fgetwc(fp
)) != WEOF
&&
448 width
!= 0 && INCCL(wi
)) {
449 if (width
>= MB_CUR_MAX
&&
450 !(flags
& SUPPRESS
)) {
451 nconv
= wcrtomb(mbp
, wi
, &mbs
);
452 if (nconv
== (size_t)-1)
455 nconv
= wcrtomb(mbbuf
, wi
,
457 if (nconv
== (size_t)-1)
461 if (!(flags
& SUPPRESS
))
465 if (!(flags
& SUPPRESS
))
472 if (!(flags
& SUPPRESS
)) {
482 /* like CCL, but zero-length string OK, & no NOSKIP */
485 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
486 while ((wi
= __fgetwc(fp
)) != WEOF
&&
492 } else if (flags
& LONG
) {
493 p0
= p
= va_arg(ap
, wchar_t *);
494 while ((wi
= __fgetwc(fp
)) != WEOF
&&
505 if (!(flags
& SUPPRESS
))
506 mbp
= va_arg(ap
, char *);
508 while ((wi
= __fgetwc(fp
)) != WEOF
&&
511 if (width
>= MB_CUR_MAX
&&
512 !(flags
& SUPPRESS
)) {
513 nconv
= wcrtomb(mbp
, wi
, &mbs
);
514 if (nconv
== (size_t)-1)
517 nconv
= wcrtomb(mbbuf
, wi
,
519 if (nconv
== (size_t)-1)
523 if (!(flags
& SUPPRESS
))
527 if (!(flags
& SUPPRESS
))
534 if (!(flags
& SUPPRESS
)) {
543 /* scan an integer as if by the conversion function */
544 if (width
== 0 || width
> sizeof(buf
) /
546 width
= sizeof(buf
) / sizeof(*buf
) - 1;
547 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
548 for (p
= buf
; width
; width
--) {
551 * Switch on the character; `goto ok'
552 * if we accept it as a part of number.
557 * The digit 0 is always legal, but is
558 * special. For %i conversions, if no
559 * digits (zero or nonzero) have been
560 * scanned (only signs), we will have
561 * base==0. In that case, we should set
562 * it to 8 and enable 0x prefixing.
563 * Also, if we have not scanned zero digits
564 * before this, do not turn off prefixing
565 * (someone else will turn it off if we
566 * have scanned any nonzero digits).
573 if (flags
& NZDIGITS
)
574 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
576 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
579 /* 1 through 7 always legal */
580 case '1': case '2': case '3':
581 case '4': case '5': case '6': case '7':
582 base
= basefix
[base
];
583 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
586 /* digits 8 and 9 ok iff decimal or hex */
588 base
= basefix
[base
];
590 break; /* not legal here */
591 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
594 /* letters ok iff hex */
595 case 'A': case 'B': case 'C':
596 case 'D': case 'E': case 'F':
597 case 'a': case 'b': case 'c':
598 case 'd': case 'e': case 'f':
599 /* no need to fix base here */
601 break; /* not legal here */
602 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
605 /* sign ok only as first character */
607 if (flags
& SIGNOK
) {
615 * x ok iff flag still set & 2nd char (or
616 * 3rd char if we have a sign).
619 if (flags
& PFXOK
&& p
==
620 buf
+ 1 + !!(flags
& HAVESIGN
)) {
621 base
= 16; /* if %i */
629 * If we got here, c is not a legal character
630 * for a number. Stop accumulating digits.
637 * c is legal: store it and look at the next.
642 * If we had only a sign, it is no good; push
643 * back the sign. If the number ends in `x',
644 * it was [sign] '0' 'x', so push back the x
645 * and treat it as [sign] '0'.
647 if (flags
& NDIGITS
) {
653 if (c
== 'x' || c
== 'X') {
657 if ((flags
& SUPPRESS
) == 0) {
661 if ((flags
& UNSIGNED
) == 0)
662 res
= wcstoimax(buf
, NULL
, base
);
664 res
= wcstoumax(buf
, NULL
, base
);
666 *va_arg(ap
, void **) =
667 (void *)(uintptr_t)res
;
668 else if (flags
& SHORTSHORT
)
669 *va_arg(ap
, char *) = res
;
670 else if (flags
& SHORT
)
671 *va_arg(ap
, short *) = res
;
672 else if (flags
& LONG
)
673 *va_arg(ap
, long *) = res
;
674 else if (flags
& LONGLONG
)
675 *va_arg(ap
, long long *) = res
;
676 else if (flags
& INTMAXT
)
677 *va_arg(ap
, intmax_t *) = res
;
678 else if (flags
& PTRDIFFT
)
679 *va_arg(ap
, ptrdiff_t *) = res
;
680 else if (flags
& SIZET
)
681 *va_arg(ap
, size_t *) = res
;
683 *va_arg(ap
, int *) = res
;
690 #ifndef NO_FLOATING_POINT
692 /* scan a floating point number as if by strtod */
693 if (width
== 0 || width
> sizeof(buf
) /
695 width
= sizeof(buf
) / sizeof(*buf
) - 1;
696 if ((width
= parsefloat(fp
, buf
, buf
+ width
)) == 0)
698 if ((flags
& SUPPRESS
) == 0) {
699 if (flags
& LONGDBL
) {
700 long double res
= wcstold(buf
, &p
);
701 *va_arg(ap
, long double *) = res
;
702 } else if (flags
& LONG
) {
703 double res
= wcstod(buf
, &p
);
704 *va_arg(ap
, double *) = res
;
706 float res
= wcstof(buf
, &p
);
707 *va_arg(ap
, float *) = res
;
709 if (__scanfdebug
&& p
- buf
!= width
)
716 #endif /* !NO_FLOATING_POINT */
720 return (nconversions
!= 0 ? nassigned
: EOF
);
725 #ifndef NO_FLOATING_POINT
727 parsefloat(FILE *fp
, wchar_t *buf
, wchar_t *end
)
732 S_START
, S_GOTSIGN
, S_INF
, S_NAN
, S_MAYBEHEX
,
733 S_DIGITS
, S_FRAC
, S_EXP
, S_EXPDIGITS
736 wchar_t decpt
= (wchar_t)(unsigned char)*localeconv()->decimal_point
;
737 _Bool gotmantdig
= 0, ishex
= 0;
740 * We set commit = p whenever the string we have read so far
741 * constitutes a valid representation of a floating point
742 * number by itself. At some point, the parse will complete
743 * or fail, and we will ungetc() back to the last commit point.
744 * To ensure that the file offset gets updated properly, it is
745 * always necessary to read at least one character that doesn't
746 * match; thus, we can't short-circuit "infinity" or "nan(...)".
750 for (p
= buf
; p
< end
; ) {
751 if ((c
= __fgetwc(fp
)) == WEOF
)
757 if (c
== '-' || c
== '+')
782 (c
!= "nfinity"[infnanpos
] &&
783 c
!= "NFINITY"[infnanpos
]))
785 if (infnanpos
== 1 || infnanpos
== 6)
786 commit
= p
; /* inf or infinity */
791 case -1: /* XXX kludge to deal with nan(...) */
794 if (c
!= 'A' && c
!= 'a')
798 if (c
!= 'N' && c
!= 'n')
811 } else if (!iswalnum(c
) && c
!= '_')
819 if (c
== 'X' || c
== 'x') {
822 } else { /* we saw a '0', but no 'x' */
827 if ((ishex
&& iswxdigit(c
)) || iswdigit(c
))
838 if (((c
== 'E' || c
== 'e') && !ishex
) ||
839 ((c
== 'P' || c
== 'p') && ishex
)) {
844 } else if ((ishex
&& iswxdigit(c
)) || iswdigit(c
)) {
852 if (c
== '-' || c
== '+')
875 return (commit
- buf
);