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 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/ctype.h>
43 #include <sys/limits.h>
44 #include <sys/stddef.h>
47 * Note that stdarg.h and the ANSI style va_start macro is used for both
48 * ANSI and traditional C compilers.
50 #include <machine/stdarg.h>
52 #define BUF 32 /* Maximum length of numeric string. */
55 * Flags used during conversion.
57 #define LONG 0x01 /* l: long or double */
58 #define SHORT 0x04 /* h: short */
59 #define SUPPRESS 0x08 /* suppress assignment */
60 #define POINTER 0x10 /* weird %p pointer (`fake hex') */
61 #define NOSKIP 0x20 /* do not skip blanks */
63 #define INTMAXT 0x800 /* j: intmax_t */
64 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
65 #define SIZET 0x2000 /* z: size_t */
66 #define SHORTSHORT 0x4000 /** hh: char */
69 * The following are used in numeric conversions only:
70 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
71 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
73 #define SIGNOK 0x40 /* +/- is (still) legal */
74 #define NDIGITS 0x80 /* no digits detected */
76 #define DPTOK 0x100 /* (float) decimal point is still legal */
77 #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
79 #define PFXOK 0x100 /* 0x prefix is (still) legal */
80 #define NZDIGITS 0x200 /* no zero digits detected */
85 #define CT_CHAR 0 /* %c conversion */
86 #define CT_CCL 1 /* %[...] conversion */
87 #define CT_STRING 2 /* %s conversion */
88 #define CT_INT 3 /* integer, i.e., strtoq or strtouq */
89 typedef u_quad_t (*ccfntype)(const char *, char **, int);
91 static const u_char *__sccl(char *, const u_char *);
94 sscanf(const char *ibuf, const char *fmt, ...)
100 ret = vsscanf(ibuf, fmt, ap);
106 vsscanf(const char *inp, char const *fmt0, va_list ap)
109 const u_char *fmt = (const u_char *)fmt0;
110 int c; /* character from format, or conversion */
111 size_t width; /* field width, or 0 */
112 char *p; /* points into all kinds of strings */
113 int n; /* handy integer */
114 int flags; /* flags as defined above */
115 char *p0; /* saves original value of p when necessary */
116 int nassigned; /* number of fields assigned */
117 int nconversions; /* number of conversions */
118 int nread; /* number of characters consumed from fp */
119 int base; /* base argument to strtoq/strtouq */
120 ccfntype ccfn; /* conversion function (strtoq/strtouq) */
121 char ccltab[256]; /* character class table for %[...] */
122 char buf[BUF]; /* buffer for numeric conversions */
124 /* `basefix' is used to avoid `if' tests in the integer scanner */
125 static short basefix[17] =
126 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
133 base = 0; /* XXX just to keep gcc happy */
134 ccfn = NULL; /* XXX just to keep gcc happy */
140 while (inr > 0 && isspace(*inp))
141 nread++, inr--, inp++;
149 * switch on the format. continue if done;
150 * break once format type is derived.
196 case '0': case '1': case '2': case '3': case '4':
197 case '5': case '6': case '7': case '8': case '9':
198 width = width * 10 + c - '0';
207 ccfn = (ccfntype)strtoq;
213 ccfn = (ccfntype)strtoq;
230 flags |= PFXOK; /* enable 0x prefixing */
241 fmt = __sccl(ccltab, fmt);
251 case 'p': /* pointer format is like hex */
252 flags |= POINTER | PFXOK;
260 if (flags & SUPPRESS) /* ??? */
262 if (flags & SHORTSHORT)
263 *va_arg(ap, char *) = nread;
264 else if (flags & SHORT)
265 *va_arg(ap, short *) = nread;
266 else if (flags & LONG)
267 *va_arg(ap, long *) = nread;
268 else if (flags & QUAD)
269 *va_arg(ap, quad_t *) = nread;
270 else if (flags & INTMAXT)
271 *va_arg(ap, intmax_t *) = nread;
272 else if (flags & SIZET)
273 *va_arg(ap, size_t *) = nread;
274 else if (flags & PTRDIFFT)
275 *va_arg(ap, ptrdiff_t *) = nread;
277 *va_arg(ap, int *) = nread;
282 * We have a conversion that requires input.
288 * Consume leading white space, except for formats
289 * that suppress this.
291 if ((flags & NOSKIP) == 0) {
292 while (isspace(*inp)) {
300 * Note that there is at least one character in
301 * the buffer, so conversions that do not set NOSKIP
302 * can no longer result in an input failure.
311 /* scan arbitrary characters (sets NOSKIP) */
314 if (flags & SUPPRESS) {
317 if ((n = inr) < 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 strtoq/strtouq */
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.
435 * The digit 0 is always legal, but is
436 * special. For %i conversions, if no
437 * digits (zero or nonzero) have been
438 * scanned (only signs), we will have
439 * base==0. In that case, we should set
440 * it to 8 and enable 0x prefixing.
441 * Also, if we have not scanned zero digits
442 * before this, do not turn off prefixing
443 * (someone else will turn it off if we
444 * have scanned any nonzero digits).
451 if (flags & NZDIGITS)
452 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
454 flags &= ~(SIGNOK|PFXOK|NDIGITS);
457 /* 1 through 7 always legal */
458 case '1': case '2': case '3':
459 case '4': case '5': case '6': case '7':
460 base = basefix[base];
461 flags &= ~(SIGNOK | PFXOK | NDIGITS);
464 /* digits 8 and 9 ok iff decimal or hex */
466 base = basefix[base];
468 break; /* not legal here */
469 flags &= ~(SIGNOK | PFXOK | NDIGITS);
472 /* letters ok iff hex */
473 case 'A': case 'B': case 'C':
474 case 'D': case 'E': case 'F':
475 case 'a': case 'b': case 'c':
476 case 'd': case 'e': case 'f':
477 /* no need to fix base here */
479 break; /* not legal here */
480 flags &= ~(SIGNOK | PFXOK | NDIGITS);
483 /* sign ok only as first character */
485 if (flags & SIGNOK) {
491 /* x ok iff flag still set & 2nd char */
493 if (flags & PFXOK && p == buf + 1) {
494 base = 16; /* if %i */
502 * If we got here, c is not a legal character
503 * for a number. Stop accumulating digits.
508 * c is legal: store it and look at the next.
514 break; /* end of input */
517 * If we had only a sign, it is no good; push
518 * back the sign. If the number ends in `x',
519 * it was [sign] '0' 'x', so push back the x
520 * and treat it as [sign] '0'.
522 if (flags & NDIGITS) {
529 c = ((u_char *)p)[-1];
530 if (c == 'x' || c == 'X') {
535 if ((flags & SUPPRESS) == 0) {
539 res = (*ccfn)(buf, (char **)NULL, base);
541 *va_arg(ap, void **) =
542 (void *)(uintptr_t)res;
543 else if (flags & SHORTSHORT)
544 *va_arg(ap, char *) = res;
545 else if (flags & SHORT)
546 *va_arg(ap, short *) = res;
547 else if (flags & LONG)
548 *va_arg(ap, long *) = res;
549 else if (flags & QUAD)
550 *va_arg(ap, quad_t *) = res;
551 else if (flags & INTMAXT)
552 *va_arg(ap, intmax_t *) = res;
553 else if (flags & PTRDIFFT)
554 *va_arg(ap, ptrdiff_t *) = res;
555 else if (flags & SIZET)
556 *va_arg(ap, size_t *) = res;
558 *va_arg(ap, int *) = res;
567 return (nconversions != 0 ? nassigned : -1);
573 * Fill in the given table from the scanset at the given format
574 * (just after `['). Return a pointer to the character past the
575 * closing `]'. The table has a 1 wherever characters should be
576 * considered part of the scanset.
578 static const u_char *
579 __sccl(char *tab, const u_char *fmt)
583 /* first `clear' the whole table */
584 c = *fmt++; /* first char hat => negated scanset */
586 v = 1; /* default => accept */
587 c = *fmt++; /* get new first char */
589 v = 0; /* default => reject */
591 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
592 for (n = 0; n < 256; n++)
593 tab[n] = v; /* memset(tab, v, 256) */
596 return (fmt - 1);/* format ended before closing ] */
599 * Now set the entries corresponding to the actual scanset
600 * to the opposite of the above.
602 * The first character may be ']' (or '-') without being special;
603 * the last character may be '-'.
607 tab[c] = v; /* take character c */
609 n = *fmt++; /* and examine the next */
611 case 0: /* format ended too soon */
616 * A scanset of the form
618 * is defined as `the digit 0, the digit 1,
619 * the character +, the character -', but
620 * the effect of a scanset such as
622 * is implementation defined. The V7 Unix
623 * scanf treats `a-z' as `the letters a through
624 * z', but treats `a-a' as `the letter a, the
625 * character -, and the letter a'.
627 * For compatibility, the `-' is not considered
628 * to define a range if the character following
629 * it is either a close bracket (required by ANSI)
630 * or is not numerically greater than the character
631 * we just stored in the table (c).
634 if (n == ']' || n < c) {
636 break; /* resume the for(;;) */
639 /* fill in the range */
645 * Alas, the V7 Unix scanf also treats formats
646 * such as [a-c-e] as `the letters a through e'.
647 * This too is permitted by the standard....
652 case ']': /* end of scanset */
655 default: /* just another character */