]>
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 * 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
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
36 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.17 2009/01/19 06:19:51 das Exp $");
41 #include "namespace.h"
52 #include "un-namespace.h"
54 #include "libc_private.h"
57 #ifndef NO_FLOATING_POINT
61 #define BUF 513 /* Maximum length of numeric string. */
64 * Flags used during conversion.
66 #define LONG 0x01 /* l: long or double */
67 #define LONGDBL 0x02 /* L: long double */
68 #define SHORT 0x04 /* h: short */
69 #define SUPPRESS 0x08 /* *: suppress assignment */
70 #define POINTER 0x10 /* p: void * (as hex) */
71 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
72 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
73 #define INTMAXT 0x800 /* j: intmax_t */
74 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
75 #define SIZET 0x2000 /* z: size_t */
76 #define SHORTSHORT 0x4000 /* hh: char */
77 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
80 * The following are used in integral conversions only:
81 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
83 #define SIGNOK 0x40 /* +/- is (still) legal */
84 #define NDIGITS 0x80 /* no digits detected */
85 #define PFXOK 0x100 /* 0x prefix is (still) legal */
86 #define NZDIGITS 0x200 /* no zero digits detected */
87 #define HAVESIGN 0x10000 /* sign detected */
92 #define CT_CHAR 0 /* %c conversion */
93 #define CT_CCL 1 /* %[...] conversion */
94 #define CT_STRING 2 /* %s conversion */
95 #define CT_INT 3 /* %[dioupxX] conversion */
96 #define CT_FLOAT 4 /* %[efgEFG] conversion */
98 #ifndef NO_FLOATING_POINT
99 static int parsefloat(FILE *, wchar_t *, wchar_t *);
103 (cclcompl ? (wmemchr(ccls, (_c), ccle - ccls) == NULL) : \
104 (wmemchr(ccls, (_c), ccle - ccls) != NULL))
106 static const mbstate_t initial_mbs
;
112 vfwscanf(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
118 ret
= __vfwscanf(fp
, fmt
, ap
);
124 * Non-MT-safe version.
127 __vfwscanf(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
129 wint_t c
; /* character from format, or conversion */
130 size_t width
; /* field width, or 0 */
131 wchar_t *p
; /* points into all kinds of strings */
132 int n
; /* handy integer */
133 int flags
; /* flags as defined above */
134 wchar_t *p0
; /* saves original value of p when necessary */
135 int nassigned
; /* number of fields assigned */
136 int nconversions
; /* number of conversions */
137 int nread
; /* number of characters consumed from fp */
138 int base
; /* base argument to conversion function */
139 wchar_t buf
[BUF
]; /* buffer for numeric conversions */
140 const wchar_t *ccls
; /* character class start */
141 const wchar_t *ccle
; /* character class end */
142 int cclcompl
; /* ccl is complemented? */
143 wint_t wi
; /* handy wint_t */
144 char *mbp
; /* multibyte string pointer for %c %s %[ */
145 size_t nconv
; /* number of bytes in mb. conversion */
146 char mbbuf
[MB_LEN_MAX
]; /* temporary mb. character buffer */
149 /* `basefix' is used to avoid `if' tests in the integer scanner */
150 static short basefix
[17] =
151 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
162 while ((c
= __fgetwc(fp
)) != WEOF
&&
174 * switch on the format. continue if done;
175 * break once format type is derived.
181 if ((wi
= __fgetwc(fp
)) == WEOF
)
204 flags
|= LONGLONG
; /* not quite */
223 case '0': case '1': case '2': case '3': case '4':
224 case '5': case '6': case '7': case '8': case '9':
225 width
= width
* 10 + c
- '0';
255 flags
|= PFXOK
; /* enable 0x prefixing */
261 #ifndef NO_FLOATING_POINT
262 case 'A': case 'E': case 'F': case 'G':
263 case 'a': case 'e': case 'f': case 'g':
284 while (*fmt
!= '\0' && *fmt
!= ']')
300 case 'p': /* pointer format is like hex */
301 flags
|= POINTER
| PFXOK
;
302 c
= CT_INT
; /* assumes sizeof(uintmax_t) */
303 flags
|= UNSIGNED
; /* >= sizeof(uintptr_t) */
309 if (flags
& SUPPRESS
) /* ??? */
311 if (flags
& SHORTSHORT
)
312 *va_arg(ap
, char *) = nread
;
313 else if (flags
& SHORT
)
314 *va_arg(ap
, short *) = nread
;
315 else if (flags
& LONG
)
316 *va_arg(ap
, long *) = nread
;
317 else if (flags
& LONGLONG
)
318 *va_arg(ap
, long long *) = nread
;
319 else if (flags
& INTMAXT
)
320 *va_arg(ap
, intmax_t *) = nread
;
321 else if (flags
& SIZET
)
322 *va_arg(ap
, size_t *) = nread
;
323 else if (flags
& PTRDIFFT
)
324 *va_arg(ap
, ptrdiff_t *) = nread
;
326 *va_arg(ap
, int *) = nread
;
333 * Disgusting backwards compatibility hack. XXX
335 case '\0': /* compat */
340 * Consume leading white space, except for formats
341 * that suppress this.
343 if ((flags
& NOSKIP
) == 0) {
344 while ((wi
= __fgetwc(fp
)) != WEOF
&& iswspace(wi
))
357 /* scan arbitrary characters (sets NOSKIP) */
361 if (!(flags
& SUPPRESS
))
362 p
= va_arg(ap
, wchar_t *);
364 while (width
-- != 0 &&
365 (wi
= __fgetwc(fp
)) != WEOF
) {
366 if (!(flags
& SUPPRESS
))
373 if (!(flags
& SUPPRESS
))
376 if (!(flags
& SUPPRESS
))
377 mbp
= va_arg(ap
, char *);
381 (wi
= __fgetwc(fp
)) != WEOF
) {
382 if (width
>= MB_CUR_MAX
&&
383 !(flags
& SUPPRESS
)) {
384 nconv
= wcrtomb(mbp
, wi
, &mbs
);
385 if (nconv
== (size_t)-1)
388 nconv
= wcrtomb(mbbuf
, wi
,
390 if (nconv
== (size_t)-1)
396 if (!(flags
& SUPPRESS
))
400 if (!(flags
& SUPPRESS
))
408 if (!(flags
& SUPPRESS
))
415 /* scan a (nonempty) character class (sets NOSKIP) */
417 width
= (size_t)~0; /* `infinity' */
418 /* take only those things in the class */
419 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
421 while ((wi
= __fgetwc(fp
)) != WEOF
&&
422 width
-- != 0 && INCCL(wi
))
428 } else if (flags
& LONG
) {
429 p0
= p
= va_arg(ap
, wchar_t *);
430 while ((wi
= __fgetwc(fp
)) != WEOF
&&
431 width
-- != 0 && INCCL(wi
))
441 if (!(flags
& SUPPRESS
))
442 mbp
= va_arg(ap
, char *);
445 while ((wi
= __fgetwc(fp
)) != WEOF
&&
446 width
!= 0 && INCCL(wi
)) {
447 if (width
>= MB_CUR_MAX
&&
448 !(flags
& SUPPRESS
)) {
449 nconv
= wcrtomb(mbp
, wi
, &mbs
);
450 if (nconv
== (size_t)-1)
453 nconv
= wcrtomb(mbbuf
, wi
,
455 if (nconv
== (size_t)-1)
459 if (!(flags
& SUPPRESS
))
463 if (!(flags
& SUPPRESS
))
470 if (!(flags
& SUPPRESS
)) {
480 /* like CCL, but zero-length string OK, & no NOSKIP */
483 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
484 while ((wi
= __fgetwc(fp
)) != WEOF
&&
490 } else if (flags
& LONG
) {
491 p0
= p
= va_arg(ap
, wchar_t *);
492 while ((wi
= __fgetwc(fp
)) != WEOF
&&
503 if (!(flags
& SUPPRESS
))
504 mbp
= va_arg(ap
, char *);
506 while ((wi
= __fgetwc(fp
)) != WEOF
&&
509 if (width
>= MB_CUR_MAX
&&
510 !(flags
& SUPPRESS
)) {
511 nconv
= wcrtomb(mbp
, wi
, &mbs
);
512 if (nconv
== (size_t)-1)
515 nconv
= wcrtomb(mbbuf
, wi
,
517 if (nconv
== (size_t)-1)
521 if (!(flags
& SUPPRESS
))
525 if (!(flags
& SUPPRESS
))
532 if (!(flags
& SUPPRESS
)) {
541 /* scan an integer as if by the conversion function */
542 if (width
== 0 || width
> sizeof(buf
) /
544 width
= sizeof(buf
) / sizeof(*buf
) - 1;
545 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
546 for (p
= buf
; width
; width
--) {
549 * Switch on the character; `goto ok'
550 * if we accept it as a part of number.
555 * The digit 0 is always legal, but is
556 * special. For %i conversions, if no
557 * digits (zero or nonzero) have been
558 * scanned (only signs), we will have
559 * base==0. In that case, we should set
560 * it to 8 and enable 0x prefixing.
561 * Also, if we have not scanned zero digits
562 * before this, do not turn off prefixing
563 * (someone else will turn it off if we
564 * have scanned any nonzero digits).
571 if (flags
& NZDIGITS
)
572 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
574 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
577 /* 1 through 7 always legal */
578 case '1': case '2': case '3':
579 case '4': case '5': case '6': case '7':
580 base
= basefix
[base
];
581 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
584 /* digits 8 and 9 ok iff decimal or hex */
586 base
= basefix
[base
];
588 break; /* not legal here */
589 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
592 /* letters ok iff hex */
593 case 'A': case 'B': case 'C':
594 case 'D': case 'E': case 'F':
595 case 'a': case 'b': case 'c':
596 case 'd': case 'e': case 'f':
597 /* no need to fix base here */
599 break; /* not legal here */
600 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
603 /* sign ok only as first character */
605 if (flags
& SIGNOK
) {
613 * x ok iff flag still set & 2nd char (or
614 * 3rd char if we have a sign).
617 if (flags
& PFXOK
&& p
==
618 buf
+ 1 + !!(flags
& HAVESIGN
)) {
619 base
= 16; /* if %i */
627 * If we got here, c is not a legal character
628 * for a number. Stop accumulating digits.
635 * c is legal: store it and look at the next.
640 * If we had only a sign, it is no good; push
641 * back the sign. If the number ends in `x',
642 * it was [sign] '0' 'x', so push back the x
643 * and treat it as [sign] '0'.
645 if (flags
& NDIGITS
) {
651 if (c
== 'x' || c
== 'X') {
655 if ((flags
& SUPPRESS
) == 0) {
659 if ((flags
& UNSIGNED
) == 0)
660 res
= wcstoimax(buf
, NULL
, base
);
662 res
= wcstoumax(buf
, NULL
, base
);
664 *va_arg(ap
, void **) =
665 (void *)(uintptr_t)res
;
666 else if (flags
& SHORTSHORT
)
667 *va_arg(ap
, char *) = res
;
668 else if (flags
& SHORT
)
669 *va_arg(ap
, short *) = res
;
670 else if (flags
& LONG
)
671 *va_arg(ap
, long *) = res
;
672 else if (flags
& LONGLONG
)
673 *va_arg(ap
, long long *) = res
;
674 else if (flags
& INTMAXT
)
675 *va_arg(ap
, intmax_t *) = res
;
676 else if (flags
& PTRDIFFT
)
677 *va_arg(ap
, ptrdiff_t *) = res
;
678 else if (flags
& SIZET
)
679 *va_arg(ap
, size_t *) = res
;
681 *va_arg(ap
, int *) = res
;
688 #ifndef NO_FLOATING_POINT
690 /* scan a floating point number as if by strtod */
691 if (width
== 0 || width
> sizeof(buf
) /
693 width
= sizeof(buf
) / sizeof(*buf
) - 1;
694 if ((width
= parsefloat(fp
, buf
, buf
+ width
)) == 0)
696 if ((flags
& SUPPRESS
) == 0) {
697 if (flags
& LONGDBL
) {
698 long double res
= wcstold(buf
, &p
);
699 *va_arg(ap
, long double *) = res
;
700 } else if (flags
& LONG
) {
701 double res
= wcstod(buf
, &p
);
702 *va_arg(ap
, double *) = res
;
704 float res
= wcstof(buf
, &p
);
705 *va_arg(ap
, float *) = res
;
712 #endif /* !NO_FLOATING_POINT */
716 return (nconversions
!= 0 ? nassigned
: EOF
);
721 #ifndef NO_FLOATING_POINT
723 parsefloat(FILE *fp
, wchar_t *buf
, wchar_t *end
)
730 S_START
, S_GOTSIGN
, S_INF
, S_NAN
, S_DONE
, S_MAYBEHEX
,
731 S_DIGITS
, S_FRAC
, S_EXP
, S_EXPDIGITS
735 _Bool gotmantdig
= 0, ishex
= 0;
738 nconv
= mbrtowc(&decpt
, localeconv()->decimal_point
, MB_CUR_MAX
, &mbs
);
739 if (nconv
== (size_t)-1 || nconv
== (size_t)-2)
740 decpt
= '.'; /* failsafe */
743 * We set commit = p whenever the string we have read so far
744 * constitutes a valid representation of a floating point
745 * number by itself. At some point, the parse will complete
746 * or fail, and we will ungetc() back to the last commit point.
747 * To ensure that the file offset gets updated properly, it is
748 * always necessary to read at least one character that doesn't
749 * match; thus, we can't short-circuit "infinity" or "nan(...)".
753 for (p
= buf
; p
< end
; ) {
754 if ((c
= __fgetwc(fp
)) == WEOF
)
760 if (c
== '-' || c
== '+')
785 (c
!= "nfinity"[infnanpos
] &&
786 c
!= "NFINITY"[infnanpos
]))
788 if (infnanpos
== 1 || infnanpos
== 6)
789 commit
= p
; /* inf or infinity */
795 if (c
!= 'A' && c
!= 'a')
799 if (c
!= 'N' && c
!= 'n')
812 } else if (!iswalnum(c
) && c
!= '_')
822 if (c
== 'X' || c
== 'x') {
825 } else { /* we saw a '0', but no 'x' */
830 if ((ishex
&& iswxdigit(c
)) || iswdigit(c
))
841 if (((c
== 'E' || c
== 'e') && !ishex
) ||
842 ((c
== 'P' || c
== 'p') && ishex
)) {
847 } else if ((ishex
&& iswxdigit(c
)) || iswdigit(c
)) {
855 if (c
== '-' || c
== '+')
878 return (commit
- buf
);