]>
git.saurik.com Git - apple/libc.git/blob - stdio/vfwscanf-fbsd.c
2bee85d933d171c5c94fa8821eca69e160cb11e6
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 "xlocale_private.h"
47 #include "namespace.h"
57 #include "un-namespace.h"
59 #include "libc_private.h"
62 #ifndef NO_FLOATING_POINT
66 #define BUF 513 /* Maximum length of numeric string. */
69 * Flags used during conversion.
71 #define LONG 0x01 /* l: long or double */
72 #define LONGDBL 0x02 /* L: long double */
73 #define SHORT 0x04 /* h: short */
74 #define SUPPRESS 0x08 /* *: suppress assignment */
75 #define POINTER 0x10 /* p: void * (as hex) */
76 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
77 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
78 #define INTMAXT 0x800 /* j: intmax_t */
79 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
80 #define SIZET 0x2000 /* z: size_t */
81 #define SHORTSHORT 0x4000 /* hh: char */
82 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
85 * The following are used in integral conversions only:
86 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
88 #define SIGNOK 0x40 /* +/- is (still) legal */
89 #define NDIGITS 0x80 /* no digits detected */
90 #define PFXOK 0x100 /* 0x prefix is (still) legal */
91 #define NZDIGITS 0x200 /* no zero digits detected */
92 #define HAVESIGN 0x10000 /* sign 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 #ifndef NO_FLOATING_POINT
104 static int parsefloat(FILE *, wchar_t **, size_t, locale_t loc
);
105 #endif /* !NO_FLOATING_POINT */
107 extern int __scanfdebug
;
110 (cclcompl ? (wmemchr(ccls, (_c), ccle - ccls) == NULL) : \
111 (wmemchr(ccls, (_c), ccle - ccls) != NULL))
117 vfwscanf(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
123 ret
= __vfwscanf(fp
, __current_locale(), fmt
, ap
);
129 vfwscanf_l(FILE * __restrict fp
, locale_t loc
, const wchar_t * __restrict fmt
,
134 NORMALIZE_LOCALE(loc
);
137 ret
= __vfwscanf(fp
, loc
, fmt
, ap
);
143 * Non-MT-safe version.
145 __private_extern__
int
146 __vfwscanf(FILE * __restrict fp
, locale_t loc
, const wchar_t * __restrict fmt
,
149 wint_t c
; /* character from format, or conversion */
150 size_t width
; /* field width, or 0 */
151 wchar_t *p
; /* points into all kinds of strings */
152 int n
; /* handy integer */
153 int flags
; /* flags as defined above */
154 wchar_t *p0
; /* saves original value of p when necessary */
155 int nassigned
; /* number of fields assigned */
156 int nread
; /* number of characters consumed from fp */
157 int base
; /* base argument to conversion function */
158 wchar_t buf
[BUF
]; /* buffer for numeric conversions */
159 const wchar_t *ccls
; /* character class start */
160 const wchar_t *ccle
; /* character class end */
161 int cclcompl
; /* ccl is complemented? */
162 wint_t wi
; /* handy wint_t */
163 char *mbp
; /* multibyte string pointer for %c %s %[ */
164 size_t nconv
; /* number of bytes in mb. conversion */
165 char mbbuf
[MB_LEN_MAX
]; /* temporary mb. character buffer */
166 int index
; /* for %index$ */
167 va_list ap_orig
; /* to reset ap to first argument */
168 static const mbstate_t initial
;
170 int mb_cur_max
= MB_CUR_MAX_L(loc
);
172 /* `basefix' is used to avoid `if' tests in the integer scanner */
173 static short basefix
[17] =
174 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
179 va_copy(ap_orig
, ap
);
184 if (iswspace_l(c
, loc
)) {
185 while ((c
= __fgetwc(fp
, loc
)) != WEOF
&&
189 __ungetwc(c
, fp
, loc
);
197 * switch on the format. continue if done;
198 * break once format type is derived.
204 if ((wi
= __fgetwc(fp
, loc
)) == WEOF
)
207 __ungetwc(wi
, fp
, loc
);
215 if (index
< 1 || index
> NL_ARGMAX
|| fmt
[-3] != '%') {
220 va_copy(ap
, ap_orig
); /* reset to %1$ */
221 for (; index
> 1; index
--) {
239 flags
|= LONGLONG
; /* not quite */
258 case '0': case '1': case '2': case '3': case '4':
259 case '5': case '6': case '7': case '8': case '9':
260 width
= width
* 10 + c
- '0';
290 flags
|= PFXOK
; /* enable 0x prefixing */
296 #ifndef NO_FLOATING_POINT
297 case 'A': case 'E': case 'F': case 'G':
298 case 'a': case 'e': case 'f': case 'g':
319 while (*fmt
!= '\0' && *fmt
!= ']')
335 case 'p': /* pointer format is like hex */
336 flags
|= POINTER
| PFXOK
;
337 c
= CT_INT
; /* assumes sizeof(uintmax_t) */
338 flags
|= UNSIGNED
; /* >= sizeof(uintptr_t) */
343 if (flags
& SUPPRESS
) /* ??? */
345 if (flags
& SHORTSHORT
)
346 *va_arg(ap
, char *) = nread
;
347 else if (flags
& SHORT
)
348 *va_arg(ap
, short *) = nread
;
349 else if (flags
& LONG
)
350 *va_arg(ap
, long *) = nread
;
351 else if (flags
& LONGLONG
)
352 *va_arg(ap
, long long *) = nread
;
353 else if (flags
& INTMAXT
)
354 *va_arg(ap
, intmax_t *) = nread
;
355 else if (flags
& SIZET
)
356 *va_arg(ap
, size_t *) = nread
;
357 else if (flags
& PTRDIFFT
)
358 *va_arg(ap
, ptrdiff_t *) = nread
;
360 *va_arg(ap
, int *) = nread
;
367 * Disgusting backwards compatibility hack. XXX
369 case '\0': /* compat */
374 * Consume leading white space, except for formats
375 * that suppress this.
377 if ((flags
& NOSKIP
) == 0) {
378 while ((wi
= __fgetwc(fp
, loc
)) != WEOF
&& iswspace_l(wi
, loc
))
382 __ungetwc(wi
, fp
, loc
);
391 /* scan arbitrary characters (sets NOSKIP) */
395 if (!(flags
& SUPPRESS
))
396 p
= va_arg(ap
, wchar_t *);
398 while (width
-- != 0 &&
399 (wi
= __fgetwc(fp
, loc
)) != WEOF
) {
400 if (!(flags
& SUPPRESS
))
407 if (!(flags
& SUPPRESS
))
410 if (!(flags
& SUPPRESS
))
411 mbp
= va_arg(ap
, char *);
415 (wi
= __fgetwc(fp
, loc
)) != WEOF
) {
416 if (width
>= mb_cur_max
&&
417 !(flags
& SUPPRESS
)) {
418 nconv
= wcrtomb_l(mbp
, wi
, &mbs
, loc
);
419 if (nconv
== (size_t)-1)
422 nconv
= wcrtomb_l(mbbuf
, wi
,
424 if (nconv
== (size_t)-1)
427 __ungetwc(wi
, fp
, loc
);
430 if (!(flags
& SUPPRESS
))
434 if (!(flags
& SUPPRESS
))
442 if (!(flags
& SUPPRESS
))
448 /* scan a (nonempty) character class (sets NOSKIP) */
450 width
= (size_t)~0; /* `infinity' */
451 /* take only those things in the class */
452 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
454 while ((wi
= __fgetwc(fp
, loc
)) != WEOF
&&
455 width
-- != 0 && INCCL(wi
))
458 __ungetwc(wi
, fp
, loc
);
461 } else if (flags
& LONG
) {
462 p0
= p
= va_arg(ap
, wchar_t *);
463 while ((wi
= __fgetwc(fp
, loc
)) != WEOF
&&
464 width
-- != 0 && INCCL(wi
))
467 __ungetwc(wi
, fp
, loc
);
474 if (!(flags
& SUPPRESS
))
475 mbp
= va_arg(ap
, char *);
478 while ((wi
= __fgetwc(fp
, loc
)) != WEOF
&&
479 width
!= 0 && INCCL(wi
)) {
480 if (width
>= mb_cur_max
&&
481 !(flags
& SUPPRESS
)) {
482 nconv
= wcrtomb_l(mbp
, wi
, &mbs
, loc
);
483 if (nconv
== (size_t)-1)
486 nconv
= wcrtomb_l(mbbuf
, wi
,
488 if (nconv
== (size_t)-1)
492 if (!(flags
& SUPPRESS
))
496 if (!(flags
& SUPPRESS
))
502 __ungetwc(wi
, fp
, loc
);
505 if (!(flags
& SUPPRESS
)) {
514 /* like CCL, but zero-length string OK, & no NOSKIP */
517 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
518 while ((wi
= __fgetwc(fp
, loc
)) != WEOF
&&
520 !iswspace_l(wi
, loc
))
523 __ungetwc(wi
, fp
, loc
);
524 } else if (flags
& LONG
) {
525 p0
= p
= va_arg(ap
, wchar_t *);
526 while ((wi
= __fgetwc(fp
, loc
)) != WEOF
&&
528 !iswspace_l(wi
, loc
)) {
533 __ungetwc(wi
, fp
, loc
);
537 if (!(flags
& SUPPRESS
))
538 mbp
= va_arg(ap
, char *);
540 while ((wi
= __fgetwc(fp
, loc
)) != WEOF
&&
542 !iswspace_l(wi
, loc
)) {
543 if (width
>= mb_cur_max
&&
544 !(flags
& SUPPRESS
)) {
545 nconv
= wcrtomb_l(mbp
, wi
, &mbs
, loc
);
546 if (nconv
== (size_t)-1)
549 nconv
= wcrtomb_l(mbbuf
, wi
,
551 if (nconv
== (size_t)-1)
555 if (!(flags
& SUPPRESS
))
559 if (!(flags
& SUPPRESS
))
565 __ungetwc(wi
, fp
, loc
);
566 if (!(flags
& SUPPRESS
)) {
574 /* scan an integer as if by the conversion function */
575 if (width
== 0 || width
> sizeof(buf
) /
577 width
= sizeof(buf
) / sizeof(*buf
) - 1;
578 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
579 for (p
= buf
; width
; width
--) {
580 c
= __fgetwc(fp
, loc
);
582 * Switch on the character; `goto ok'
583 * if we accept it as a part of number.
588 * The digit 0 is always legal, but is
589 * special. For %i conversions, if no
590 * digits (zero or nonzero) have been
591 * scanned (only signs), we will have
592 * base==0. In that case, we should set
593 * it to 8 and enable 0x prefixing.
594 * Also, if we have not scanned zero digits
595 * before this, do not turn off prefixing
596 * (someone else will turn it off if we
597 * have scanned any nonzero digits).
604 if (flags
& NZDIGITS
)
605 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
607 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
610 /* 1 through 7 always legal */
611 case '1': case '2': case '3':
612 case '4': case '5': case '6': case '7':
613 base
= basefix
[base
];
614 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
617 /* digits 8 and 9 ok iff decimal or hex */
619 base
= basefix
[base
];
621 break; /* not legal here */
622 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
625 /* letters ok iff hex */
626 case 'A': case 'B': case 'C':
627 case 'D': case 'E': case 'F':
628 case 'a': case 'b': case 'c':
629 case 'd': case 'e': case 'f':
630 /* no need to fix base here */
632 break; /* not legal here */
633 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
636 /* sign ok only as first character */
638 if (flags
& SIGNOK
) {
646 * x ok iff flag still set & 2nd char (or
647 * 3rd char if we have a sign).
650 if (flags
& PFXOK
&& p
==
651 buf
+ 1 + !!(flags
& HAVESIGN
)) {
652 base
= 16; /* if %i */
660 * If we got here, c is not a legal character
661 * for a number. Stop accumulating digits.
664 __ungetwc(c
, fp
, loc
);
668 * c is legal: store it and look at the next.
673 * If we had only a sign, it is no good; push
674 * back the sign. If the number ends in `x',
675 * it was [sign] '0' 'x', so push back the x
676 * and treat it as [sign] '0'.
678 if (flags
& NDIGITS
) {
680 __ungetwc(*--p
, fp
, loc
);
684 if (c
== 'x' || c
== 'X') {
686 __ungetwc(c
, fp
, loc
);
688 if ((flags
& SUPPRESS
) == 0) {
692 if ((flags
& UNSIGNED
) == 0)
693 res
= wcstoimax_l(buf
, NULL
, base
, loc
);
695 res
= wcstoumax_l(buf
, NULL
, base
, loc
);
697 *va_arg(ap
, void **) =
698 (void *)(uintptr_t)res
;
699 else if (flags
& SHORTSHORT
)
700 *va_arg(ap
, char *) = res
;
701 else if (flags
& SHORT
)
702 *va_arg(ap
, short *) = res
;
703 else if (flags
& LONG
)
704 *va_arg(ap
, long *) = res
;
705 else if (flags
& LONGLONG
)
706 *va_arg(ap
, long long *) = res
;
707 else if (flags
& INTMAXT
)
708 *va_arg(ap
, intmax_t *) = res
;
709 else if (flags
& PTRDIFFT
)
710 *va_arg(ap
, ptrdiff_t *) = res
;
711 else if (flags
& SIZET
)
712 *va_arg(ap
, size_t *) = res
;
714 *va_arg(ap
, int *) = res
;
720 #ifndef NO_FLOATING_POINT
724 /* scan a floating point number as if by strtod */
725 if ((width
= parsefloat(fp
, &pbuf
, width
, loc
)) == 0)
727 if ((flags
& SUPPRESS
) == 0) {
728 if (flags
& LONGDBL
) {
729 long double res
= wcstold_l(pbuf
, &p
, loc
);
730 *va_arg(ap
, long double *) = res
;
731 } else if (flags
& LONG
) {
732 double res
= wcstod_l(pbuf
, &p
, loc
);
733 *va_arg(ap
, double *) = res
;
735 float res
= wcstof_l(pbuf
, &p
, loc
);
736 *va_arg(ap
, float *) = res
;
738 if (__scanfdebug
&& p
- pbuf
!= width
)
745 #endif /* !NO_FLOATING_POINT */
749 return (nassigned
? nassigned
: EOF
);
754 #ifndef NO_FLOATING_POINT
756 parsefloat(FILE *fp
, wchar_t **buf
, size_t width
, locale_t loc
)
761 S_START
, S_GOTSIGN
, S_INF
, S_NAN
, S_MAYBEHEX
,
762 S_DIGITS
, S_FRAC
, S_EXP
, S_EXPDIGITS
767 _Bool gotmantdig
= 0, ishex
= 0;
768 static wchar_t *b
= NULL
;
769 static size_t bsiz
= 0;
774 b
= (wchar_t *)malloc(BUF
* sizeof(wchar_t));
781 s
= (width
== 0 ? bsiz
: (width
+ 1));
783 b
= (wchar_t *)reallocf(b
, s
* sizeof(wchar_t));
793 * We set commit = p whenever the string we have read so far
794 * constitutes a valid representation of a floating point
795 * number by itself. At some point, the parse will complete
796 * or fail, and we will ungetc() back to the last commit point.
797 * To ensure that the file offset gets updated properly, it is
798 * always necessary to read at least one character that doesn't
799 * match; thus, we can't short-circuit "infinity" or "nan(...)".
803 decimal_point
= localeconv_l(loc
)->decimal_point
;
804 mbtowc_l(&decpt
, decimal_point
, strlen(decimal_point
), loc
);
805 for (p
= b
; width
== 0 || p
< e
; ) {
806 if ((c
= __fgetwc(fp
, loc
)) == WEOF
)
812 if (c
== '-' || c
== '+')
837 (c
!= "nfinity"[infnanpos
] &&
838 c
!= "NFINITY"[infnanpos
]))
840 if (infnanpos
== 1 || infnanpos
== 6)
841 commit
= p
; /* inf or infinity */
846 case -1: /* XXX kludge to deal with nan(...) */
849 if (c
!= 'A' && c
!= 'a')
853 if (c
!= 'N' && c
!= 'n')
866 } else if (!iswalnum_l(c
, loc
) && c
!= '_')
874 if (c
== 'X' || c
== 'x') {
877 } else { /* we saw a '0', but no 'x' */
882 if ((ishex
&& iswxdigit_l(c
, loc
)) || iswdigit_l(c
, loc
))
893 if (((c
== 'E' || c
== 'e') && !ishex
) ||
894 ((c
== 'P' || c
== 'p') && ishex
)) {
899 } else if ((ishex
&& iswxdigit_l(c
, loc
)) || iswdigit_l(c
, loc
)) {
907 if (c
== '-' || c
== '+')
912 if (iswdigit_l(c
, loc
))
921 ssize_t diff
= (p
- b
);
922 ssize_t com
= (commit
- b
);
924 b
= (wchar_t *)reallocf(b
, s
* sizeof(wchar_t));
941 __ungetwc(c
, fp
, loc
);
943 __ungetwc(*p
, fp
, loc
);