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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * From: Id: vfscanf.c,v 1.13 1998/09/25 12:20:27 obrien Exp
33 * From: static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93";
34 * From: static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/ctype.h>
43 #include <sys/limits.h>
46 * Note that stdarg.h and the ANSI style va_start macro is used for both
47 * ANSI and traditional C compilers.
49 #include <machine/stdarg.h>
51 #define BUF 32 /* Maximum length of numeric string. */
54 * Flags used during conversion.
56 #define LONG 0x01 /* l: long or double */
57 #define SHORT 0x04 /* h: short */
58 #define SUPPRESS 0x08 /* suppress assignment */
59 #define POINTER 0x10 /* weird %p pointer (`fake hex') */
60 #define NOSKIP 0x20 /* do not skip blanks */
62 #define SHORTSHORT 0x4000 /** hh: char */
65 * The following are used in numeric conversions only:
66 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
67 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
69 #define SIGNOK 0x40 /* +/- is (still) legal */
70 #define NDIGITS 0x80 /* no digits detected */
72 #define DPTOK 0x100 /* (float) decimal point is still legal */
73 #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
75 #define PFXOK 0x100 /* 0x prefix is (still) legal */
76 #define NZDIGITS 0x200 /* no zero digits detected */
81 #define CT_CHAR 0 /* %c conversion */
82 #define CT_CCL 1 /* %[...] conversion */
83 #define CT_STRING 2 /* %s conversion */
84 #define CT_INT 3 /* integer, i.e., strtoq or strtouq */
85 typedef u_quad_t (*ccfntype)(const char *, char **, int);
87 static const u_char *__sccl(char *, const u_char *);
90 sscanf(const char *ibuf, const char *fmt, ...)
96 ret = vsscanf(ibuf, fmt, ap);
102 vsscanf(const char *inp, char const *fmt0, va_list ap)
105 const u_char *fmt = (const u_char *)fmt0;
106 int c; /* character from format, or conversion */
107 size_t width; /* field width, or 0 */
108 char *p; /* points into all kinds of strings */
109 int n; /* handy integer */
110 int flags; /* flags as defined above */
111 char *p0; /* saves original value of p when necessary */
112 int nassigned; /* number of fields assigned */
113 int nconversions; /* number of conversions */
114 int nread; /* number of characters consumed from fp */
115 int base; /* base argument to strtoq/strtouq */
116 ccfntype ccfn; /* conversion function (strtoq/strtouq) */
117 char ccltab[256]; /* character class table for %[...] */
118 char buf[BUF]; /* buffer for numeric conversions */
120 /* `basefix' is used to avoid `if' tests in the integer scanner */
121 static short basefix[17] =
122 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
129 base = 0; /* XXX just to keep gcc happy */
130 ccfn = NULL; /* XXX just to keep gcc happy */
136 while (inr > 0 && isspace(*inp))
137 nread++, inr--, inp++;
145 * switch on the format. continue if done;
146 * break once format type is derived.
183 case '0': case '1': case '2': case '3': case '4':
184 case '5': case '6': case '7': case '8': case '9':
185 width = width * 10 + c - '0';
194 ccfn = (ccfntype)strtoq;
200 ccfn = (ccfntype)strtoq;
217 flags |= PFXOK; /* enable 0x prefixing */
228 fmt = __sccl(ccltab, fmt);
238 case 'p': /* pointer format is like hex */
239 flags |= POINTER | PFXOK;
247 if (flags & SUPPRESS) /* ??? */
249 if (flags & SHORTSHORT)
250 *va_arg(ap, char *) = nread;
251 else if (flags & SHORT)
252 *va_arg(ap, short *) = nread;
253 else if (flags & LONG)
254 *va_arg(ap, long *) = nread;
255 else if (flags & QUAD)
256 *va_arg(ap, quad_t *) = nread;
258 *va_arg(ap, int *) = nread;
263 * We have a conversion that requires input.
269 * Consume leading white space, except for formats
270 * that suppress this.
272 if ((flags & NOSKIP) == 0) {
273 while (isspace(*inp)) {
281 * Note that there is at least one character in
282 * the buffer, so conversions that do not set NOSKIP
283 * can no longer result in an input failure.
293 /* scan arbitrary characters (sets NOSKIP) */
296 if (flags & SUPPRESS) {
299 if ((n = inr) < width) {
315 bcopy(inp, va_arg(ap, char *), width);
325 /* scan a (nonempty) character class (sets NOSKIP) */
327 width = (size_t)~0; /* `infinity' */
328 /* take only those things in the class */
329 if (flags & SUPPRESS) {
331 while (ccltab[(unsigned char)*inp]) {
344 p0 = p = va_arg(ap, char *);
345 while (ccltab[(unsigned char)*inp]) {
367 /* like CCL, but zero-length string OK, & no NOSKIP */
370 if (flags & SUPPRESS) {
372 while (!isspace(*inp)) {
381 p0 = p = va_arg(ap, char *);
382 while (!isspace(*inp)) {
398 /* scan an integer as if by strtoq/strtouq */
400 if (width == 0 || width > sizeof(buf) - 1)
401 width = sizeof(buf) - 1;
403 /* size_t is unsigned, hence this optimisation */
404 if (--width > sizeof(buf) - 2)
405 width = sizeof(buf) - 2;
408 flags |= SIGNOK | NDIGITS | NZDIGITS;
409 for (p = buf; width; width--) {
412 * Switch on the character; `goto ok'
413 * if we accept it as a part of number.
418 * The digit 0 is always legal, but is
419 * special. For %i conversions, if no
420 * digits (zero or nonzero) have been
421 * scanned (only signs), we will have
422 * base==0. In that case, we should set
423 * it to 8 and enable 0x prefixing.
424 * Also, if we have not scanned zero digits
425 * before this, do not turn off prefixing
426 * (someone else will turn it off if we
427 * have scanned any nonzero digits).
434 if (flags & NZDIGITS)
435 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
437 flags &= ~(SIGNOK|PFXOK|NDIGITS);
440 /* 1 through 7 always legal */
441 case '1': case '2': case '3':
442 case '4': case '5': case '6': case '7':
443 base = basefix[base];
444 flags &= ~(SIGNOK | PFXOK | NDIGITS);
447 /* digits 8 and 9 ok iff decimal or hex */
449 base = basefix[base];
451 break; /* not legal here */
452 flags &= ~(SIGNOK | PFXOK | NDIGITS);
455 /* letters ok iff hex */
456 case 'A': case 'B': case 'C':
457 case 'D': case 'E': case 'F':
458 case 'a': case 'b': case 'c':
459 case 'd': case 'e': case 'f':
460 /* no need to fix base here */
462 break; /* not legal here */
463 flags &= ~(SIGNOK | PFXOK | NDIGITS);
466 /* sign ok only as first character */
468 if (flags & SIGNOK) {
474 /* x ok iff flag still set & 2nd char */
476 if (flags & PFXOK && p == buf + 1) {
477 base = 16; /* if %i */
485 * If we got here, c is not a legal character
486 * for a number. Stop accumulating digits.
491 * c is legal: store it and look at the next.
497 break; /* end of input */
500 * If we had only a sign, it is no good; push
501 * back the sign. If the number ends in `x',
502 * it was [sign] '0' 'x', so push back the x
503 * and treat it as [sign] '0'.
505 if (flags & NDIGITS) {
512 c = ((u_char *)p)[-1];
513 if (c == 'x' || c == 'X') {
518 if ((flags & SUPPRESS) == 0) {
522 res = (*ccfn)(buf, (char **)NULL, base);
524 *va_arg(ap, void **) =
525 (void *)(uintptr_t)res;
526 else if (flags & SHORTSHORT)
527 *va_arg(ap, char *) = res;
528 else if (flags & SHORT)
529 *va_arg(ap, short *) = res;
530 else if (flags & LONG)
531 *va_arg(ap, long *) = res;
532 else if (flags & QUAD)
533 *va_arg(ap, quad_t *) = res;
535 *va_arg(ap, int *) = res;
545 return (nconversions != 0 ? nassigned : -1);
551 * Fill in the given table from the scanset at the given format
552 * (just after `['). Return a pointer to the character past the
553 * closing `]'. The table has a 1 wherever characters should be
554 * considered part of the scanset.
556 static const u_char *
557 __sccl(char *tab, const u_char *fmt)
561 /* first `clear' the whole table */
562 c = *fmt++; /* first char hat => negated scanset */
564 v = 1; /* default => accept */
565 c = *fmt++; /* get new first char */
567 v = 0; /* default => reject */
569 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
570 for (n = 0; n < 256; n++)
571 tab[n] = v; /* memset(tab, v, 256) */
574 return (fmt - 1);/* format ended before closing ] */
577 * Now set the entries corresponding to the actual scanset
578 * to the opposite of the above.
580 * The first character may be ']' (or '-') without being special;
581 * the last character may be '-'.
585 tab[c] = v; /* take character c */
587 n = *fmt++; /* and examine the next */
590 case 0: /* format ended too soon */
595 * A scanset of the form
597 * is defined as `the digit 0, the digit 1,
598 * the character +, the character -', but
599 * the effect of a scanset such as
601 * is implementation defined. The V7 Unix
602 * scanf treats `a-z' as `the letters a through
603 * z', but treats `a-a' as `the letter a, the
604 * character -, and the letter a'.
606 * For compatibility, the `-' is not considerd
607 * to define a range if the character following
608 * it is either a close bracket (required by ANSI)
609 * or is not numerically greater than the character
610 * we just stored in the table (c).
613 if (n == ']' || n < c) {
615 break; /* resume the for(;;) */
618 /* fill in the range */
624 * Alas, the V7 Unix scanf also treats formats
625 * such as [a-c-e] as `the letters a through e'.
626 * This too is permitted by the standard....
631 case ']': /* end of scanset */
634 default: /* just another character */