]>
git.saurik.com Git - apple/xnu.git/blob - libkern/stdio/scanf.c
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1990, 1993
32 * The Regents of the University of California. All rights reserved.
34 * This code is derived from software contributed to Berkeley by
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 #include <sys/cdefs.h>
68 #if 0 /* XXX coming soon */
74 return (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\12');
79 #include <sys/param.h>
80 #include <sys/systm.h>
82 #define BUF 32 /* Maximum length of numeric string. */
85 * Flags used during conversion.
87 #define LONG 0x01 /* l: long or double */
88 #define SHORT 0x04 /* h: short */
89 #define SUPPRESS 0x08 /* *: suppress assignment */
90 #define POINTER 0x10 /* p: void * (as hex) */
91 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
92 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
93 #define SHORTSHORT 0x4000 /* hh: char */
94 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
97 * The following are used in numeric conversions only:
98 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
99 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
101 #define SIGNOK 0x40 /* +/- is (still) legal */
102 #define NDIGITS 0x80 /* no digits detected */
104 #define DPTOK 0x100 /* (float) decimal point is still legal */
105 #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
107 #define PFXOK 0x100 /* 0x prefix is (still) legal */
108 #define NZDIGITS 0x200 /* no zero digits detected */
113 #define CT_CHAR 0 /* %c conversion */
114 #define CT_CCL 1 /* %[...] conversion */
115 #define CT_STRING 2 /* %s conversion */
116 #define CT_INT 3 /* %[dioupxX] conversion */
118 static const u_char
*__sccl(char *, const u_char
*);
121 sscanf(const char *ibuf
, const char *fmt
, ...)
127 ret
= vsscanf(ibuf
, fmt
, ap
);
133 vsscanf(const char *inp
, char const *fmt0
, va_list ap
)
136 const u_char
*fmt
= (const u_char
*)fmt0
;
137 int c
; /* character from format, or conversion */
138 size_t width
; /* field width, or 0 */
139 char *p
; /* points into all kinds of strings */
140 int n
; /* handy integer */
141 int flags
; /* flags as defined above */
142 char *p0
; /* saves original value of p when necessary */
143 int nassigned
; /* number of fields assigned */
144 int nconversions
; /* number of conversions */
145 int nread
; /* number of characters consumed from fp */
146 int base
; /* base argument to conversion function */
147 char ccltab
[256]; /* character class table for %[...] */
148 char buf
[BUF
]; /* buffer for numeric conversions */
150 /* `basefix' is used to avoid `if' tests in the integer scanner */
151 static short basefix
[17] =
152 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
159 base
= 0; /* XXX just to keep gcc happy */
165 while (inr
> 0 && isspace(*inp
))
166 nread
++, inr
--, inp
++;
174 * switch on the format. continue if done;
175 * break once format type is derived.
200 flags
|= LONGLONG
; /* not quite */
210 case '0': case '1': case '2': case '3': case '4':
211 case '5': case '6': case '7': case '8': case '9':
212 width
= width
* 10 + c
- '0';
242 flags
|= PFXOK
; /* enable 0x prefixing */
253 fmt
= __sccl(ccltab
, fmt
);
263 case 'p': /* pointer format is like hex */
264 flags
|= POINTER
| PFXOK
;
272 if (flags
& SUPPRESS
) /* ??? */
274 if (flags
& SHORTSHORT
)
275 *va_arg(ap
, char *) = nread
;
276 else if (flags
& SHORT
)
277 *va_arg(ap
, short *) = nread
;
278 else if (flags
& LONG
)
279 *va_arg(ap
, long *) = nread
;
280 else if (flags
& LONGLONG
)
281 *va_arg(ap
, long long *) = nread
;
283 *va_arg(ap
, int *) = nread
;
288 * We have a conversion that requires input.
294 * Consume leading white space, except for formats
295 * that suppress this.
297 if ((flags
& NOSKIP
) == 0) {
298 while (isspace(*inp
)) {
306 * Note that there is at least one character in
307 * the buffer, so conversions that do not set NOSKIP
308 * can no longer result in an input failure.
318 /* scan arbitrary characters (sets NOSKIP) */
321 if (flags
& SUPPRESS
) {
324 if ((n
= inr
) < (int)width
) {
340 bcopy(inp
, va_arg(ap
, char *), width
);
350 /* scan a (nonempty) character class (sets NOSKIP) */
352 width
= (size_t)~0; /* `infinity' */
353 /* take only those things in the class */
354 if (flags
& SUPPRESS
) {
356 while (ccltab
[(unsigned char)*inp
]) {
369 p0
= p
= va_arg(ap
, char *);
370 while (ccltab
[(unsigned char)*inp
]) {
392 /* like CCL, but zero-length string OK, & no NOSKIP */
395 if (flags
& SUPPRESS
) {
397 while (!isspace(*inp
)) {
406 p0
= p
= va_arg(ap
, char *);
407 while (!isspace(*inp
)) {
423 /* scan an integer as if by the conversion function */
425 if (width
== 0 || width
> sizeof(buf
) - 1)
426 width
= sizeof(buf
) - 1;
428 /* size_t is unsigned, hence this optimisation */
429 if (--width
> sizeof(buf
) - 2)
430 width
= sizeof(buf
) - 2;
433 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
434 for (p
= buf
; width
; width
--) {
437 * Switch on the character; `goto ok'
438 * if we accept it as a part of number.
443 * The digit 0 is always legal, but is
444 * special. For %i conversions, if no
445 * digits (zero or nonzero) have been
446 * scanned (only signs), we will have
447 * base==0. In that case, we should set
448 * it to 8 and enable 0x prefixing.
449 * Also, if we have not scanned zero digits
450 * before this, do not turn off prefixing
451 * (someone else will turn it off if we
452 * have scanned any nonzero digits).
459 if (flags
& NZDIGITS
)
460 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
462 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
465 /* 1 through 7 always legal */
466 case '1': case '2': case '3':
467 case '4': case '5': case '6': case '7':
468 base
= basefix
[base
];
469 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
472 /* digits 8 and 9 ok iff decimal or hex */
474 base
= basefix
[base
];
476 break; /* not legal here */
477 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
480 /* letters ok iff hex */
481 case 'A': case 'B': case 'C':
482 case 'D': case 'E': case 'F':
483 case 'a': case 'b': case 'c':
484 case 'd': case 'e': case 'f':
485 /* no need to fix base here */
487 break; /* not legal here */
488 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
491 /* sign ok only as first character */
493 if (flags
& SIGNOK
) {
499 /* x ok iff flag still set & 2nd char */
501 if (flags
& PFXOK
&& p
== buf
+ 1) {
502 base
= 16; /* if %i */
510 * If we got here, c is not a legal character
511 * for a number. Stop accumulating digits.
516 * c is legal: store it and look at the next.
522 break; /* end of input */
525 * If we had only a sign, it is no good; push
526 * back the sign. If the number ends in `x',
527 * it was [sign] '0' 'x', so push back the x
528 * and treat it as [sign] '0'.
530 if (flags
& NDIGITS
) {
537 c
= ((u_char
*)p
)[-1];
538 if (c
== 'x' || c
== 'X') {
543 if ((flags
& SUPPRESS
) == 0) {
547 if ((flags
& UNSIGNED
) == 0)
548 res
= strtoq(buf
, (char **)NULL
, base
);
550 res
= strtouq(buf
, (char **)NULL
, base
);
552 *va_arg(ap
, void **) =
553 (void *)(uintptr_t)res
;
554 else if (flags
& SHORTSHORT
)
555 *va_arg(ap
, char *) = res
;
556 else if (flags
& SHORT
)
557 *va_arg(ap
, short *) = res
;
558 else if (flags
& LONG
)
559 *va_arg(ap
, long *) = res
;
560 else if (flags
& LONGLONG
)
561 *va_arg(ap
, long long *) = res
;
563 *va_arg(ap
, int *) = res
;
573 return (nconversions
!= 0 ? nassigned
: -1);
579 * Fill in the given table from the scanset at the given format
580 * (just after `['). Return a pointer to the character past the
581 * closing `]'. The table has a 1 wherever characters should be
582 * considered part of the scanset.
584 static const u_char
*
585 __sccl(char *tab
, const u_char
*fmt
)
589 /* first `clear' the whole table */
590 c
= *fmt
++; /* first char hat => negated scanset */
592 v
= 1; /* default => accept */
593 c
= *fmt
++; /* get new first char */
595 v
= 0; /* default => reject */
597 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
598 (void) memset(tab
, v
, 256);
601 return (fmt
- 1);/* format ended before closing ] */
604 * Now set the entries corresponding to the actual scanset
605 * to the opposite of the above.
607 * The first character may be ']' (or '-') without being special;
608 * the last character may be '-'.
612 tab
[c
] = v
; /* take character c */
614 n
= *fmt
++; /* and examine the next */
617 case 0: /* format ended too soon */
622 * A scanset of the form
624 * is defined as `the digit 0, the digit 1,
625 * the character +, the character -', but
626 * the effect of a scanset such as
628 * is implementation defined. The V7 Unix
629 * scanf treats `a-z' as `the letters a through
630 * z', but treats `a-a' as `the letter a, the
631 * character -, and the letter a'.
633 * For compatibility, the `-' is not considerd
634 * to define a range if the character following
635 * it is either a close bracket (required by ANSI)
636 * or is not numerically greater than the character
637 * we just stored in the table (c).
640 if (n
== ']' || n
< c
) {
642 break; /* resume the for(;;) */
645 /* fill in the range */
651 * Alas, the V7 Unix scanf also treats formats
652 * such as [a-c-e] as `the letters a through e'.
653 * This too is permitted by the standard....
658 case ']': /* end of scanset */
661 default: /* just another character */