]>
git.saurik.com Git - apple/libc.git/blob - stdio/vfscanf.c
70807363ad9019a4c4325e61c08e95c8dc43e494
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1993
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 #define FLOATING_POINT
75 #define BUF 513 /* Maximum length of numeric string. */
78 * Flags used during conversion.
80 #define LONG 0x01 /* l: long or double */
81 #define LONGDBL 0x02 /* L: long double; unimplemented */
82 #define SHORT 0x04 /* h: short */
83 #define SUPPRESS 0x08 /* suppress assignment */
84 #define POINTER 0x10 /* weird %p pointer (`fake hex') */
85 #define NOSKIP 0x20 /* do not skip blanks */
89 * The following are used in numeric conversions only:
90 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
91 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
93 #define SIGNOK 0x40 /* +/- is (still) legal */
94 #define NDIGITS 0x80 /* no digits detected */
96 #define DPTOK 0x100 /* (float) decimal point is still legal */
97 #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
99 #define PFXOK 0x100 /* 0x prefix is (still) legal */
100 #define NZDIGITS 0x200 /* no zero digits detected */
105 #define CT_CHAR 0 /* %c conversion */
106 #define CT_CCL 1 /* %[...] conversion */
107 #define CT_STRING 2 /* %s conversion */
108 #define CT_INT 3 /* integer, i.e., strtoq or strtouq */
109 #define CT_FLOAT 4 /* floating, i.e., strtod */
111 #define u_char unsigned char
112 #define u_long unsigned long
114 static u_char
*__sccl(char *, u_char
*);
120 __svfscanf(fp
, fmt0
, ap
)
125 register u_char
*fmt
= (u_char
*)fmt0
;
126 register int c
; /* character from format, or conversion */
127 register size_t width
; /* field width, or 0 */
128 register char *p
; /* points into all kinds of strings */
129 register int n
; /* handy integer */
130 register int flags
; /* flags as defined above */
131 register char *p0
; /* saves original value of p when necessary */
132 int nassigned
; /* number of fields assigned */
133 int nread
; /* number of characters consumed from fp */
134 int base
; /* base argument to strtoq/strtouq */
135 u_quad_t (*ccfn
)(); /* conversion function (strtoq/strtouq) */
136 char ccltab
[256]; /* character class table for %[...] */
137 char buf
[BUF
]; /* buffer for numeric conversions */
139 /* `basefix' is used to avoid `if' tests in the integer scanner */
140 static short basefix
[17] =
141 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
145 base
= 0; /* XXX just to keep gcc happy */
146 ccfn
= NULL
; /* XXX just to keep gcc happy */
153 if (fp
->_r
<= 0 && __srefill(fp
))
155 if (!isspace(*fp
->_p
))
157 nread
++, fp
->_r
--, fp
->_p
++;
166 * switch on the format. continue if done;
167 * break once format type is derived.
173 if (fp
->_r
<= 0 && __srefill(fp
))
194 if( sizeof(size_t) == sizeof(long) )
196 if( sizeof(size_t) == sizeof(quad_t
) )
206 case '0': case '1': case '2': case '3': case '4':
207 case '5': case '6': case '7': case '8': case '9':
208 width
= width
* 10 + c
- '0';
213 * Those marked `compat' are for 4.[123]BSD compatibility.
215 * (According to ANSI, E and X formats are supposed
216 * to the same as e and x. Sorry about that.)
218 case 'D': /* compat */
223 ccfn
= (u_quad_t (*)())strtoq
;
229 ccfn
= (u_quad_t (*)())strtoq
;
233 case 'O': /* compat */
248 case 'X': /* compat XXX */
252 flags
|= PFXOK
; /* enable 0x prefixing */
258 #ifdef FLOATING_POINT
259 case 'E': /* compat XXX */
260 case 'F': /* compat */
263 case 'e': case 'f': case 'g':
273 fmt
= __sccl(ccltab
, fmt
);
283 case 'p': /* pointer format is like hex */
284 flags
|= POINTER
| PFXOK
;
291 if (flags
& SUPPRESS
) /* ??? */
294 *va_arg(ap
, short *) = nread
;
295 else if (flags
& LONG
)
296 *va_arg(ap
, long *) = nread
;
297 else if (flags
& QUAD
)
298 *va_arg(ap
, quad_t
*) = nread
;
300 *va_arg(ap
, int *) = nread
;
304 * Disgusting backwards compatibility hacks. XXX
306 case '\0': /* compat */
309 default: /* compat */
313 ccfn
= (u_quad_t (*)())strtoq
;
319 * We have a conversion that requires input.
321 if (fp
->_r
<= 0 && __srefill(fp
))
325 * Consume leading white space, except for formats
326 * that suppress this.
328 if ((flags
& NOSKIP
) == 0) {
329 while (isspace(*fp
->_p
)) {
333 else if (__srefill(fp
))
337 * Note that there is at least one character in
338 * the buffer, so conversions that do not set NOSKIP
339 * ca no longer result in an input failure.
349 /* scan arbitrary characters (sets NOSKIP) */
352 if (flags
& SUPPRESS
) {
355 if ((n
= fp
->_r
) < width
) {
373 size_t r
= fread((void *)va_arg(ap
, char *), 1,
384 /* scan a (nonempty) character class (sets NOSKIP) */
386 width
= ~0; /* `infinity' */
387 /* take only those things in the class */
388 if (flags
& SUPPRESS
) {
390 while (ccltab
[*fp
->_p
]) {
391 n
++, fp
->_r
--, fp
->_p
++;
394 if (fp
->_r
<= 0 && __srefill(fp
)) {
403 p0
= p
= va_arg(ap
, char *);
404 while (ccltab
[*fp
->_p
]) {
409 if (fp
->_r
<= 0 && __srefill(fp
)) {
425 /* like CCL, but zero-length string OK, & no NOSKIP */
428 if (flags
& SUPPRESS
) {
430 while (!isspace(*fp
->_p
)) {
431 n
++, fp
->_r
--, fp
->_p
++;
434 if (fp
->_r
<= 0 && __srefill(fp
))
439 p0
= p
= va_arg(ap
, char *);
440 while (!isspace(*fp
->_p
)) {
445 if (fp
->_r
<= 0 && __srefill(fp
))
455 /* scan an integer as if by strtoq/strtouq */
457 if (width
== 0 || width
> sizeof(buf
) - 1)
458 width
= sizeof(buf
) - 1;
460 /* size_t is unsigned, hence this optimisation */
461 if (--width
> sizeof(buf
) - 2)
462 width
= sizeof(buf
) - 2;
465 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
466 for (p
= buf
; width
; width
--) {
469 * Switch on the character; `goto ok'
470 * if we accept it as a part of number.
475 * The digit 0 is always legal, but is
476 * special. For %i conversions, if no
477 * digits (zero or nonzero) have been
478 * scanned (only signs), we will have
479 * base==0. In that case, we should set
480 * it to 8 and enable 0x prefixing.
481 * Also, if we have not scanned zero digits
482 * before this, do not turn off prefixing
483 * (someone else will turn it off if we
484 * have scanned any nonzero digits).
491 if (flags
& NZDIGITS
)
492 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
494 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
497 /* 1 through 7 always legal */
498 case '1': case '2': case '3':
499 case '4': case '5': case '6': case '7':
500 base
= basefix
[base
];
501 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
504 /* digits 8 and 9 ok iff decimal or hex */
506 base
= basefix
[base
];
508 break; /* not legal here */
509 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
512 /* letters ok iff hex */
513 case 'A': case 'B': case 'C':
514 case 'D': case 'E': case 'F':
515 case 'a': case 'b': case 'c':
516 case 'd': case 'e': case 'f':
517 /* no need to fix base here */
519 break; /* not legal here */
520 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
523 /* sign ok only as first character */
525 if (flags
& SIGNOK
) {
531 /* x ok iff flag still set & 2nd char */
533 if (flags
& PFXOK
&& p
== buf
+ 1) {
534 base
= 16; /* if %i */
542 * If we got here, c is not a legal character
543 * for a number. Stop accumulating digits.
548 * c is legal: store it and look at the next.
553 else if (__srefill(fp
))
557 * If we had only a sign, it is no good; push
558 * back the sign. If the number ends in `x',
559 * it was [sign] '0' 'x', so push back the x
560 * and treat it as [sign] '0'.
562 if (flags
& NDIGITS
) {
564 (void) ungetc(*(u_char
*)--p
, fp
);
567 c
= ((u_char
*)p
)[-1];
568 if (c
== 'x' || c
== 'X') {
570 (void) ungetc(c
, fp
);
572 if ((flags
& SUPPRESS
) == 0) {
576 res
= (*ccfn
)(buf
, (char **)NULL
, base
);
578 *va_arg(ap
, void **) = (void *)res
;
579 else if (flags
& SHORT
)
580 *va_arg(ap
, short *) = res
;
581 else if (flags
& QUAD
)
582 *va_arg(ap
, quad_t
*) = res
;
583 else if (flags
& LONG
)
584 *va_arg(ap
, long *) = res
;
586 *va_arg(ap
, int *) = res
;
592 #ifdef FLOATING_POINT
594 /* scan a floating point number as if by strtod */
596 if (width
== 0 || width
> sizeof(buf
) - 1)
597 width
= sizeof(buf
) - 1;
599 /* size_t is unsigned, hence this optimisation */
600 if (--width
> sizeof(buf
) - 2)
601 width
= sizeof(buf
) - 2;
604 flags
|= SIGNOK
| NDIGITS
| DPTOK
| EXPOK
;
605 for (p
= buf
; width
; width
--) {
608 * This code mimicks the integer conversion
609 * code, but is much simpler.
613 case '0': case '1': case '2': case '3':
614 case '4': case '5': case '6': case '7':
616 flags
&= ~(SIGNOK
| NDIGITS
);
620 if (flags
& SIGNOK
) {
627 flags
&= ~(SIGNOK
| DPTOK
);
632 /* no exponent without some digits */
633 if ((flags
&(NDIGITS
|EXPOK
)) == EXPOK
) {
635 (flags
& ~(EXPOK
|DPTOK
)) |
646 else if (__srefill(fp
))
650 * If no digits, might be missing exponent digits
651 * (just give back the exponent) or might be missing
652 * regular digits, but had sign and/or decimal point.
654 if (flags
& NDIGITS
) {
656 /* no digits at all */
658 ungetc(*(u_char
*)--p
, fp
);
661 /* just a bad exponent (e and maybe sign) */
663 if (c
!= 'e' && c
!= 'E') {
664 (void) ungetc(c
, fp
);/* sign */
667 (void) ungetc(c
, fp
);
669 if ((flags
& SUPPRESS
) == 0) {
673 res
= strtod(buf
,(char **) NULL
);
675 *va_arg(ap
, long double *) = res
;
676 else if (flags
& LONG
)
677 *va_arg(ap
, double *) = res
;
679 *va_arg(ap
, float *) = res
;
684 #endif /* FLOATING_POINT */
688 return (nassigned
? nassigned
: -1);
694 * Fill in the given table from the scanset at the given format
695 * (just after `['). Return a pointer to the character past the
696 * closing `]'. The table has a 1 wherever characters should be
697 * considered part of the scanset.
702 register u_char
*fmt
;
704 register int c
, n
, v
;
706 /* first `clear' the whole table */
707 c
= *fmt
++; /* first char hat => negated scanset */
709 v
= 1; /* default => accept */
710 c
= *fmt
++; /* get new first char */
712 v
= 0; /* default => reject */
713 /* should probably use memset here */
714 for (n
= 0; n
< 256; n
++)
717 return (fmt
- 1);/* format ended before closing ] */
720 * Now set the entries corresponding to the actual scanset
721 * to the opposite of the above.
723 * The first character may be ']' (or '-') without being special;
724 * the last character may be '-'.
728 tab
[c
] = v
; /* take character c */
730 n
= *fmt
++; /* and examine the next */
733 case 0: /* format ended too soon */
738 * A scanset of the form
740 * is defined as `the digit 0, the digit 1,
741 * the character +, the character -', but
742 * the effect of a scanset such as
744 * is implementation defined. The V7 Unix
745 * scanf treats `a-z' as `the letters a through
746 * z', but treats `a-a' as `the letter a, the
747 * character -, and the letter a'.
749 * For compatibility, the `-' is not considerd
750 * to define a range if the character following
751 * it is either a close bracket (required by ANSI)
752 * or is not numerically greater than the character
753 * we just stored in the table (c).
756 if (n
== ']' || n
< c
) {
758 break; /* resume the for(;;) */
761 do { /* fill in the range */
764 #if 1 /* XXX another disgusting compatibility hack */
766 * Alas, the V7 Unix scanf also treats formats
767 * such as [a-c-e] as `the letters a through e'.
768 * This too is permitted by the standard....
780 case ']': /* end of scanset */
783 default: /* just another character */