2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * From: Id: vfscanf.c,v 1.13 1998/09/25 12:20:27 obrien Exp
38 * From: static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93";
39 * From: static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93";
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/ctype.h>
45 #include <machine/limits.h>
48 * Note that stdarg.h and the ANSI style va_start macro is used for both
49 * ANSI and traditional C compilers.
51 #include <machine/stdarg.h>
53 #define BUF 32 /* Maximum length of numeric string. */
56 * Flags used during conversion.
58 #define LONG 0x01 /* l: long or double */
59 #define SHORT 0x04 /* h: short */
60 #define SUPPRESS 0x08 /* suppress assignment */
61 #define POINTER 0x10 /* weird %p pointer (`fake hex') */
62 #define NOSKIP 0x20 /* do not skip blanks */
66 * The following are used in numeric conversions only:
67 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
68 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
70 #define SIGNOK 0x40 /* +/- is (still) legal */
71 #define NDIGITS 0x80 /* no digits detected */
73 #define DPTOK 0x100 /* (float) decimal point is still legal */
74 #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
76 #define PFXOK 0x100 /* 0x prefix is (still) legal */
77 #define NZDIGITS 0x200 /* no zero digits detected */
82 #define CT_CHAR 0 /* %c conversion */
83 #define CT_CCL 1 /* %[...] conversion */
84 #define CT_STRING 2 /* %s conversion */
85 #define CT_INT 3 /* integer, i.e., strtoq or strtouq */
86 typedef u_quad_t (*ccfntype)(const char *, char **, int);
88 static const u_char *__sccl(char *, const u_char *);
91 sscanf(const char *ibuf, const char *fmt, ...)
97 ret = vsscanf(ibuf, fmt, ap);
103 vsscanf(const char *inp, char const *fmt0, va_list ap)
106 const u_char *fmt = (const u_char *)fmt0;
107 int c; /* character from format, or conversion */
108 size_t width; /* field width, or 0 */
109 char *p; /* points into all kinds of strings */
110 int n; /* handy integer */
111 int flags; /* flags as defined above */
112 char *p0; /* saves original value of p when necessary */
113 int nassigned; /* number of fields assigned */
114 int nconversions; /* number of conversions */
115 int nread; /* number of characters consumed from fp */
116 int base; /* base argument to strtoq/strtouq */
117 ccfntype ccfn; /* conversion function (strtoq/strtouq) */
118 char ccltab[256]; /* character class table for %[...] */
119 char buf[BUF]; /* buffer for numeric conversions */
121 /* `basefix' is used to avoid `if' tests in the integer scanner */
122 static short basefix[17] =
123 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
130 base = 0; /* XXX just to keep gcc happy */
131 ccfn = NULL; /* XXX just to keep gcc happy */
137 while (inr > 0 && isspace(*inp))
138 nread++, inr--, inp++;
146 * switch on the format. continue if done;
147 * break once format type is derived.
174 case '0': case '1': case '2': case '3': case '4':
175 case '5': case '6': case '7': case '8': case '9':
176 width = width * 10 + c - '0';
185 ccfn = (ccfntype)strtoq;
191 ccfn = (ccfntype)strtoq;
208 flags |= PFXOK; /* enable 0x prefixing */
219 fmt = __sccl(ccltab, fmt);
229 case 'p': /* pointer format is like hex */
230 flags |= POINTER | PFXOK;
238 if (flags & SUPPRESS) /* ??? */
241 *va_arg(ap, short *) = nread;
242 else if (flags & LONG)
243 *va_arg(ap, long *) = nread;
244 else if (flags & QUAD)
245 *va_arg(ap, quad_t *) = nread;
247 *va_arg(ap, int *) = nread;
252 * We have a conversion that requires input.
258 * Consume leading white space, except for formats
259 * that suppress this.
261 if ((flags & NOSKIP) == 0) {
262 while (isspace(*inp)) {
270 * Note that there is at least one character in
271 * the buffer, so conversions that do not set NOSKIP
272 * can no longer result in an input failure.
282 /* scan arbitrary characters (sets NOSKIP) */
285 if (flags & SUPPRESS) {
288 if ((n = inr) < width) {
304 bcopy(inp, va_arg(ap, char *), width);
314 /* scan a (nonempty) character class (sets NOSKIP) */
316 width = (size_t)~0; /* `infinity' */
317 /* take only those things in the class */
318 if (flags & SUPPRESS) {
320 while (ccltab[(unsigned char)*inp]) {
333 p0 = p = va_arg(ap, char *);
334 while (ccltab[(unsigned char)*inp]) {
356 /* like CCL, but zero-length string OK, & no NOSKIP */
359 if (flags & SUPPRESS) {
361 while (!isspace(*inp)) {
370 p0 = p = va_arg(ap, char *);
371 while (!isspace(*inp)) {
387 /* scan an integer as if by strtoq/strtouq */
389 if (width == 0 || width > sizeof(buf) - 1)
390 width = sizeof(buf) - 1;
392 /* size_t is unsigned, hence this optimisation */
393 if (--width > sizeof(buf) - 2)
394 width = sizeof(buf) - 2;
397 flags |= SIGNOK | NDIGITS | NZDIGITS;
398 for (p = buf; width; width--) {
401 * Switch on the character; `goto ok'
402 * if we accept it as a part of number.
407 * The digit 0 is always legal, but is
408 * special. For %i conversions, if no
409 * digits (zero or nonzero) have been
410 * scanned (only signs), we will have
411 * base==0. In that case, we should set
412 * it to 8 and enable 0x prefixing.
413 * Also, if we have not scanned zero digits
414 * before this, do not turn off prefixing
415 * (someone else will turn it off if we
416 * have scanned any nonzero digits).
423 if (flags & NZDIGITS)
424 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
426 flags &= ~(SIGNOK|PFXOK|NDIGITS);
429 /* 1 through 7 always legal */
430 case '1': case '2': case '3':
431 case '4': case '5': case '6': case '7':
432 base = basefix[base];
433 flags &= ~(SIGNOK | PFXOK | NDIGITS);
436 /* digits 8 and 9 ok iff decimal or hex */
438 base = basefix[base];
440 break; /* not legal here */
441 flags &= ~(SIGNOK | PFXOK | NDIGITS);
444 /* letters ok iff hex */
445 case 'A': case 'B': case 'C':
446 case 'D': case 'E': case 'F':
447 case 'a': case 'b': case 'c':
448 case 'd': case 'e': case 'f':
449 /* no need to fix base here */
451 break; /* not legal here */
452 flags &= ~(SIGNOK | PFXOK | NDIGITS);
455 /* sign ok only as first character */
457 if (flags & SIGNOK) {
463 /* x ok iff flag still set & 2nd char */
465 if (flags & PFXOK && p == buf + 1) {
466 base = 16; /* if %i */
474 * If we got here, c is not a legal character
475 * for a number. Stop accumulating digits.
480 * c is legal: store it and look at the next.
486 break; /* end of input */
489 * If we had only a sign, it is no good; push
490 * back the sign. If the number ends in `x',
491 * it was [sign] '0' 'x', so push back the x
492 * and treat it as [sign] '0'.
494 if (flags & NDIGITS) {
501 c = ((u_char *)p)[-1];
502 if (c == 'x' || c == 'X') {
507 if ((flags & SUPPRESS) == 0) {
511 res = (*ccfn)(buf, (char **)NULL, base);
513 *va_arg(ap, void **) =
514 (void *)(uintptr_t)res;
515 else if (flags & SHORT)
516 *va_arg(ap, short *) = res;
517 else if (flags & LONG)
518 *va_arg(ap, long *) = res;
519 else if (flags & QUAD)
520 *va_arg(ap, quad_t *) = res;
522 *va_arg(ap, int *) = res;
532 return (nconversions != 0 ? nassigned : -1);
538 * Fill in the given table from the scanset at the given format
539 * (just after `['). Return a pointer to the character past the
540 * closing `]'. The table has a 1 wherever characters should be
541 * considered part of the scanset.
543 static const u_char *
544 __sccl(char *tab, const u_char *fmt)
548 /* first `clear' the whole table */
549 c = *fmt++; /* first char hat => negated scanset */
551 v = 1; /* default => accept */
552 c = *fmt++; /* get new first char */
554 v = 0; /* default => reject */
556 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
557 for (n = 0; n < 256; n++)
558 tab[n] = v; /* memset(tab, v, 256) */
561 return (fmt - 1);/* format ended before closing ] */
564 * Now set the entries corresponding to the actual scanset
565 * to the opposite of the above.
567 * The first character may be ']' (or '-') without being special;
568 * the last character may be '-'.
572 tab[c] = v; /* take character c */
574 n = *fmt++; /* and examine the next */
577 case 0: /* format ended too soon */
582 * A scanset of the form
584 * is defined as `the digit 0, the digit 1,
585 * the character +, the character -', but
586 * the effect of a scanset such as
588 * is implementation defined. The V7 Unix
589 * scanf treats `a-z' as `the letters a through
590 * z', but treats `a-a' as `the letter a, the
591 * character -, and the letter a'.
593 * For compatibility, the `-' is not considerd
594 * to define a range if the character following
595 * it is either a close bracket (required by ANSI)
596 * or is not numerically greater than the character
597 * we just stored in the table (c).
600 if (n == ']' || n < c) {
602 break; /* resume the for(;;) */
605 /* fill in the range */
611 * Alas, the V7 Unix scanf also treats formats
612 * such as [a-c-e] as `the letters a through e'.
613 * This too is permitted by the standard....
618 case ']': /* end of scanset */
621 default: /* just another character */