]>
git.saurik.com Git - apple/xnu.git/blob - libkern/stdio/scanf.c
0c634abf4ee2d73176793f69f8790ae6a002b2f7
2 * Copyright (c) 2004-2016 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1990, 1993
30 * The Regents of the University of California. All rights reserved.
32 * This code is derived from software contributed to Berkeley by
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 #include <sys/cdefs.h>
68 #include <sys/param.h>
70 quad_t
strtoq(const char *, char **, int);
71 u_quad_t
strtouq(const char *, char **, int);
76 return c
== ' ' || c
== '\t' || c
== '\n' || c
== '\12';
79 #define BUF 32 /* Maximum length of numeric string. */
82 * Flags used during conversion.
84 #define LONG 0x01 /* l: long or double */
85 #define SHORT 0x04 /* h: short */
86 #define SUPPRESS 0x08 /* *: suppress assignment */
87 #define POINTER 0x10 /* p: void * (as hex) */
88 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
89 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
90 #define SHORTSHORT 0x4000 /* hh: char */
91 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
94 * The following are used in numeric conversions only:
95 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
96 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
98 #define SIGNOK 0x40 /* +/- is (still) legal */
99 #define NDIGITS 0x80 /* no digits detected */
101 #define DPTOK 0x100 /* (float) decimal point is still legal */
102 #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
104 #define PFXOK 0x100 /* 0x prefix is (still) legal */
105 #define NZDIGITS 0x200 /* no zero digits detected */
110 #define CT_CHAR 0 /* %c conversion */
111 #define CT_CCL 1 /* %[...] conversion */
112 #define CT_STRING 2 /* %s conversion */
113 #define CT_INT 3 /* %[dioupxX] conversion */
115 static const u_char
*__sccl(char *, const u_char
*);
117 int sscanf(const char *, const char *, ...);
118 int vsscanf(const char *, char const *, va_list);
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 ssize_t width
; /* field width, or 0 */
138 char *p
; /* points into all kinds of strings */
139 int flags
; /* flags as defined above */
140 char *p0
; /* saves original value of p when necessary */
141 int nassigned
= 0; /* number of fields assigned */
142 int nconversions
= 0; /* number of conversions */
143 int nread
= 0; /* number of characters consumed from fp */
144 int base
= 0; /* base argument to conversion function */
145 char ccltab
[256]; /* character class table for %[...] */
146 char buf
[BUF
]; /* buffer for numeric conversions */
148 /* `basefix' is used to avoid `if' tests in the integer scanner */
149 static short basefix
[17] =
150 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
152 inr
= (ssize_t
)strlen(inp
);
155 char c
= (char)*fmt
++; /* character from format, or conversion */
160 while (inr
> 0 && isspace(*inp
)) {
173 * switch on the format. continue if done;
174 * break once format type is derived.
204 flags
|= LONGLONG
; /* not quite */
215 case '0': case '1': case '2': case '3': case '4':
216 case '5': case '6': case '7': case '8': case '9':
217 width
= width
* 10 + c
- '0';
247 flags
|= PFXOK
; /* enable 0x prefixing */
258 fmt
= __sccl(ccltab
, fmt
);
268 case 'p': /* pointer format is like hex */
269 flags
|= POINTER
| PFXOK
;
277 if (flags
& SUPPRESS
) { /* ??? */
280 if (flags
& SHORTSHORT
) {
281 *va_arg(ap
, char *) = (char)nread
;
282 } else if (flags
& SHORT
) {
283 *va_arg(ap
, short *) = (short)nread
;
284 } else if (flags
& LONG
) {
285 *va_arg(ap
, long *) = (long)nread
;
286 } else if (flags
& LONGLONG
) {
287 *va_arg(ap
, long long *) = (long long)nread
;
289 *va_arg(ap
, int *) = (int)nread
;
295 * We have a conversion that requires input.
302 * Consume leading white space, except for formats
303 * that suppress this.
305 if ((flags
& NOSKIP
) == 0) {
306 while (isspace(*inp
)) {
315 * Note that there is at least one character in
316 * the buffer, so conversions that do not set NOSKIP
317 * can no longer result in an input failure.
326 /* scan arbitrary characters (sets NOSKIP) */
330 if (flags
& SUPPRESS
) {
343 sum
+= (size_t)width
;
351 bcopy(inp
, va_arg(ap
, char *), width
);
361 /* scan a (nonempty) character class (sets NOSKIP) */
363 width
= SSIZE_MAX
; /* `infinity' */
365 /* take only those things in the class */
367 if (flags
& SUPPRESS
) {
369 while (ccltab
[(unsigned char)*inp
]) {
387 p0
= p
= va_arg(ap
, char *);
388 while (ccltab
[(unsigned char)*inp
]) {
414 /* like CCL, but zero-length string OK, & no NOSKIP */
418 if (flags
& SUPPRESS
) {
420 while (!isspace(*inp
)) {
433 p0
= p
= va_arg(ap
, char *);
434 while (!isspace(*inp
)) {
452 /* scan an integer as if by the conversion function */
453 if (width
<= 0 || width
> (ssize_t
)(sizeof(buf
) - 1)) {
454 width
= sizeof(buf
) - 1;
456 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
457 for (p
= buf
; width
; width
--) {
460 * Switch on the character; `goto ok'
461 * if we accept it as a part of number.
465 * The digit 0 is always legal, but is
466 * special. For %i conversions, if no
467 * digits (zero or nonzero) have been
468 * scanned (only signs), we will have
469 * base==0. In that case, we should set
470 * it to 8 and enable 0x prefixing.
471 * Also, if we have not scanned zero digits
472 * before this, do not turn off prefixing
473 * (someone else will turn it off if we
474 * have scanned any nonzero digits).
481 if (flags
& NZDIGITS
) {
482 flags
&= ~(SIGNOK
| NZDIGITS
| NDIGITS
);
484 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
488 /* 1 through 7 always legal */
489 case '1': case '2': case '3':
490 case '4': case '5': case '6': case '7':
491 base
= basefix
[base
];
492 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
495 /* digits 8 and 9 ok iff decimal or hex */
497 base
= basefix
[base
];
499 break; /* not legal here */
501 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
504 /* letters ok iff hex */
505 case 'A': case 'B': case 'C':
506 case 'D': case 'E': case 'F':
507 case 'a': case 'b': case 'c':
508 case 'd': case 'e': case 'f':
509 /* no need to fix base here */
511 break; /* not legal here */
513 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
516 /* sign ok only as first character */
518 if (flags
& SIGNOK
) {
524 /* x ok iff flag still set & 2nd char */
526 if (flags
& PFXOK
&& p
== buf
+ 1) {
527 base
= 16; /* if %i */
535 * If we got here, c is not a legal character
536 * for a number. Stop accumulating digits.
541 * c is legal: store it and look at the next.
547 break; /* end of input */
551 * If we had only a sign, it is no good; push
552 * back the sign. If the number ends in `x',
553 * it was [sign] '0' 'x', so push back the x
554 * and treat it as [sign] '0'.
556 if (flags
& NDIGITS
) {
564 if (c
== 'x' || c
== 'X') {
569 if ((flags
& SUPPRESS
) == 0) {
573 if ((flags
& UNSIGNED
) == 0) {
574 res
= (u_quad_t
)strtoq(buf
, (char **)NULL
, base
);
576 res
= strtouq(buf
, (char **)NULL
, base
);
578 if (flags
& POINTER
) {
579 *va_arg(ap
, void **) =
580 (void *)(uintptr_t)res
;
581 } else if (flags
& SHORTSHORT
) {
582 *va_arg(ap
, char *) = (char)res
;
583 } else if (flags
& SHORT
) {
584 *va_arg(ap
, short *) = (short)res
;
585 } else if (flags
& LONG
) {
586 *va_arg(ap
, long *) = (long)res
;
587 } else if (flags
& LONGLONG
) {
588 *va_arg(ap
, long long *) = (long long)res
;
590 *va_arg(ap
, int *) = (int)res
;
600 return nconversions
!= 0 ? nassigned
: -1;
606 * Fill in the given table from the scanset at the given format
607 * (just after `['). Return a pointer to the character past the
608 * closing `]'. The table has a 1 wherever characters should be
609 * considered part of the scanset.
611 static const u_char
*
612 __sccl(char *tab
, const u_char
*fmt
)
616 /* first `clear' the whole table */
617 int c
= *fmt
++; /* first char hat => negated scanset */
619 v
= 1; /* default => accept */
620 c
= *fmt
++; /* get new first char */
622 v
= 0; /* default => reject */
624 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
625 (void) memset(tab
, v
, 256);
628 return fmt
- 1;/* format ended before closing ] */
631 * Now set the entries corresponding to the actual scanset
632 * to the opposite of the above.
634 * The first character may be ']' (or '-') without being special;
635 * the last character may be '-'.
640 tab
[c
] = v
; /* take character c */
644 case 0: /* format ended too soon */
649 * A scanset of the form
651 * is defined as `the digit 0, the digit 1,
652 * the character +, the character -', but
653 * the effect of a scanset such as
655 * is implementation defined. The V7 Unix
656 * scanf treats `a-z' as `the letters a through
657 * z', but treats `a-a' as `the letter a, the
658 * character -, and the letter a'.
660 * For compatibility, the `-' is not considerd
661 * to define a range if the character following
662 * it is either a close bracket (required by ANSI)
663 * or is not numerically greater than the character
664 * we just stored in the table (c).
667 if (n
== ']' || n
< c
) {
669 break; /* resume the for(;;) */
672 /* fill in the range */
678 * Alas, the V7 Unix scanf also treats formats
679 * such as [a-c-e] as `the letters a through e'.
680 * This too is permitted by the standard....
684 case ']': /* end of scanset */
687 default: /* just another character */