]>
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
37 #include <sys/cdefs.h>
39 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid
[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
41 #endif /* LIBC_SCCS and not lint */
42 __FBSDID("FreeBSD: src/lib/libc/stdio/vfscanf.c,v 1.32 2003/06/28 09:03:05 das Exp ");
44 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.6 2003/07/05 03:39:23 tjr Exp $");
46 #include "namespace.h"
56 #include "un-namespace.h"
58 #include "libc_private.h"
61 #define FLOATING_POINT
67 #define BUF 513 /* Maximum length of numeric string. */
70 * Flags used during conversion.
72 #define LONG 0x01 /* l: long or double */
73 #define LONGDBL 0x02 /* L: long double */
74 #define SHORT 0x04 /* h: short */
75 #define SUPPRESS 0x08 /* *: suppress assignment */
76 #define POINTER 0x10 /* p: void * (as hex) */
77 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
78 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
79 #define INTMAXT 0x800 /* j: intmax_t */
80 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
81 #define SIZET 0x2000 /* z: size_t */
82 #define SHORTSHORT 0x4000 /* hh: char */
83 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
86 * The following are used in integral conversions only:
87 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
89 #define SIGNOK 0x40 /* +/- is (still) legal */
90 #define NDIGITS 0x80 /* no digits detected */
91 #define PFXOK 0x100 /* 0x prefix is (still) legal */
92 #define NZDIGITS 0x200 /* no zero digits detected */
97 #define CT_CHAR 0 /* %c conversion */
98 #define CT_CCL 1 /* %[...] conversion */
99 #define CT_STRING 2 /* %s conversion */
100 #define CT_INT 3 /* %[dioupxX] conversion */
101 #define CT_FLOAT 4 /* %[efgEFG] conversion */
103 static int parsefloat(FILE *, wchar_t *, wchar_t *);
105 extern int __scanfdebug
;
108 (cclcompl ? (wmemchr(ccls, (_c), ccle - ccls) == NULL) : \
109 (wmemchr(ccls, (_c), ccle - ccls) != NULL))
115 vfwscanf(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
121 ret
= __vfwscanf(fp
, fmt
, ap
);
127 * Non-MT-safe version.
130 __vfwscanf(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
132 wint_t c
; /* character from format, or conversion */
133 size_t width
; /* field width, or 0 */
134 wchar_t *p
; /* points into all kinds of strings */
135 int n
; /* handy integer */
136 int flags
; /* flags as defined above */
137 wchar_t *p0
; /* saves original value of p when necessary */
138 int nassigned
; /* number of fields assigned */
139 int nconversions
; /* number of conversions */
140 int nread
; /* number of characters consumed from fp */
141 int base
; /* base argument to conversion function */
142 wchar_t buf
[BUF
]; /* buffer for numeric conversions */
143 const wchar_t *ccls
; /* character class start */
144 const wchar_t *ccle
; /* character class end */
145 int cclcompl
; /* ccl is complemented? */
146 wint_t wi
; /* handy wint_t */
147 char *mbp
; /* multibyte string pointer for %c %s %[ */
148 size_t nconv
; /* number of bytes in mb. conversion */
149 mbstate_t mbs
; /* multibyte state */
150 char mbbuf
[MB_LEN_MAX
]; /* temporary mb. character buffer */
152 /* `basefix' is used to avoid `if' tests in the integer scanner */
153 static short basefix
[17] =
154 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
165 while ((c
= __fgetwc(fp
)) != WEOF
&&
177 * switch on the format. continue if done;
178 * break once format type is derived.
184 if ((wi
= __fgetwc(fp
)) == WEOF
)
207 flags
|= LONGLONG
; /* not quite */
226 case '0': case '1': case '2': case '3': case '4':
227 case '5': case '6': case '7': case '8': case '9':
228 width
= width
* 10 + c
- '0';
258 flags
|= PFXOK
; /* enable 0x prefixing */
264 #ifdef FLOATING_POINT
265 case 'A': case 'E': case 'F': case 'G':
266 case 'a': case 'e': case 'f': case 'g':
287 while (*fmt
!= '\0' && *fmt
!= ']')
303 case 'p': /* pointer format is like hex */
304 flags
|= POINTER
| PFXOK
;
305 c
= CT_INT
; /* assumes sizeof(uintmax_t) */
306 flags
|= UNSIGNED
; /* >= sizeof(uintptr_t) */
312 if (flags
& SUPPRESS
) /* ??? */
314 if (flags
& SHORTSHORT
)
315 *va_arg(ap
, char *) = nread
;
316 else if (flags
& SHORT
)
317 *va_arg(ap
, short *) = nread
;
318 else if (flags
& LONG
)
319 *va_arg(ap
, long *) = nread
;
320 else if (flags
& LONGLONG
)
321 *va_arg(ap
, long long *) = nread
;
322 else if (flags
& INTMAXT
)
323 *va_arg(ap
, intmax_t *) = nread
;
324 else if (flags
& SIZET
)
325 *va_arg(ap
, size_t *) = nread
;
326 else if (flags
& PTRDIFFT
)
327 *va_arg(ap
, ptrdiff_t *) = nread
;
329 *va_arg(ap
, int *) = nread
;
336 * Disgusting backwards compatibility hack. XXX
338 case '\0': /* compat */
343 * Consume leading white space, except for formats
344 * that suppress this.
346 if ((flags
& NOSKIP
) == 0) {
347 while ((wi
= __fgetwc(fp
)) != WEOF
&& iswspace(wi
))
360 /* scan arbitrary characters (sets NOSKIP) */
364 if (!(flags
& SUPPRESS
))
365 p
= va_arg(ap
, wchar_t *);
367 while (width
-- != 0 &&
368 (wi
= __fgetwc(fp
)) != WEOF
) {
369 if (!(flags
& SUPPRESS
))
376 if (!(flags
& SUPPRESS
))
379 if (!(flags
& SUPPRESS
))
380 mbp
= va_arg(ap
, char *);
382 memset(&mbs
, 0, sizeof(mbs
));
384 (wi
= __fgetwc(fp
)) != WEOF
) {
385 if (width
>= MB_CUR_MAX
&&
386 !(flags
& SUPPRESS
)) {
387 nconv
= wcrtomb(mbp
, wi
, &mbs
);
388 if (nconv
== (size_t)-1)
391 nconv
= wcrtomb(mbbuf
, wi
,
393 if (nconv
== (size_t)-1)
399 if (!(flags
& SUPPRESS
))
403 if (!(flags
& SUPPRESS
))
411 if (!(flags
& SUPPRESS
))
418 /* scan a (nonempty) character class (sets NOSKIP) */
420 width
= (size_t)~0; /* `infinity' */
421 /* take only those things in the class */
422 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
424 while ((wi
= __fgetwc(fp
)) != WEOF
&&
425 width
-- != 0 && INCCL(wi
))
431 } else if (flags
& LONG
) {
432 p0
= p
= va_arg(ap
, wchar_t *);
433 while ((wi
= __fgetwc(fp
)) != WEOF
&&
434 width
-- != 0 && INCCL(wi
))
444 if (!(flags
& SUPPRESS
))
445 mbp
= va_arg(ap
, char *);
447 memset(&mbs
, 0, sizeof(mbs
));
448 while ((wi
= __fgetwc(fp
)) != WEOF
&&
449 width
!= 0 && INCCL(wi
)) {
450 if (width
>= MB_CUR_MAX
&&
451 !(flags
& SUPPRESS
)) {
452 nconv
= wcrtomb(mbp
, wi
, &mbs
);
453 if (nconv
== (size_t)-1)
456 nconv
= wcrtomb(mbbuf
, wi
,
458 if (nconv
== (size_t)-1)
462 if (!(flags
& SUPPRESS
))
466 if (!(flags
& SUPPRESS
))
473 if (!(flags
& SUPPRESS
)) {
483 /* like CCL, but zero-length string OK, & no NOSKIP */
486 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
487 while ((wi
= __fgetwc(fp
)) != WEOF
&&
493 } else if (flags
& LONG
) {
494 p0
= p
= va_arg(ap
, wchar_t *);
495 while ((wi
= __fgetwc(fp
)) != WEOF
&&
506 if (!(flags
& SUPPRESS
))
507 mbp
= va_arg(ap
, char *);
508 memset(&mbs
, 0, sizeof(mbs
));
509 while ((wi
= __fgetwc(fp
)) != WEOF
&&
512 if (width
>= MB_CUR_MAX
&&
513 !(flags
& SUPPRESS
)) {
514 nconv
= wcrtomb(mbp
, wi
, &mbs
);
515 if (nconv
== (size_t)-1)
518 nconv
= wcrtomb(mbbuf
, wi
,
520 if (nconv
== (size_t)-1)
524 if (!(flags
& SUPPRESS
))
528 if (!(flags
& SUPPRESS
))
535 if (!(flags
& SUPPRESS
)) {
544 /* scan an integer as if by the conversion function */
545 if (width
== 0 || width
> sizeof(buf
) /
547 width
= sizeof(buf
) / sizeof(*buf
) - 1;
548 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
549 for (p
= buf
; width
; width
--) {
552 * Switch on the character; `goto ok'
553 * if we accept it as a part of number.
558 * The digit 0 is always legal, but is
559 * special. For %i conversions, if no
560 * digits (zero or nonzero) have been
561 * scanned (only signs), we will have
562 * base==0. In that case, we should set
563 * it to 8 and enable 0x prefixing.
564 * Also, if we have not scanned zero digits
565 * before this, do not turn off prefixing
566 * (someone else will turn it off if we
567 * have scanned any nonzero digits).
574 if (flags
& NZDIGITS
)
575 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
577 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
580 /* 1 through 7 always legal */
581 case '1': case '2': case '3':
582 case '4': case '5': case '6': case '7':
583 base
= basefix
[base
];
584 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
587 /* digits 8 and 9 ok iff decimal or hex */
589 base
= basefix
[base
];
591 break; /* not legal here */
592 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
595 /* letters ok iff hex */
596 case 'A': case 'B': case 'C':
597 case 'D': case 'E': case 'F':
598 case 'a': case 'b': case 'c':
599 case 'd': case 'e': case 'f':
600 /* no need to fix base here */
602 break; /* not legal here */
603 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
606 /* sign ok only as first character */
608 if (flags
& SIGNOK
) {
614 /* x ok iff flag still set & 2nd char */
616 if (flags
& PFXOK
&& p
== buf
+ 1) {
617 base
= 16; /* if %i */
625 * If we got here, c is not a legal character
626 * for a number. Stop accumulating digits.
633 * c is legal: store it and look at the next.
638 * If we had only a sign, it is no good; push
639 * back the sign. If the number ends in `x',
640 * it was [sign] '0' 'x', so push back the x
641 * and treat it as [sign] '0'.
643 if (flags
& NDIGITS
) {
649 if (c
== 'x' || c
== 'X') {
653 if ((flags
& SUPPRESS
) == 0) {
657 if ((flags
& UNSIGNED
) == 0)
658 res
= wcstoimax(buf
, NULL
, base
);
660 res
= wcstoumax(buf
, NULL
, base
);
662 *va_arg(ap
, void **) =
663 (void *)(uintptr_t)res
;
664 else if (flags
& SHORTSHORT
)
665 *va_arg(ap
, char *) = res
;
666 else if (flags
& SHORT
)
667 *va_arg(ap
, short *) = res
;
668 else if (flags
& LONG
)
669 *va_arg(ap
, long *) = res
;
670 else if (flags
& LONGLONG
)
671 *va_arg(ap
, long long *) = res
;
672 else if (flags
& INTMAXT
)
673 *va_arg(ap
, intmax_t *) = res
;
674 else if (flags
& PTRDIFFT
)
675 *va_arg(ap
, ptrdiff_t *) = res
;
676 else if (flags
& SIZET
)
677 *va_arg(ap
, size_t *) = res
;
679 *va_arg(ap
, int *) = res
;
686 #ifdef FLOATING_POINT
688 /* scan a floating point number as if by strtod */
689 if (width
== 0 || width
> sizeof(buf
) /
691 width
= sizeof(buf
) / sizeof(*buf
) - 1;
692 if ((width
= parsefloat(fp
, buf
, buf
+ width
)) == 0)
694 if ((flags
& SUPPRESS
) == 0) {
695 if (flags
& LONGDBL
) {
696 long double res
= wcstold(buf
, &p
);
697 *va_arg(ap
, long double *) = res
;
698 } else if (flags
& LONG
) {
699 double res
= wcstod(buf
, &p
);
700 *va_arg(ap
, double *) = res
;
702 float res
= wcstof(buf
, &p
);
703 *va_arg(ap
, float *) = res
;
705 if (__scanfdebug
&& p
- buf
!= width
)
712 #endif /* FLOATING_POINT */
716 return (nconversions
!= 0 ? nassigned
: EOF
);
721 #ifdef FLOATING_POINT
723 parsefloat(FILE *fp
, wchar_t *buf
, wchar_t *end
)
728 S_START
, S_GOTSIGN
, S_INF
, S_NAN
, S_MAYBEHEX
,
729 S_DIGITS
, S_FRAC
, S_EXP
, S_EXPDIGITS
732 wchar_t decpt
= (wchar_t)(unsigned char)*localeconv()->decimal_point
;
733 _Bool gotmantdig
= 0, ishex
= 0;
736 * We set commit = p whenever the string we have read so far
737 * constitutes a valid representation of a floating point
738 * number by itself. At some point, the parse will complete
739 * or fail, and we will ungetc() back to the last commit point.
740 * To ensure that the file offset gets updated properly, it is
741 * always necessary to read at least one character that doesn't
742 * match; thus, we can't short-circuit "infinity" or "nan(...)".
746 for (p
= buf
; p
< end
; ) {
747 if ((c
= __fgetwc(fp
)) == WEOF
)
753 if (c
== '-' || c
== '+')
778 (c
!= "nfinity"[infnanpos
] &&
779 c
!= "NFINITY"[infnanpos
]))
781 if (infnanpos
== 1 || infnanpos
== 6)
782 commit
= p
; /* inf or infinity */
787 case -1: /* XXX kludge to deal with nan(...) */
790 if (c
!= 'A' && c
!= 'a')
794 if (c
!= 'N' && c
!= 'n')
807 } else if (!iswalnum(c
) && c
!= '_')
815 if (c
== 'X' || c
== 'x') {
818 } else { /* we saw a '0', but no 'x' */
823 if (ishex
&& iswxdigit(c
) || iswdigit(c
))
834 if ((c
== 'E' || c
== 'e') && !ishex
||
835 (c
== 'P' || c
== 'p') && ishex
) {
840 } else if (ishex
&& iswxdigit(c
) || iswdigit(c
)) {
848 if (c
== '-' || c
== '+')
871 return (commit
- buf
);