]>
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_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1990, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/cdefs.h>
61 #if 0 /* XXX coming soon */
67 return (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\12');
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #define BUF 32 /* Maximum length of numeric string. */
78 * Flags used during conversion.
80 #define LONG 0x01 /* l: long or double */
81 #define SHORT 0x04 /* h: short */
82 #define SUPPRESS 0x08 /* *: suppress assignment */
83 #define POINTER 0x10 /* p: void * (as hex) */
84 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
85 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
86 #define SHORTSHORT 0x4000 /* hh: char */
87 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
90 * The following are used in numeric conversions only:
91 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
92 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
94 #define SIGNOK 0x40 /* +/- is (still) legal */
95 #define NDIGITS 0x80 /* no digits detected */
97 #define DPTOK 0x100 /* (float) decimal point is still legal */
98 #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
100 #define PFXOK 0x100 /* 0x prefix is (still) legal */
101 #define NZDIGITS 0x200 /* no zero digits detected */
106 #define CT_CHAR 0 /* %c conversion */
107 #define CT_CCL 1 /* %[...] conversion */
108 #define CT_STRING 2 /* %s conversion */
109 #define CT_INT 3 /* %[dioupxX] conversion */
111 static const u_char
*__sccl(char *, const u_char
*);
114 sscanf(const char *ibuf
, const char *fmt
, ...)
120 ret
= vsscanf(ibuf
, fmt
, ap
);
126 vsscanf(const char *inp
, char const *fmt0
, va_list ap
)
129 const u_char
*fmt
= (const u_char
*)fmt0
;
130 int c
; /* character from format, or conversion */
131 size_t width
; /* field width, or 0 */
132 char *p
; /* points into all kinds of strings */
133 int n
; /* handy integer */
134 int flags
; /* flags as defined above */
135 char *p0
; /* saves original value of p when necessary */
136 int nassigned
; /* number of fields assigned */
137 int nconversions
; /* number of conversions */
138 int nread
; /* number of characters consumed from fp */
139 int base
; /* base argument to conversion function */
140 char ccltab
[256]; /* character class table for %[...] */
141 char buf
[BUF
]; /* buffer for numeric conversions */
143 /* `basefix' is used to avoid `if' tests in the integer scanner */
144 static short basefix
[17] =
145 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
152 base
= 0; /* XXX just to keep gcc happy */
158 while (inr
> 0 && isspace(*inp
))
159 nread
++, inr
--, inp
++;
167 * switch on the format. continue if done;
168 * break once format type is derived.
193 flags
|= LONGLONG
; /* not quite */
203 case '0': case '1': case '2': case '3': case '4':
204 case '5': case '6': case '7': case '8': case '9':
205 width
= width
* 10 + c
- '0';
235 flags
|= PFXOK
; /* enable 0x prefixing */
246 fmt
= __sccl(ccltab
, fmt
);
256 case 'p': /* pointer format is like hex */
257 flags
|= POINTER
| PFXOK
;
265 if (flags
& SUPPRESS
) /* ??? */
267 if (flags
& SHORTSHORT
)
268 *va_arg(ap
, char *) = nread
;
269 else if (flags
& SHORT
)
270 *va_arg(ap
, short *) = nread
;
271 else if (flags
& LONG
)
272 *va_arg(ap
, long *) = nread
;
273 else if (flags
& LONGLONG
)
274 *va_arg(ap
, long long *) = nread
;
276 *va_arg(ap
, int *) = nread
;
281 * We have a conversion that requires input.
287 * Consume leading white space, except for formats
288 * that suppress this.
290 if ((flags
& NOSKIP
) == 0) {
291 while (isspace(*inp
)) {
299 * Note that there is at least one character in
300 * the buffer, so conversions that do not set NOSKIP
301 * can no longer result in an input failure.
311 /* scan arbitrary characters (sets NOSKIP) */
314 if (flags
& SUPPRESS
) {
317 if ((n
= inr
) < (int)width
) {
333 bcopy(inp
, va_arg(ap
, char *), width
);
343 /* scan a (nonempty) character class (sets NOSKIP) */
345 width
= (size_t)~0; /* `infinity' */
346 /* take only those things in the class */
347 if (flags
& SUPPRESS
) {
349 while (ccltab
[(unsigned char)*inp
]) {
362 p0
= p
= va_arg(ap
, char *);
363 while (ccltab
[(unsigned char)*inp
]) {
385 /* like CCL, but zero-length string OK, & no NOSKIP */
388 if (flags
& SUPPRESS
) {
390 while (!isspace(*inp
)) {
399 p0
= p
= va_arg(ap
, char *);
400 while (!isspace(*inp
)) {
416 /* scan an integer as if by the conversion function */
418 if (width
== 0 || width
> sizeof(buf
) - 1)
419 width
= sizeof(buf
) - 1;
421 /* size_t is unsigned, hence this optimisation */
422 if (--width
> sizeof(buf
) - 2)
423 width
= sizeof(buf
) - 2;
426 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
427 for (p
= buf
; width
; width
--) {
430 * Switch on the character; `goto ok'
431 * if we accept it as a part of number.
436 * The digit 0 is always legal, but is
437 * special. For %i conversions, if no
438 * digits (zero or nonzero) have been
439 * scanned (only signs), we will have
440 * base==0. In that case, we should set
441 * it to 8 and enable 0x prefixing.
442 * Also, if we have not scanned zero digits
443 * before this, do not turn off prefixing
444 * (someone else will turn it off if we
445 * have scanned any nonzero digits).
452 if (flags
& NZDIGITS
)
453 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
455 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
458 /* 1 through 7 always legal */
459 case '1': case '2': case '3':
460 case '4': case '5': case '6': case '7':
461 base
= basefix
[base
];
462 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
465 /* digits 8 and 9 ok iff decimal or hex */
467 base
= basefix
[base
];
469 break; /* not legal here */
470 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
473 /* letters ok iff hex */
474 case 'A': case 'B': case 'C':
475 case 'D': case 'E': case 'F':
476 case 'a': case 'b': case 'c':
477 case 'd': case 'e': case 'f':
478 /* no need to fix base here */
480 break; /* not legal here */
481 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
484 /* sign ok only as first character */
486 if (flags
& SIGNOK
) {
492 /* x ok iff flag still set & 2nd char */
494 if (flags
& PFXOK
&& p
== buf
+ 1) {
495 base
= 16; /* if %i */
503 * If we got here, c is not a legal character
504 * for a number. Stop accumulating digits.
509 * c is legal: store it and look at the next.
515 break; /* end of input */
518 * If we had only a sign, it is no good; push
519 * back the sign. If the number ends in `x',
520 * it was [sign] '0' 'x', so push back the x
521 * and treat it as [sign] '0'.
523 if (flags
& NDIGITS
) {
530 c
= ((u_char
*)p
)[-1];
531 if (c
== 'x' || c
== 'X') {
536 if ((flags
& SUPPRESS
) == 0) {
540 if ((flags
& UNSIGNED
) == 0)
541 res
= strtoq(buf
, (char **)NULL
, base
);
543 res
= strtouq(buf
, (char **)NULL
, base
);
545 *va_arg(ap
, void **) =
546 (void *)(uintptr_t)res
;
547 else if (flags
& SHORTSHORT
)
548 *va_arg(ap
, char *) = res
;
549 else if (flags
& SHORT
)
550 *va_arg(ap
, short *) = res
;
551 else if (flags
& LONG
)
552 *va_arg(ap
, long *) = res
;
553 else if (flags
& LONGLONG
)
554 *va_arg(ap
, long long *) = res
;
556 *va_arg(ap
, int *) = res
;
566 return (nconversions
!= 0 ? nassigned
: -1);
572 * Fill in the given table from the scanset at the given format
573 * (just after `['). Return a pointer to the character past the
574 * closing `]'. The table has a 1 wherever characters should be
575 * considered part of the scanset.
577 static const u_char
*
578 __sccl(char *tab
, const u_char
*fmt
)
582 /* first `clear' the whole table */
583 c
= *fmt
++; /* first char hat => negated scanset */
585 v
= 1; /* default => accept */
586 c
= *fmt
++; /* get new first char */
588 v
= 0; /* default => reject */
590 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
591 (void) memset(tab
, v
, 256);
594 return (fmt
- 1);/* format ended before closing ] */
597 * Now set the entries corresponding to the actual scanset
598 * to the opposite of the above.
600 * The first character may be ']' (or '-') without being special;
601 * the last character may be '-'.
605 tab
[c
] = v
; /* take character c */
607 n
= *fmt
++; /* and examine the next */
610 case 0: /* format ended too soon */
615 * A scanset of the form
617 * is defined as `the digit 0, the digit 1,
618 * the character +, the character -', but
619 * the effect of a scanset such as
621 * is implementation defined. The V7 Unix
622 * scanf treats `a-z' as `the letters a through
623 * z', but treats `a-a' as `the letter a, the
624 * character -, and the letter a'.
626 * For compatibility, the `-' is not considerd
627 * to define a range if the character following
628 * it is either a close bracket (required by ANSI)
629 * or is not numerically greater than the character
630 * we just stored in the table (c).
633 if (n
== ']' || n
< c
) {
635 break; /* resume the for(;;) */
638 /* fill in the range */
644 * Alas, the V7 Unix scanf also treats formats
645 * such as [a-c-e] as `the letters a through e'.
646 * This too is permitted by the standard....
651 case ']': /* end of scanset */
654 default: /* just another character */