2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * From: Id: vfscanf.c,v 1.13 1998/09/25 12:20:27 obrien Exp
35 * From: static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93";
36 * From: static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/ctype.h>
45 #include <sys/limits.h>
46 #include <sys/stddef.h>
49 * Note that stdarg.h and the ANSI style va_start macro is used for both
50 * ANSI and traditional C compilers.
52 #include <machine/stdarg.h>
54 #define BUF 32 /* Maximum length of numeric string. */
57 * Flags used during conversion.
59 #define LONG 0x01 /* l: long or double */
60 #define SHORT 0x04 /* h: short */
61 #define SUPPRESS 0x08 /* suppress assignment */
62 #define POINTER 0x10 /* weird %p pointer (`fake hex') */
63 #define NOSKIP 0x20 /* do not skip blanks */
65 #define INTMAXT 0x800 /* j: intmax_t */
66 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
67 #define SIZET 0x2000 /* z: size_t */
68 #define SHORTSHORT 0x4000 /** hh: char */
71 * The following are used in numeric conversions only:
72 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
73 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
75 #define SIGNOK 0x40 /* +/- is (still) legal */
76 #define NDIGITS 0x80 /* no digits detected */
78 #define DPTOK 0x100 /* (float) decimal point is still legal */
79 #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
81 #define PFXOK 0x100 /* 0x prefix is (still) legal */
82 #define NZDIGITS 0x200 /* no zero digits detected */
87 #define CT_CHAR 0 /* %c conversion */
88 #define CT_CCL 1 /* %[...] conversion */
89 #define CT_STRING 2 /* %s conversion */
90 #define CT_INT 3 /* integer, i.e., strtoq or strtouq */
91 typedef u_quad_t (*ccfntype)(const char *, char **, int);
93 static const u_char *__sccl(char *, const u_char *);
96 sscanf(const char *ibuf, const char *fmt, ...)
102 ret = vsscanf(ibuf, fmt, ap);
108 vsscanf(const char *inp, char const *fmt0, va_list ap)
111 const u_char *fmt = (const u_char *)fmt0;
112 int c; /* character from format, or conversion */
113 size_t width; /* field width, or 0 */
114 char *p; /* points into all kinds of strings */
115 int n; /* handy integer */
116 int flags; /* flags as defined above */
117 char *p0; /* saves original value of p when necessary */
118 int nassigned; /* number of fields assigned */
119 int nconversions; /* number of conversions */
120 int nread; /* number of characters consumed from fp */
121 int base; /* base argument to strtoq/strtouq */
122 ccfntype ccfn; /* conversion function (strtoq/strtouq) */
123 char ccltab[256]; /* character class table for %[...] */
124 char buf[BUF]; /* buffer for numeric conversions */
126 /* `basefix' is used to avoid `if' tests in the integer scanner */
127 static short basefix[17] =
128 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
135 base = 0; /* XXX just to keep gcc happy */
136 ccfn = NULL; /* XXX just to keep gcc happy */
142 while (inr > 0 && isspace(*inp))
143 nread++, inr--, inp++;
151 * switch on the format. continue if done;
152 * break once format type is derived.
198 case '0': case '1': case '2': case '3': case '4':
199 case '5': case '6': case '7': case '8': case '9':
200 width = width * 10 + c - '0';
209 ccfn = (ccfntype)strtoq;
215 ccfn = (ccfntype)strtoq;
232 flags |= PFXOK; /* enable 0x prefixing */
243 fmt = __sccl(ccltab, fmt);
253 case 'p': /* pointer format is like hex */
254 flags |= POINTER | PFXOK;
262 if (flags & SUPPRESS) /* ??? */
264 if (flags & SHORTSHORT)
265 *va_arg(ap, char *) = nread;
266 else if (flags & SHORT)
267 *va_arg(ap, short *) = nread;
268 else if (flags & LONG)
269 *va_arg(ap, long *) = nread;
270 else if (flags & QUAD)
271 *va_arg(ap, quad_t *) = nread;
272 else if (flags & INTMAXT)
273 *va_arg(ap, intmax_t *) = nread;
274 else if (flags & SIZET)
275 *va_arg(ap, size_t *) = nread;
276 else if (flags & PTRDIFFT)
277 *va_arg(ap, ptrdiff_t *) = nread;
279 *va_arg(ap, int *) = nread;
284 * We have a conversion that requires input.
290 * Consume leading white space, except for formats
291 * that suppress this.
293 if ((flags & NOSKIP) == 0) {
294 while (isspace(*inp)) {
302 * Note that there is at least one character in
303 * the buffer, so conversions that do not set NOSKIP
304 * can no longer result in an input failure.
313 /* scan arbitrary characters (sets NOSKIP) */
316 if (flags & SUPPRESS) {
319 if ((n = inr) < width) {
335 bcopy(inp, va_arg(ap, char *), width);
345 /* scan a (nonempty) character class (sets NOSKIP) */
347 width = (size_t)~0; /* `infinity' */
348 /* take only those things in the class */
349 if (flags & SUPPRESS) {
351 while (ccltab[(unsigned char)*inp]) {
364 p0 = p = va_arg(ap, char *);
365 while (ccltab[(unsigned char)*inp]) {
387 /* like CCL, but zero-length string OK, & no NOSKIP */
390 if (flags & SUPPRESS) {
392 while (!isspace(*inp)) {
401 p0 = p = va_arg(ap, char *);
402 while (!isspace(*inp)) {
418 /* scan an integer as if by strtoq/strtouq */
420 if (width == 0 || width > sizeof(buf) - 1)
421 width = sizeof(buf) - 1;
423 /* size_t is unsigned, hence this optimisation */
424 if (--width > sizeof(buf) - 2)
425 width = sizeof(buf) - 2;
428 flags |= SIGNOK | NDIGITS | NZDIGITS;
429 for (p = buf; width; width--) {
432 * Switch on the character; `goto ok'
433 * if we accept it as a part of number.
437 * The digit 0 is always legal, but is
438 * special. For %i conversions, if no
439 * digits (zero or nonzero) have been
440 * scanned (only signs), we will have
441 * base==0. In that case, we should set
442 * it to 8 and enable 0x prefixing.
443 * Also, if we have not scanned zero digits
444 * before this, do not turn off prefixing
445 * (someone else will turn it off if we
446 * have scanned any nonzero digits).
453 if (flags & NZDIGITS)
454 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
456 flags &= ~(SIGNOK|PFXOK|NDIGITS);
459 /* 1 through 7 always legal */
460 case '1': case '2': case '3':
461 case '4': case '5': case '6': case '7':
462 base = basefix[base];
463 flags &= ~(SIGNOK | PFXOK | NDIGITS);
466 /* digits 8 and 9 ok iff decimal or hex */
468 base = basefix[base];
470 break; /* not legal here */
471 flags &= ~(SIGNOK | PFXOK | NDIGITS);
474 /* letters ok iff hex */
475 case 'A': case 'B': case 'C':
476 case 'D': case 'E': case 'F':
477 case 'a': case 'b': case 'c':
478 case 'd': case 'e': case 'f':
479 /* no need to fix base here */
481 break; /* not legal here */
482 flags &= ~(SIGNOK | PFXOK | NDIGITS);
485 /* sign ok only as first character */
487 if (flags & SIGNOK) {
493 /* x ok iff flag still set & 2nd char */
495 if (flags & PFXOK && p == buf + 1) {
496 base = 16; /* if %i */
504 * If we got here, c is not a legal character
505 * for a number. Stop accumulating digits.
510 * c is legal: store it and look at the next.
516 break; /* end of input */
519 * If we had only a sign, it is no good; push
520 * back the sign. If the number ends in `x',
521 * it was [sign] '0' 'x', so push back the x
522 * and treat it as [sign] '0'.
524 if (flags & NDIGITS) {
531 c = ((u_char *)p)[-1];
532 if (c == 'x' || c == 'X') {
537 if ((flags & SUPPRESS) == 0) {
541 res = (*ccfn)(buf, (char **)NULL, base);
543 *va_arg(ap, void **) =
544 (void *)(uintptr_t)res;
545 else if (flags & SHORTSHORT)
546 *va_arg(ap, char *) = res;
547 else if (flags & SHORT)
548 *va_arg(ap, short *) = res;
549 else if (flags & LONG)
550 *va_arg(ap, long *) = res;
551 else if (flags & QUAD)
552 *va_arg(ap, quad_t *) = res;
553 else if (flags & INTMAXT)
554 *va_arg(ap, intmax_t *) = res;
555 else if (flags & PTRDIFFT)
556 *va_arg(ap, ptrdiff_t *) = res;
557 else if (flags & SIZET)
558 *va_arg(ap, size_t *) = res;
560 *va_arg(ap, int *) = res;
569 return (nconversions != 0 ? nassigned : -1);
575 * Fill in the given table from the scanset at the given format
576 * (just after `['). Return a pointer to the character past the
577 * closing `]'. The table has a 1 wherever characters should be
578 * considered part of the scanset.
580 static const u_char *
581 __sccl(char *tab, const u_char *fmt)
585 /* first `clear' the whole table */
586 c = *fmt++; /* first char hat => negated scanset */
588 v = 1; /* default => accept */
589 c = *fmt++; /* get new first char */
591 v = 0; /* default => reject */
593 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
594 for (n = 0; n < 256; n++)
595 tab[n] = v; /* memset(tab, v, 256) */
598 return (fmt - 1);/* format ended before closing ] */
601 * Now set the entries corresponding to the actual scanset
602 * to the opposite of the above.
604 * The first character may be ']' (or '-') without being special;
605 * the last character may be '-'.
609 tab[c] = v; /* take character c */
611 n = *fmt++; /* and examine the next */
613 case 0: /* format ended too soon */
618 * A scanset of the form
620 * is defined as `the digit 0, the digit 1,
621 * the character +, the character -', but
622 * the effect of a scanset such as
624 * is implementation defined. The V7 Unix
625 * scanf treats `a-z' as `the letters a through
626 * z', but treats `a-a' as `the letter a, the
627 * character -, and the letter a'.
629 * For compatibility, the `-' is not considered
630 * to define a range if the character following
631 * it is either a close bracket (required by ANSI)
632 * or is not numerically greater than the character
633 * we just stored in the table (c).
636 if (n == ']' || n < c) {
638 break; /* resume the for(;;) */
641 /* fill in the range */
647 * Alas, the V7 Unix scanf also treats formats
648 * such as [a-c-e] as `the letters a through e'.
649 * This too is permitted by the standard....
654 case ']': /* end of scanset */
657 default: /* just another character */