1 /* $OpenBSD: vfscanf.c,v 1.21 2006/01/13 21:33:28 millert Exp $ */
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 #define BUF 513 /* Maximum length of numeric string. */
49 * Flags used during conversion.
51 #define LONG 0x00001 /* l: long or double */
52 #define LONGDBL 0x00002 /* L: long double; unimplemented */
53 #define SHORT 0x00004 /* h: short */
54 #define SHORTSHORT 0x00008 /* hh: 8 bit integer */
55 #define LLONG 0x00010 /* ll: long long (+ deprecated q: quad) */
56 #define POINTER 0x00020 /* p: void * (as hex) */
57 #define SIZEINT 0x00040 /* z: (signed) size_t */
58 #define MAXINT 0x00080 /* j: intmax_t */
59 #define PTRINT 0x00100 /* t: ptrdiff_t */
60 #define NOSKIP 0x00200 /* [ or c: do not skip blanks */
61 #define SUPPRESS 0x00400 /* *: suppress assignment */
62 #define UNSIGNED 0x00800 /* %[oupxX] conversions */
65 * The following are used in numeric conversions only:
66 * SIGNOK, HAVESIGN, NDIGITS, DPTOK, and EXPOK are for floating point;
67 * SIGNOK, HAVESIGN, NDIGITS, PFXOK, and NZDIGITS are for integral.
69 #define SIGNOK 0x01000 /* +/- is (still) legal */
70 #define HAVESIGN 0x02000 /* sign detected */
71 #define NDIGITS 0x04000 /* no digits detected */
73 #define DPTOK 0x08000 /* (float) decimal point is still legal */
74 #define EXPOK 0x10000 /* (float) exponent (e+3, etc) still legal */
76 #define PFXOK 0x08000 /* 0x prefix is (still) legal */
77 #define NZDIGITS 0x10000 /* 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., strtoimax or strtoumax */
86 #define CT_FLOAT 4 /* floating, i.e., strtod */
88 #define u_char unsigned char
89 #define u_long unsigned long
91 static u_char *__sccl(char *, u_char *);
94 #define VFSCANF vfscanf
101 VFSCANF(FILE *fp, const char *fmt0, __va_list ap)
103 u_char *fmt = (u_char *)fmt0;
104 int c; /* character from format, or conversion */
105 size_t width; /* field width, or 0 */
106 char *p; /* points into all kinds of strings */
107 int n; /* handy integer */
108 int flags; /* flags as defined above */
109 char *p0; /* saves original value of p when necessary */
110 int nassigned; /* number of fields assigned */
111 int nread; /* number of characters consumed from fp */
112 int base; /* base argument to strtoimax/strtouimax */
113 char ccltab[256]; /* character class table for %[...] */
114 char buf[BUF]; /* buffer for numeric conversions */
116 /* `basefix' is used to avoid `if' tests in the integer scanner */
117 static short basefix[17] =
118 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
120 _SET_ORIENTATION(fp, -1);
124 base = 0; /* XXX just to keep gcc happy */
130 while ((fp->_r > 0 || __srefill(fp) == 0) &&
132 nread++, fp->_r--, fp->_p++;
140 * switch on the format. continue if done;
141 * break once format type is derived.
147 if (fp->_r <= 0 && __srefill(fp))
181 flags |= LLONG; /* deprecated */
190 case '0': case '1': case '2': case '3': case '4':
191 case '5': case '6': case '7': case '8': case '9':
192 width = width * 10 + c - '0';
197 * Those marked `compat' are for 4.[123]BSD compatibility.
199 * (According to ANSI, E and X formats are supposed
200 * to the same as e and x. Sorry about that.)
202 case 'D': /* compat */
215 case 'O': /* compat */
232 flags |= PFXOK; /* enable 0x prefixing */
238 #ifdef FLOATING_POINT
253 fmt = __sccl(ccltab, fmt);
263 case 'p': /* pointer format is like hex */
264 flags |= POINTER | PFXOK;
271 if (flags & SUPPRESS)
273 if (flags & SHORTSHORT)
274 *va_arg(ap, __signed char *) = nread;
275 else if (flags & SHORT)
276 *va_arg(ap, short *) = nread;
277 else if (flags & LONG)
278 *va_arg(ap, long *) = nread;
279 else if (flags & SIZEINT)
280 *va_arg(ap, ssize_t *) = nread;
281 else if (flags & PTRINT)
282 *va_arg(ap, ptrdiff_t *) = nread;
283 else if (flags & LLONG)
284 *va_arg(ap, long long *) = nread;
285 else if (flags & MAXINT)
286 *va_arg(ap, intmax_t *) = nread;
288 *va_arg(ap, int *) = nread;
292 * Disgusting backwards compatibility hacks. XXX
294 case '\0': /* compat */
297 default: /* compat */
306 * We have a conversion that requires input.
308 if (fp->_r <= 0 && __srefill(fp))
312 * Consume leading white space, except for formats
313 * that suppress this.
315 if ((flags & NOSKIP) == 0) {
316 while (isspace(*fp->_p)) {
320 else if (__srefill(fp))
324 * Note that there is at least one character in
325 * the buffer, so conversions that do not set NOSKIP
326 * ca no longer result in an input failure.
336 /* scan arbitrary characters (sets NOSKIP) */
339 if (flags & SUPPRESS) {
342 if ((n = fp->_r) < (int)width) {
360 size_t r = fread((void *)va_arg(ap, char *), 1,
371 /* scan a (nonempty) character class (sets NOSKIP) */
373 width = (size_t)~0; /* `infinity' */
374 /* take only those things in the class */
375 if (flags & SUPPRESS) {
377 while (ccltab[*fp->_p]) {
378 n++, fp->_r--, fp->_p++;
381 if (fp->_r <= 0 && __srefill(fp)) {
390 p0 = p = va_arg(ap, char *);
391 while (ccltab[*fp->_p]) {
396 if (fp->_r <= 0 && __srefill(fp)) {
412 /* like CCL, but zero-length string OK, & no NOSKIP */
415 if (flags & SUPPRESS) {
417 while (!isspace(*fp->_p)) {
418 n++, fp->_r--, fp->_p++;
421 if (fp->_r <= 0 && __srefill(fp))
426 p0 = p = va_arg(ap, char *);
427 while (!isspace(*fp->_p)) {
432 if (fp->_r <= 0 && __srefill(fp))
442 /* scan an integer as if by strtoimax/strtoumax */
444 if (width == 0 || width > sizeof(buf) - 1)
445 width = sizeof(buf) - 1;
447 /* size_t is unsigned, hence this optimisation */
448 if (--width > sizeof(buf) - 2)
449 width = sizeof(buf) - 2;
452 flags |= SIGNOK | NDIGITS | NZDIGITS;
453 for (p = buf; width; width--) {
456 * Switch on the character; `goto ok'
457 * if we accept it as a part of number.
462 * The digit 0 is always legal, but is
463 * special. For %i conversions, if no
464 * digits (zero or nonzero) have been
465 * scanned (only signs), we will have
466 * base==0. In that case, we should set
467 * it to 8 and enable 0x prefixing.
468 * Also, if we have not scanned zero digits
469 * before this, do not turn off prefixing
470 * (someone else will turn it off if we
471 * have scanned any nonzero digits).
478 if (flags & NZDIGITS)
479 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
481 flags &= ~(SIGNOK|PFXOK|NDIGITS);
484 /* 1 through 7 always legal */
485 case '1': case '2': case '3':
486 case '4': case '5': case '6': case '7':
487 base = basefix[base];
488 flags &= ~(SIGNOK | PFXOK | NDIGITS);
491 /* digits 8 and 9 ok iff decimal or hex */
493 base = basefix[base];
495 break; /* not legal here */
496 flags &= ~(SIGNOK | PFXOK | NDIGITS);
499 /* letters ok iff hex */
500 case 'A': case 'B': case 'C':
501 case 'D': case 'E': case 'F':
502 case 'a': case 'b': case 'c':
503 case 'd': case 'e': case 'f':
504 /* no need to fix base here */
506 break; /* not legal here */
507 flags &= ~(SIGNOK | PFXOK | NDIGITS);
510 /* sign ok only as first character */
512 if (flags & SIGNOK) {
520 * x ok iff flag still set and 2nd char (or
521 * 3rd char if we have a sign).
524 if ((flags & PFXOK) && p ==
525 buf + 1 + !!(flags & HAVESIGN)) {
526 base = 16; /* if %i */
534 * If we got here, c is not a legal character
535 * for a number. Stop accumulating digits.
540 * c is legal: store it and look at the next.
545 else if (__srefill(fp))
549 * If we had only a sign, it is no good; push
550 * back the sign. If the number ends in `x',
551 * it was [sign] '0' 'x', so push back the x
552 * and treat it as [sign] '0'.
554 if (flags & NDIGITS) {
556 (void) ungetc(*(u_char *)--p, fp);
559 c = ((u_char *)p)[-1];
560 if (c == 'x' || c == 'X') {
562 (void) ungetc(c, fp);
564 if ((flags & SUPPRESS) == 0) {
568 if (flags & UNSIGNED)
569 res = strtoumax(buf, NULL, base);
571 res = strtoimax(buf, NULL, base);
573 *va_arg(ap, void **) =
574 (void *)(uintptr_t)res;
575 else if (flags & MAXINT)
576 *va_arg(ap, intmax_t *) = res;
577 else if (flags & LLONG)
578 *va_arg(ap, long long *) = res;
579 else if (flags & SIZEINT)
580 *va_arg(ap, ssize_t *) = res;
581 else if (flags & PTRINT)
582 *va_arg(ap, ptrdiff_t *) = res;
583 else if (flags & LONG)
584 *va_arg(ap, long *) = res;
585 else if (flags & SHORT)
586 *va_arg(ap, short *) = res;
587 else if (flags & SHORTSHORT)
588 *va_arg(ap, __signed char *) = res;
590 *va_arg(ap, int *) = res;
596 #ifdef FLOATING_POINT
598 /* scan a floating point number as if by strtod */
600 if (width == 0 || width > sizeof(buf) - 1)
601 width = sizeof(buf) - 1;
603 /* size_t is unsigned, hence this optimisation */
604 if (--width > sizeof(buf) - 2)
605 width = sizeof(buf) - 2;
608 flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
609 for (p = buf; width; width--) {
612 * This code mimicks the integer conversion
613 * code, but is much simpler.
617 case '0': case '1': case '2': case '3':
618 case '4': case '5': case '6': case '7':
620 flags &= ~(SIGNOK | NDIGITS);
624 if (flags & SIGNOK) {
631 flags &= ~(SIGNOK | DPTOK);
636 /* no exponent without some digits */
637 if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
639 (flags & ~(EXPOK|DPTOK)) |
650 else if (__srefill(fp))
654 * If no digits, might be missing exponent digits
655 * (just give back the exponent) or might be missing
656 * regular digits, but had sign and/or decimal point.
658 if (flags & NDIGITS) {
660 /* no digits at all */
662 ungetc(*(u_char *)--p, fp);
665 /* just a bad exponent (e and maybe sign) */
667 if (c != 'e' && c != 'E') {
668 (void) ungetc(c, fp);/* sign */
671 (void) ungetc(c, fp);
673 if ((flags & SUPPRESS) == 0) {
677 res = strtod(buf, (char **) NULL);
679 *va_arg(ap, long double *) = res;
680 else if (flags & LONG)
681 *va_arg(ap, double *) = res;
683 *va_arg(ap, float *) = res;
688 #endif /* FLOATING_POINT */
692 return (nassigned ? nassigned : -1);
698 * Fill in the given table from the scanset at the given format
699 * (just after `['). Return a pointer to the character past the
700 * closing `]'. The table has a 1 wherever characters should be
701 * considered part of the scanset.
704 __sccl(char *tab, u_char *fmt)
708 /* first `clear' the whole table */
709 c = *fmt++; /* first char hat => negated scanset */
711 v = 1; /* default => accept */
712 c = *fmt++; /* get new first char */
714 v = 0; /* default => reject */
715 /* should probably use memset here */
716 for (n = 0; n < 256; n++)
719 return (fmt - 1);/* format ended before closing ] */
722 * Now set the entries corresponding to the actual scanset
723 * to the opposite of the above.
725 * The first character may be ']' (or '-') without being special;
726 * the last character may be '-'.
730 tab[c] = v; /* take character c */
732 n = *fmt++; /* and examine the next */
735 case 0: /* format ended too soon */
740 * A scanset of the form
742 * is defined as `the digit 0, the digit 1,
743 * the character +, the character -', but
744 * the effect of a scanset such as
746 * is implementation defined. The V7 Unix
747 * scanf treats `a-z' as `the letters a through
748 * z', but treats `a-a' as `the letter a, the
749 * character -, and the letter a'.
751 * For compatibility, the `-' is not considerd
752 * to define a range if the character following
753 * it is either a close bracket (required by ANSI)
754 * or is not numerically greater than the character
755 * we just stored in the table (c).
758 if (n == ']' || n < c) {
760 break; /* resume the for(;;) */
763 do { /* fill in the range */
766 #if 1 /* XXX another disgusting compatibility hack */
768 * Alas, the V7 Unix scanf also treats formats
769 * such as [a-c-e] as `the letters a through e'.
770 * This too is permitted by the standard....
782 case ']': /* end of scanset */
785 default: /* just another character */