Home | History | Annotate | Line # | Download | only in stdio
vfscanf.c revision 1.12.2.1
      1  1.12.2.1      jtc /*	$NetBSD: vfscanf.c,v 1.12.2.1 1995/02/17 11:00:53 jtc Exp $	*/
      2      1.12      jtc 
      3       1.1      cgd /*-
      4      1.12      jtc  * Copyright (c) 1990, 1993
      5      1.12      jtc  *	The Regents of the University of California.  All rights reserved.
      6       1.1      cgd  *
      7       1.1      cgd  * This code is derived from software contributed to Berkeley by
      8       1.1      cgd  * Chris Torek.
      9       1.1      cgd  *
     10       1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11       1.1      cgd  * modification, are permitted provided that the following conditions
     12       1.1      cgd  * are met:
     13       1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14       1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15       1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17       1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18       1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19       1.1      cgd  *    must display the following acknowledgement:
     20       1.1      cgd  *	This product includes software developed by the University of
     21       1.1      cgd  *	California, Berkeley and its contributors.
     22       1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23       1.1      cgd  *    may be used to endorse or promote products derived from this software
     24       1.1      cgd  *    without specific prior written permission.
     25       1.1      cgd  *
     26       1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27       1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28       1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29       1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30       1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31       1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32       1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33       1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34       1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35       1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36       1.1      cgd  * SUCH DAMAGE.
     37       1.1      cgd  */
     38       1.1      cgd 
     39       1.1      cgd #if defined(LIBC_SCCS) && !defined(lint)
     40      1.12      jtc #if 0
     41      1.12      jtc static char sccsid[] = "@(#)vfscanf.c	8.1 (Berkeley) 6/4/93";
     42      1.12      jtc #endif
     43  1.12.2.1      jtc static char rcsid[] = "$NetBSD: vfscanf.c,v 1.12.2.1 1995/02/17 11:00:53 jtc Exp $";
     44       1.1      cgd #endif /* LIBC_SCCS and not lint */
     45       1.1      cgd 
     46  1.12.2.1      jtc #include "namespace.h"
     47       1.1      cgd #include <stdio.h>
     48       1.1      cgd #include <stdlib.h>
     49       1.1      cgd #include <ctype.h>
     50       1.1      cgd #if __STDC__
     51       1.1      cgd #include <stdarg.h>
     52       1.1      cgd #else
     53       1.1      cgd #include <varargs.h>
     54       1.1      cgd #endif
     55       1.1      cgd #include "local.h"
     56       1.1      cgd 
     57       1.8  mycroft #ifdef FLOATING_POINT
     58       1.8  mycroft #include "floatio.h"
     59       1.8  mycroft #endif
     60       1.1      cgd 
     61       1.3      cgd #define	BUF		513	/* Maximum length of numeric string. */
     62       1.1      cgd 
     63       1.1      cgd /*
     64       1.1      cgd  * Flags used during conversion.
     65       1.1      cgd  */
     66       1.1      cgd #define	LONG		0x01	/* l: long or double */
     67       1.1      cgd #define	LONGDBL		0x02	/* L: long double; unimplemented */
     68       1.1      cgd #define	SHORT		0x04	/* h: short */
     69      1.10      jtc #define QUAD		0x08	/* q: quad */
     70      1.10      jtc #define	SUPPRESS	0x10	/* suppress assignment */
     71      1.10      jtc #define	POINTER		0x20	/* weird %p pointer (`fake hex') */
     72      1.10      jtc #define	NOSKIP		0x40	/* do not skip blanks */
     73       1.1      cgd 
     74       1.1      cgd /*
     75       1.1      cgd  * The following are used in numeric conversions only:
     76       1.1      cgd  * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
     77       1.1      cgd  * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
     78       1.1      cgd  */
     79      1.10      jtc #define	SIGNOK		0x080	/* +/- is (still) legal */
     80      1.10      jtc #define	NDIGITS		0x100	/* no digits detected */
     81       1.1      cgd 
     82      1.10      jtc #define	DPTOK		0x200	/* (float) decimal point is still legal */
     83      1.10      jtc #define	EXPOK		0x400	/* (float) exponent (e+3, etc) still legal */
     84       1.1      cgd 
     85      1.10      jtc #define	PFXOK		0x200	/* 0x prefix is (still) legal */
     86      1.10      jtc #define	NZDIGITS	0x400	/* no zero digits detected */
     87       1.1      cgd 
     88       1.1      cgd /*
     89       1.1      cgd  * Conversion types.
     90       1.1      cgd  */
     91       1.1      cgd #define	CT_CHAR		0	/* %c conversion */
     92       1.1      cgd #define	CT_CCL		1	/* %[...] conversion */
     93       1.1      cgd #define	CT_STRING	2	/* %s conversion */
     94      1.10      jtc #define	CT_INT		3	/* integer, i.e., strtoq or strtouq */
     95       1.1      cgd #define	CT_FLOAT	4	/* floating, i.e., strtod */
     96       1.1      cgd 
     97       1.1      cgd #define u_char unsigned char
     98       1.1      cgd #define u_long unsigned long
     99       1.1      cgd 
    100       1.1      cgd static u_char *__sccl();
    101       1.1      cgd 
    102       1.1      cgd /*
    103       1.1      cgd  * vfscanf
    104       1.1      cgd  */
    105       1.1      cgd __svfscanf(fp, fmt0, ap)
    106       1.1      cgd 	register FILE *fp;
    107       1.1      cgd 	char const *fmt0;
    108       1.7      cgd 	_BSD_VA_LIST_ ap;
    109       1.1      cgd {
    110       1.1      cgd 	register u_char *fmt = (u_char *)fmt0;
    111       1.1      cgd 	register int c;		/* character from format, or conversion */
    112       1.1      cgd 	register size_t width;	/* field width, or 0 */
    113       1.1      cgd 	register char *p;	/* points into all kinds of strings */
    114       1.1      cgd 	register int n;		/* handy integer */
    115       1.1      cgd 	register int flags;	/* flags as defined above */
    116       1.1      cgd 	register char *p0;	/* saves original value of p when necessary */
    117       1.1      cgd 	int nassigned;		/* number of fields assigned */
    118       1.1      cgd 	int nread;		/* number of characters consumed from fp */
    119      1.10      jtc 	int base;		/* base argument to strtoq/strtouq */
    120      1.10      jtc 	u_quad_t (*ccfn)();	/* conversion function (strtoq/strtouq) */
    121       1.1      cgd 	char ccltab[256];	/* character class table for %[...] */
    122       1.1      cgd 	char buf[BUF];		/* buffer for numeric conversions */
    123       1.1      cgd 
    124       1.1      cgd 	/* `basefix' is used to avoid `if' tests in the integer scanner */
    125       1.1      cgd 	static short basefix[17] =
    126       1.1      cgd 		{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
    127       1.1      cgd 
    128       1.1      cgd 	nassigned = 0;
    129       1.1      cgd 	nread = 0;
    130       1.1      cgd 	base = 0;		/* XXX just to keep gcc happy */
    131       1.1      cgd 	ccfn = NULL;		/* XXX just to keep gcc happy */
    132       1.1      cgd 	for (;;) {
    133       1.1      cgd 		c = *fmt++;
    134       1.1      cgd 		if (c == 0)
    135       1.1      cgd 			return (nassigned);
    136       1.1      cgd 		if (isspace(c)) {
    137       1.1      cgd 			for (;;) {
    138       1.1      cgd 				if (fp->_r <= 0 && __srefill(fp))
    139       1.1      cgd 					return (nassigned);
    140       1.1      cgd 				if (!isspace(*fp->_p))
    141       1.1      cgd 					break;
    142       1.1      cgd 				nread++, fp->_r--, fp->_p++;
    143       1.1      cgd 			}
    144       1.1      cgd 			continue;
    145       1.1      cgd 		}
    146       1.1      cgd 		if (c != '%')
    147       1.1      cgd 			goto literal;
    148       1.1      cgd 		width = 0;
    149       1.1      cgd 		flags = 0;
    150       1.1      cgd 		/*
    151       1.1      cgd 		 * switch on the format.  continue if done;
    152       1.1      cgd 		 * break once format type is derived.
    153       1.1      cgd 		 */
    154       1.1      cgd again:		c = *fmt++;
    155       1.1      cgd 		switch (c) {
    156       1.1      cgd 		case '%':
    157       1.1      cgd literal:
    158       1.1      cgd 			if (fp->_r <= 0 && __srefill(fp))
    159       1.1      cgd 				goto input_failure;
    160       1.1      cgd 			if (*fp->_p != c)
    161       1.1      cgd 				goto match_failure;
    162       1.1      cgd 			fp->_r--, fp->_p++;
    163       1.1      cgd 			nread++;
    164       1.1      cgd 			continue;
    165       1.1      cgd 
    166       1.1      cgd 		case '*':
    167       1.1      cgd 			flags |= SUPPRESS;
    168       1.1      cgd 			goto again;
    169       1.1      cgd 		case 'L':
    170       1.1      cgd 			flags |= LONGDBL;
    171       1.1      cgd 			goto again;
    172       1.1      cgd 		case 'h':
    173       1.1      cgd 			flags |= SHORT;
    174       1.1      cgd 			goto again;
    175      1.10      jtc 		case 'l':
    176      1.10      jtc 			flags |= LONG;
    177      1.10      jtc 			goto again;
    178      1.10      jtc 		case 'q':
    179      1.10      jtc 			flags |= QUAD;
    180      1.10      jtc 			goto again;
    181       1.1      cgd 
    182       1.1      cgd 		case '0': case '1': case '2': case '3': case '4':
    183       1.1      cgd 		case '5': case '6': case '7': case '8': case '9':
    184       1.1      cgd 			width = width * 10 + c - '0';
    185       1.1      cgd 			goto again;
    186       1.1      cgd 
    187       1.1      cgd 		/*
    188       1.1      cgd 		 * Conversions.
    189       1.1      cgd 		 * Those marked `compat' are for 4.[123]BSD compatibility.
    190       1.1      cgd 		 *
    191       1.1      cgd 		 * (According to ANSI, E and X formats are supposed
    192       1.1      cgd 		 * to the same as e and x.  Sorry about that.)
    193       1.1      cgd 		 */
    194       1.1      cgd 		case 'D':	/* compat */
    195       1.1      cgd 			flags |= LONG;
    196       1.1      cgd 			/* FALLTHROUGH */
    197       1.1      cgd 		case 'd':
    198       1.1      cgd 			c = CT_INT;
    199      1.10      jtc 			ccfn = (u_quad_t (*)())strtoq;
    200       1.1      cgd 			base = 10;
    201       1.1      cgd 			break;
    202       1.1      cgd 
    203       1.1      cgd 		case 'i':
    204       1.1      cgd 			c = CT_INT;
    205      1.10      jtc 			ccfn = (u_quad_t (*)())strtoq;
    206       1.1      cgd 			base = 0;
    207       1.1      cgd 			break;
    208       1.1      cgd 
    209       1.1      cgd 		case 'O':	/* compat */
    210       1.1      cgd 			flags |= LONG;
    211       1.1      cgd 			/* FALLTHROUGH */
    212       1.1      cgd 		case 'o':
    213       1.1      cgd 			c = CT_INT;
    214      1.10      jtc 			ccfn = strtouq;
    215       1.1      cgd 			base = 8;
    216       1.1      cgd 			break;
    217       1.1      cgd 
    218       1.1      cgd 		case 'u':
    219       1.1      cgd 			c = CT_INT;
    220      1.10      jtc 			ccfn = strtouq;
    221       1.1      cgd 			base = 10;
    222       1.1      cgd 			break;
    223       1.1      cgd 
    224       1.9      jtc 		case 'X':
    225       1.1      cgd 		case 'x':
    226       1.1      cgd 			flags |= PFXOK;	/* enable 0x prefixing */
    227       1.1      cgd 			c = CT_INT;
    228      1.10      jtc 			ccfn = strtouq;
    229       1.1      cgd 			base = 16;
    230       1.1      cgd 			break;
    231       1.1      cgd 
    232       1.1      cgd #ifdef FLOATING_POINT
    233       1.9      jtc 		case 'E':
    234       1.9      jtc 		case 'G':
    235       1.9      jtc 		case 'e':
    236      1.10      jtc 		case 'f':
    237       1.9      jtc 		case 'g':
    238       1.1      cgd 			c = CT_FLOAT;
    239       1.1      cgd 			break;
    240       1.1      cgd #endif
    241       1.1      cgd 
    242       1.1      cgd 		case 's':
    243       1.1      cgd 			c = CT_STRING;
    244       1.1      cgd 			break;
    245       1.1      cgd 
    246       1.1      cgd 		case '[':
    247       1.1      cgd 			fmt = __sccl(ccltab, fmt);
    248       1.1      cgd 			flags |= NOSKIP;
    249       1.1      cgd 			c = CT_CCL;
    250       1.1      cgd 			break;
    251       1.1      cgd 
    252       1.1      cgd 		case 'c':
    253       1.1      cgd 			flags |= NOSKIP;
    254       1.1      cgd 			c = CT_CHAR;
    255       1.1      cgd 			break;
    256       1.1      cgd 
    257       1.1      cgd 		case 'p':	/* pointer format is like hex */
    258       1.1      cgd 			flags |= POINTER | PFXOK;
    259       1.1      cgd 			c = CT_INT;
    260      1.10      jtc 			ccfn = strtouq;
    261       1.1      cgd 			base = 16;
    262       1.1      cgd 			break;
    263       1.1      cgd 
    264       1.1      cgd 		case 'n':
    265       1.1      cgd 			if (flags & SUPPRESS)	/* ??? */
    266       1.1      cgd 				continue;
    267       1.1      cgd 			if (flags & SHORT)
    268       1.1      cgd 				*va_arg(ap, short *) = nread;
    269       1.1      cgd 			else if (flags & LONG)
    270       1.1      cgd 				*va_arg(ap, long *) = nread;
    271       1.1      cgd 			else
    272       1.1      cgd 				*va_arg(ap, int *) = nread;
    273       1.1      cgd 			continue;
    274       1.1      cgd 
    275       1.1      cgd 		/*
    276       1.1      cgd 		 * Disgusting backwards compatibility hacks.	XXX
    277       1.1      cgd 		 */
    278       1.1      cgd 		case '\0':	/* compat */
    279       1.1      cgd 			return (EOF);
    280       1.1      cgd 
    281       1.1      cgd 		default:	/* compat */
    282       1.1      cgd 			if (isupper(c))
    283       1.1      cgd 				flags |= LONG;
    284       1.1      cgd 			c = CT_INT;
    285      1.10      jtc 			ccfn = (u_quad_t (*)())strtoq;
    286       1.1      cgd 			base = 10;
    287       1.1      cgd 			break;
    288       1.1      cgd 		}
    289       1.1      cgd 
    290       1.1      cgd 		/*
    291       1.1      cgd 		 * We have a conversion that requires input.
    292       1.1      cgd 		 */
    293       1.1      cgd 		if (fp->_r <= 0 && __srefill(fp))
    294       1.1      cgd 			goto input_failure;
    295       1.1      cgd 
    296       1.1      cgd 		/*
    297       1.1      cgd 		 * Consume leading white space, except for formats
    298       1.1      cgd 		 * that suppress this.
    299       1.1      cgd 		 */
    300       1.1      cgd 		if ((flags & NOSKIP) == 0) {
    301       1.1      cgd 			while (isspace(*fp->_p)) {
    302       1.1      cgd 				nread++;
    303       1.1      cgd 				if (--fp->_r > 0)
    304       1.1      cgd 					fp->_p++;
    305       1.1      cgd 				else if (__srefill(fp))
    306       1.1      cgd 					goto input_failure;
    307       1.1      cgd 			}
    308       1.1      cgd 			/*
    309       1.1      cgd 			 * Note that there is at least one character in
    310       1.1      cgd 			 * the buffer, so conversions that do not set NOSKIP
    311       1.1      cgd 			 * ca no longer result in an input failure.
    312       1.1      cgd 			 */
    313       1.1      cgd 		}
    314       1.1      cgd 
    315       1.1      cgd 		/*
    316       1.1      cgd 		 * Do the conversion.
    317       1.1      cgd 		 */
    318       1.1      cgd 		switch (c) {
    319       1.1      cgd 
    320       1.1      cgd 		case CT_CHAR:
    321       1.1      cgd 			/* scan arbitrary characters (sets NOSKIP) */
    322       1.1      cgd 			if (width == 0)
    323       1.1      cgd 				width = 1;
    324       1.1      cgd 			if (flags & SUPPRESS) {
    325       1.1      cgd 				size_t sum = 0;
    326       1.1      cgd 				for (;;) {
    327       1.1      cgd 					if ((n = fp->_r) < width) {
    328       1.1      cgd 						sum += n;
    329       1.1      cgd 						width -= n;
    330       1.1      cgd 						fp->_p += n;
    331       1.1      cgd 						if (__srefill(fp)) {
    332       1.1      cgd 							if (sum == 0)
    333       1.1      cgd 							    goto input_failure;
    334       1.1      cgd 							break;
    335       1.1      cgd 						}
    336       1.1      cgd 					} else {
    337       1.1      cgd 						sum += width;
    338       1.1      cgd 						fp->_r -= width;
    339       1.1      cgd 						fp->_p += width;
    340       1.1      cgd 						break;
    341       1.1      cgd 					}
    342       1.1      cgd 				}
    343       1.1      cgd 				nread += sum;
    344       1.1      cgd 			} else {
    345       1.1      cgd 				size_t r = fread((void *)va_arg(ap, char *), 1,
    346       1.1      cgd 				    width, fp);
    347       1.1      cgd 
    348       1.1      cgd 				if (r == 0)
    349       1.1      cgd 					goto input_failure;
    350       1.1      cgd 				nread += r;
    351       1.1      cgd 				nassigned++;
    352       1.1      cgd 			}
    353       1.1      cgd 			break;
    354       1.1      cgd 
    355       1.1      cgd 		case CT_CCL:
    356       1.1      cgd 			/* scan a (nonempty) character class (sets NOSKIP) */
    357       1.1      cgd 			if (width == 0)
    358       1.1      cgd 				width = ~0;	/* `infinity' */
    359       1.1      cgd 			/* take only those things in the class */
    360       1.1      cgd 			if (flags & SUPPRESS) {
    361       1.1      cgd 				n = 0;
    362       1.1      cgd 				while (ccltab[*fp->_p]) {
    363       1.1      cgd 					n++, fp->_r--, fp->_p++;
    364       1.1      cgd 					if (--width == 0)
    365       1.1      cgd 						break;
    366       1.1      cgd 					if (fp->_r <= 0 && __srefill(fp)) {
    367       1.1      cgd 						if (n == 0)
    368       1.1      cgd 							goto input_failure;
    369       1.1      cgd 						break;
    370       1.1      cgd 					}
    371       1.1      cgd 				}
    372       1.1      cgd 				if (n == 0)
    373       1.1      cgd 					goto match_failure;
    374       1.1      cgd 			} else {
    375       1.1      cgd 				p0 = p = va_arg(ap, char *);
    376       1.1      cgd 				while (ccltab[*fp->_p]) {
    377       1.1      cgd 					fp->_r--;
    378       1.1      cgd 					*p++ = *fp->_p++;
    379       1.1      cgd 					if (--width == 0)
    380       1.1      cgd 						break;
    381       1.1      cgd 					if (fp->_r <= 0 && __srefill(fp)) {
    382       1.1      cgd 						if (p == p0)
    383       1.1      cgd 							goto input_failure;
    384       1.1      cgd 						break;
    385       1.1      cgd 					}
    386       1.1      cgd 				}
    387       1.1      cgd 				n = p - p0;
    388       1.1      cgd 				if (n == 0)
    389       1.1      cgd 					goto match_failure;
    390       1.1      cgd 				*p = 0;
    391       1.1      cgd 				nassigned++;
    392       1.1      cgd 			}
    393       1.1      cgd 			nread += n;
    394       1.1      cgd 			break;
    395       1.1      cgd 
    396       1.1      cgd 		case CT_STRING:
    397       1.1      cgd 			/* like CCL, but zero-length string OK, & no NOSKIP */
    398       1.1      cgd 			if (width == 0)
    399       1.1      cgd 				width = ~0;
    400       1.1      cgd 			if (flags & SUPPRESS) {
    401       1.1      cgd 				n = 0;
    402       1.1      cgd 				while (!isspace(*fp->_p)) {
    403       1.1      cgd 					n++, fp->_r--, fp->_p++;
    404       1.1      cgd 					if (--width == 0)
    405       1.1      cgd 						break;
    406       1.1      cgd 					if (fp->_r <= 0 && __srefill(fp))
    407       1.1      cgd 						break;
    408       1.1      cgd 				}
    409       1.1      cgd 				nread += n;
    410       1.1      cgd 			} else {
    411       1.1      cgd 				p0 = p = va_arg(ap, char *);
    412       1.1      cgd 				while (!isspace(*fp->_p)) {
    413       1.1      cgd 					fp->_r--;
    414       1.1      cgd 					*p++ = *fp->_p++;
    415       1.1      cgd 					if (--width == 0)
    416       1.1      cgd 						break;
    417       1.1      cgd 					if (fp->_r <= 0 && __srefill(fp))
    418       1.1      cgd 						break;
    419       1.1      cgd 				}
    420       1.1      cgd 				*p = 0;
    421       1.1      cgd 				nread += p - p0;
    422       1.1      cgd 				nassigned++;
    423       1.1      cgd 			}
    424       1.1      cgd 			continue;
    425       1.1      cgd 
    426       1.1      cgd 		case CT_INT:
    427      1.10      jtc 			/* scan an integer as if by strtoq/strtouq */
    428       1.1      cgd #ifdef hardway
    429       1.1      cgd 			if (width == 0 || width > sizeof(buf) - 1)
    430       1.1      cgd 				width = sizeof(buf) - 1;
    431       1.1      cgd #else
    432       1.1      cgd 			/* size_t is unsigned, hence this optimisation */
    433       1.1      cgd 			if (--width > sizeof(buf) - 2)
    434       1.1      cgd 				width = sizeof(buf) - 2;
    435       1.1      cgd 			width++;
    436       1.1      cgd #endif
    437       1.1      cgd 			flags |= SIGNOK | NDIGITS | NZDIGITS;
    438       1.1      cgd 			for (p = buf; width; width--) {
    439       1.1      cgd 				c = *fp->_p;
    440       1.1      cgd 				/*
    441       1.1      cgd 				 * Switch on the character; `goto ok'
    442       1.1      cgd 				 * if we accept it as a part of number.
    443       1.1      cgd 				 */
    444       1.1      cgd 				switch (c) {
    445       1.1      cgd 
    446       1.1      cgd 				/*
    447       1.1      cgd 				 * The digit 0 is always legal, but is
    448       1.1      cgd 				 * special.  For %i conversions, if no
    449       1.1      cgd 				 * digits (zero or nonzero) have been
    450       1.1      cgd 				 * scanned (only signs), we will have
    451       1.1      cgd 				 * base==0.  In that case, we should set
    452       1.1      cgd 				 * it to 8 and enable 0x prefixing.
    453       1.1      cgd 				 * Also, if we have not scanned zero digits
    454       1.1      cgd 				 * before this, do not turn off prefixing
    455       1.1      cgd 				 * (someone else will turn it off if we
    456       1.1      cgd 				 * have scanned any nonzero digits).
    457       1.1      cgd 				 */
    458       1.1      cgd 				case '0':
    459       1.1      cgd 					if (base == 0) {
    460       1.1      cgd 						base = 8;
    461       1.1      cgd 						flags |= PFXOK;
    462       1.1      cgd 					}
    463       1.1      cgd 					if (flags & NZDIGITS)
    464       1.1      cgd 					    flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
    465       1.1      cgd 					else
    466       1.1      cgd 					    flags &= ~(SIGNOK|PFXOK|NDIGITS);
    467       1.1      cgd 					goto ok;
    468       1.1      cgd 
    469       1.1      cgd 				/* 1 through 7 always legal */
    470       1.1      cgd 				case '1': case '2': case '3':
    471       1.1      cgd 				case '4': case '5': case '6': case '7':
    472       1.1      cgd 					base = basefix[base];
    473       1.1      cgd 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
    474       1.1      cgd 					goto ok;
    475       1.1      cgd 
    476       1.1      cgd 				/* digits 8 and 9 ok iff decimal or hex */
    477       1.1      cgd 				case '8': case '9':
    478       1.1      cgd 					base = basefix[base];
    479       1.1      cgd 					if (base <= 8)
    480       1.1      cgd 						break;	/* not legal here */
    481       1.1      cgd 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
    482       1.1      cgd 					goto ok;
    483       1.1      cgd 
    484       1.1      cgd 				/* letters ok iff hex */
    485       1.1      cgd 				case 'A': case 'B': case 'C':
    486       1.1      cgd 				case 'D': case 'E': case 'F':
    487       1.1      cgd 				case 'a': case 'b': case 'c':
    488       1.1      cgd 				case 'd': case 'e': case 'f':
    489       1.1      cgd 					/* no need to fix base here */
    490       1.1      cgd 					if (base <= 10)
    491       1.1      cgd 						break;	/* not legal here */
    492       1.1      cgd 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
    493       1.1      cgd 					goto ok;
    494       1.1      cgd 
    495       1.1      cgd 				/* sign ok only as first character */
    496       1.1      cgd 				case '+': case '-':
    497       1.1      cgd 					if (flags & SIGNOK) {
    498       1.1      cgd 						flags &= ~SIGNOK;
    499       1.1      cgd 						goto ok;
    500       1.1      cgd 					}
    501       1.1      cgd 					break;
    502       1.1      cgd 
    503       1.1      cgd 				/* x ok iff flag still set & 2nd char */
    504       1.1      cgd 				case 'x': case 'X':
    505       1.1      cgd 					if (flags & PFXOK && p == buf + 1) {
    506       1.1      cgd 						base = 16;	/* if %i */
    507       1.1      cgd 						flags &= ~PFXOK;
    508       1.1      cgd 						goto ok;
    509       1.1      cgd 					}
    510       1.1      cgd 					break;
    511       1.1      cgd 				}
    512       1.1      cgd 
    513       1.1      cgd 				/*
    514       1.1      cgd 				 * If we got here, c is not a legal character
    515       1.1      cgd 				 * for a number.  Stop accumulating digits.
    516       1.1      cgd 				 */
    517       1.1      cgd 				break;
    518       1.1      cgd 		ok:
    519       1.1      cgd 				/*
    520       1.1      cgd 				 * c is legal: store it and look at the next.
    521       1.1      cgd 				 */
    522       1.1      cgd 				*p++ = c;
    523       1.1      cgd 				if (--fp->_r > 0)
    524       1.1      cgd 					fp->_p++;
    525       1.1      cgd 				else if (__srefill(fp))
    526       1.1      cgd 					break;		/* EOF */
    527       1.1      cgd 			}
    528       1.1      cgd 			/*
    529       1.1      cgd 			 * If we had only a sign, it is no good; push
    530       1.1      cgd 			 * back the sign.  If the number ends in `x',
    531       1.1      cgd 			 * it was [sign] '0' 'x', so push back the x
    532       1.1      cgd 			 * and treat it as [sign] '0'.
    533       1.1      cgd 			 */
    534       1.1      cgd 			if (flags & NDIGITS) {
    535       1.1      cgd 				if (p > buf)
    536       1.1      cgd 					(void) ungetc(*(u_char *)--p, fp);
    537       1.1      cgd 				goto match_failure;
    538       1.1      cgd 			}
    539       1.1      cgd 			c = ((u_char *)p)[-1];
    540       1.1      cgd 			if (c == 'x' || c == 'X') {
    541       1.1      cgd 				--p;
    542       1.1      cgd 				(void) ungetc(c, fp);
    543       1.1      cgd 			}
    544       1.1      cgd 			if ((flags & SUPPRESS) == 0) {
    545      1.10      jtc 				u_quad_t res;
    546       1.1      cgd 
    547       1.1      cgd 				*p = 0;
    548       1.1      cgd 				res = (*ccfn)(buf, (char **)NULL, base);
    549       1.1      cgd 				if (flags & POINTER)
    550       1.1      cgd 					*va_arg(ap, void **) = (void *)res;
    551      1.10      jtc 				else if (flags & QUAD)
    552      1.10      jtc 					*va_arg(ap, quad_t *) = res;
    553      1.10      jtc 				else if (flags & LONG)
    554      1.10      jtc 					*va_arg(ap, long *) = res;
    555       1.1      cgd 				else if (flags & SHORT)
    556       1.1      cgd 					*va_arg(ap, short *) = res;
    557       1.1      cgd 				else
    558       1.1      cgd 					*va_arg(ap, int *) = res;
    559       1.1      cgd 				nassigned++;
    560       1.1      cgd 			}
    561       1.1      cgd 			nread += p - buf;
    562       1.1      cgd 			break;
    563       1.1      cgd 
    564       1.1      cgd #ifdef FLOATING_POINT
    565       1.1      cgd 		case CT_FLOAT:
    566       1.1      cgd 			/* scan a floating point number as if by strtod */
    567       1.1      cgd #ifdef hardway
    568       1.1      cgd 			if (width == 0 || width > sizeof(buf) - 1)
    569       1.1      cgd 				width = sizeof(buf) - 1;
    570       1.1      cgd #else
    571       1.1      cgd 			/* size_t is unsigned, hence this optimisation */
    572       1.1      cgd 			if (--width > sizeof(buf) - 2)
    573       1.1      cgd 				width = sizeof(buf) - 2;
    574       1.1      cgd 			width++;
    575       1.1      cgd #endif
    576       1.1      cgd 			flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
    577       1.1      cgd 			for (p = buf; width; width--) {
    578       1.1      cgd 				c = *fp->_p;
    579       1.1      cgd 				/*
    580       1.1      cgd 				 * This code mimicks the integer conversion
    581       1.1      cgd 				 * code, but is much simpler.
    582       1.1      cgd 				 */
    583       1.1      cgd 				switch (c) {
    584       1.1      cgd 
    585       1.1      cgd 				case '0': case '1': case '2': case '3':
    586       1.1      cgd 				case '4': case '5': case '6': case '7':
    587       1.1      cgd 				case '8': case '9':
    588       1.1      cgd 					flags &= ~(SIGNOK | NDIGITS);
    589       1.1      cgd 					goto fok;
    590       1.1      cgd 
    591       1.1      cgd 				case '+': case '-':
    592       1.1      cgd 					if (flags & SIGNOK) {
    593       1.1      cgd 						flags &= ~SIGNOK;
    594       1.1      cgd 						goto fok;
    595       1.1      cgd 					}
    596       1.1      cgd 					break;
    597       1.1      cgd 				case '.':
    598       1.1      cgd 					if (flags & DPTOK) {
    599       1.1      cgd 						flags &= ~(SIGNOK | DPTOK);
    600       1.1      cgd 						goto fok;
    601       1.1      cgd 					}
    602       1.1      cgd 					break;
    603       1.1      cgd 				case 'e': case 'E':
    604       1.1      cgd 					/* no exponent without some digits */
    605       1.1      cgd 					if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
    606       1.1      cgd 						flags =
    607       1.1      cgd 						    (flags & ~(EXPOK|DPTOK)) |
    608       1.1      cgd 						    SIGNOK | NDIGITS;
    609       1.1      cgd 						goto fok;
    610       1.1      cgd 					}
    611       1.1      cgd 					break;
    612       1.1      cgd 				}
    613       1.1      cgd 				break;
    614       1.1      cgd 		fok:
    615       1.1      cgd 				*p++ = c;
    616       1.1      cgd 				if (--fp->_r > 0)
    617       1.1      cgd 					fp->_p++;
    618       1.1      cgd 				else if (__srefill(fp))
    619       1.1      cgd 					break;	/* EOF */
    620       1.1      cgd 			}
    621       1.1      cgd 			/*
    622       1.1      cgd 			 * If no digits, might be missing exponent digits
    623       1.1      cgd 			 * (just give back the exponent) or might be missing
    624       1.1      cgd 			 * regular digits, but had sign and/or decimal point.
    625       1.1      cgd 			 */
    626       1.1      cgd 			if (flags & NDIGITS) {
    627       1.1      cgd 				if (flags & EXPOK) {
    628       1.1      cgd 					/* no digits at all */
    629       1.1      cgd 					while (p > buf)
    630       1.1      cgd 						ungetc(*(u_char *)--p, fp);
    631       1.1      cgd 					goto match_failure;
    632       1.1      cgd 				}
    633       1.1      cgd 				/* just a bad exponent (e and maybe sign) */
    634       1.1      cgd 				c = *(u_char *)--p;
    635       1.1      cgd 				if (c != 'e' && c != 'E') {
    636       1.1      cgd 					(void) ungetc(c, fp);/* sign */
    637       1.1      cgd 					c = *(u_char *)--p;
    638       1.1      cgd 				}
    639       1.1      cgd 				(void) ungetc(c, fp);
    640       1.1      cgd 			}
    641       1.1      cgd 			if ((flags & SUPPRESS) == 0) {
    642       1.1      cgd 				double res;
    643       1.1      cgd 
    644       1.1      cgd 				*p = 0;
    645       1.3      cgd 				res = strtod(buf, (char **) NULL);
    646      1.11      jtc 				if (flags & LONGDBL)
    647      1.11      jtc 					*va_arg(ap, long double *) = res;
    648      1.11      jtc 				else if (flags & LONG)
    649       1.1      cgd 					*va_arg(ap, double *) = res;
    650       1.1      cgd 				else
    651       1.1      cgd 					*va_arg(ap, float *) = res;
    652       1.1      cgd 				nassigned++;
    653       1.1      cgd 			}
    654       1.1      cgd 			nread += p - buf;
    655       1.1      cgd 			break;
    656       1.1      cgd #endif /* FLOATING_POINT */
    657       1.1      cgd 		}
    658       1.1      cgd 	}
    659       1.1      cgd input_failure:
    660       1.1      cgd 	return (nassigned ? nassigned : -1);
    661       1.1      cgd match_failure:
    662       1.1      cgd 	return (nassigned);
    663       1.1      cgd }
    664       1.1      cgd 
    665       1.1      cgd /*
    666       1.1      cgd  * Fill in the given table from the scanset at the given format
    667       1.1      cgd  * (just after `[').  Return a pointer to the character past the
    668       1.1      cgd  * closing `]'.  The table has a 1 wherever characters should be
    669       1.1      cgd  * considered part of the scanset.
    670       1.1      cgd  */
    671       1.1      cgd static u_char *
    672       1.1      cgd __sccl(tab, fmt)
    673       1.1      cgd 	register char *tab;
    674       1.1      cgd 	register u_char *fmt;
    675       1.1      cgd {
    676       1.1      cgd 	register int c, n, v;
    677       1.1      cgd 
    678       1.1      cgd 	/* first `clear' the whole table */
    679       1.1      cgd 	c = *fmt++;		/* first char hat => negated scanset */
    680       1.1      cgd 	if (c == '^') {
    681       1.1      cgd 		v = 1;		/* default => accept */
    682       1.1      cgd 		c = *fmt++;	/* get new first char */
    683       1.1      cgd 	} else
    684       1.1      cgd 		v = 0;		/* default => reject */
    685       1.1      cgd 	/* should probably use memset here */
    686       1.1      cgd 	for (n = 0; n < 256; n++)
    687       1.1      cgd 		tab[n] = v;
    688       1.1      cgd 	if (c == 0)
    689       1.1      cgd 		return (fmt - 1);/* format ended before closing ] */
    690       1.1      cgd 
    691       1.1      cgd 	/*
    692       1.1      cgd 	 * Now set the entries corresponding to the actual scanset
    693       1.1      cgd 	 * to the opposite of the above.
    694       1.1      cgd 	 *
    695       1.1      cgd 	 * The first character may be ']' (or '-') without being special;
    696       1.1      cgd 	 * the last character may be '-'.
    697       1.1      cgd 	 */
    698       1.1      cgd 	v = 1 - v;
    699       1.1      cgd 	for (;;) {
    700       1.1      cgd 		tab[c] = v;		/* take character c */
    701       1.1      cgd doswitch:
    702       1.1      cgd 		n = *fmt++;		/* and examine the next */
    703       1.1      cgd 		switch (n) {
    704       1.1      cgd 
    705       1.1      cgd 		case 0:			/* format ended too soon */
    706       1.1      cgd 			return (fmt - 1);
    707       1.1      cgd 
    708       1.1      cgd 		case '-':
    709       1.1      cgd 			/*
    710       1.1      cgd 			 * A scanset of the form
    711       1.1      cgd 			 *	[01+-]
    712       1.1      cgd 			 * is defined as `the digit 0, the digit 1,
    713       1.1      cgd 			 * the character +, the character -', but
    714       1.1      cgd 			 * the effect of a scanset such as
    715       1.1      cgd 			 *	[a-zA-Z0-9]
    716       1.1      cgd 			 * is implementation defined.  The V7 Unix
    717       1.1      cgd 			 * scanf treats `a-z' as `the letters a through
    718       1.1      cgd 			 * z', but treats `a-a' as `the letter a, the
    719       1.1      cgd 			 * character -, and the letter a'.
    720       1.1      cgd 			 *
    721       1.1      cgd 			 * For compatibility, the `-' is not considerd
    722       1.1      cgd 			 * to define a range if the character following
    723       1.1      cgd 			 * it is either a close bracket (required by ANSI)
    724       1.1      cgd 			 * or is not numerically greater than the character
    725       1.1      cgd 			 * we just stored in the table (c).
    726       1.1      cgd 			 */
    727       1.1      cgd 			n = *fmt;
    728       1.1      cgd 			if (n == ']' || n < c) {
    729       1.1      cgd 				c = '-';
    730       1.1      cgd 				break;	/* resume the for(;;) */
    731       1.1      cgd 			}
    732       1.1      cgd 			fmt++;
    733       1.1      cgd 			do {		/* fill in the range */
    734       1.1      cgd 				tab[++c] = v;
    735       1.1      cgd 			} while (c < n);
    736       1.1      cgd #if 1	/* XXX another disgusting compatibility hack */
    737       1.1      cgd 			/*
    738       1.1      cgd 			 * Alas, the V7 Unix scanf also treats formats
    739       1.1      cgd 			 * such as [a-c-e] as `the letters a through e'.
    740       1.1      cgd 			 * This too is permitted by the standard....
    741       1.1      cgd 			 */
    742       1.1      cgd 			goto doswitch;
    743       1.1      cgd #else
    744       1.1      cgd 			c = *fmt++;
    745       1.1      cgd 			if (c == 0)
    746       1.1      cgd 				return (fmt - 1);
    747       1.1      cgd 			if (c == ']')
    748       1.1      cgd 				return (fmt);
    749       1.1      cgd #endif
    750       1.1      cgd 			break;
    751       1.1      cgd 
    752       1.1      cgd 		case ']':		/* end of scanset */
    753       1.1      cgd 			return (fmt);
    754       1.1      cgd 
    755       1.1      cgd 		default:		/* just another character */
    756       1.1      cgd 			c = n;
    757       1.1      cgd 			break;
    758       1.1      cgd 		}
    759       1.1      cgd 	}
    760       1.1      cgd 	/* NOTREACHED */
    761       1.1      cgd }
    762