1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1996, by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * System V.2 Emulation Stdio Library -- vfscanf
29*7c478bd9Sstevel@tonic-gate  *
30*7c478bd9Sstevel@tonic-gate  * Copyright 1985, 1992 by Mortice Kern Systems Inc.  All rights reserved.
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #ifdef M_RCSID
35*7c478bd9Sstevel@tonic-gate #ifndef lint
36*7c478bd9Sstevel@tonic-gate static char rcsID[] = "$Id: vfscanf.c 1.27 1995/09/20 19:07:52 ant Exp $";
37*7c478bd9Sstevel@tonic-gate #endif
38*7c478bd9Sstevel@tonic-gate #endif
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #include <mks.h>
41*7c478bd9Sstevel@tonic-gate #include <ctype.h>
42*7c478bd9Sstevel@tonic-gate #include <limits.h>
43*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
44*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
45*7c478bd9Sstevel@tonic-gate #include <string.h>
46*7c478bd9Sstevel@tonic-gate #ifdef __FLOAT__
47*7c478bd9Sstevel@tonic-gate #include <math.h>
48*7c478bd9Sstevel@tonic-gate #endif
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #define	CONVTYPE	1
51*7c478bd9Sstevel@tonic-gate #define STAR		2
52*7c478bd9Sstevel@tonic-gate #define PERCENT		3
53*7c478bd9Sstevel@tonic-gate #define NUMBER		4
54*7c478bd9Sstevel@tonic-gate #define MODCONVL	5
55*7c478bd9Sstevel@tonic-gate #define NSCAN		6
56*7c478bd9Sstevel@tonic-gate #define	BRACKET		7
57*7c478bd9Sstevel@tonic-gate #define MODCONVH	8
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate #define BASE16	16
60*7c478bd9Sstevel@tonic-gate #define BASE10	10
61*7c478bd9Sstevel@tonic-gate #define BASE8	8
62*7c478bd9Sstevel@tonic-gate #define NOBASE	0
63*7c478bd9Sstevel@tonic-gate #define SIGNED	1
64*7c478bd9Sstevel@tonic-gate #define UNSIGNED 0
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate #define	CBUFSIZ	100	/* size of character buffer for input conversion */
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate struct lexlist {
69*7c478bd9Sstevel@tonic-gate 	char	name;
70*7c478bd9Sstevel@tonic-gate 	char	type;
71*7c478bd9Sstevel@tonic-gate };
72*7c478bd9Sstevel@tonic-gate static struct lexlist *lexp;
73*7c478bd9Sstevel@tonic-gate static struct lexlist lexlist[] ={
74*7c478bd9Sstevel@tonic-gate 	'*',	STAR,
75*7c478bd9Sstevel@tonic-gate 	'%',	PERCENT,
76*7c478bd9Sstevel@tonic-gate 	'l',	MODCONVL,
77*7c478bd9Sstevel@tonic-gate 	'h',	MODCONVH,
78*7c478bd9Sstevel@tonic-gate 	'n',	NSCAN,
79*7c478bd9Sstevel@tonic-gate 	'[',	BRACKET,
80*7c478bd9Sstevel@tonic-gate 	'd',	CONVTYPE,
81*7c478bd9Sstevel@tonic-gate 	'S',	CONVTYPE,	/* dummy entry (for multibyte characters) */
82*7c478bd9Sstevel@tonic-gate 	's',	CONVTYPE,
83*7c478bd9Sstevel@tonic-gate 	'u',	CONVTYPE,
84*7c478bd9Sstevel@tonic-gate 	'c',	CONVTYPE,
85*7c478bd9Sstevel@tonic-gate 	'x',	CONVTYPE,
86*7c478bd9Sstevel@tonic-gate 	'o',	CONVTYPE,
87*7c478bd9Sstevel@tonic-gate 	'0',	NUMBER,
88*7c478bd9Sstevel@tonic-gate 	'1',	NUMBER,
89*7c478bd9Sstevel@tonic-gate 	'2',	NUMBER,
90*7c478bd9Sstevel@tonic-gate 	'3',	NUMBER,
91*7c478bd9Sstevel@tonic-gate 	'4',	NUMBER,
92*7c478bd9Sstevel@tonic-gate 	'5',	NUMBER,
93*7c478bd9Sstevel@tonic-gate 	'6',	NUMBER,
94*7c478bd9Sstevel@tonic-gate 	'7',	NUMBER,
95*7c478bd9Sstevel@tonic-gate 	'8',	NUMBER,
96*7c478bd9Sstevel@tonic-gate 	'9',	NUMBER,
97*7c478bd9Sstevel@tonic-gate 	'i',	CONVTYPE,
98*7c478bd9Sstevel@tonic-gate 	'f',	CONVTYPE,
99*7c478bd9Sstevel@tonic-gate 	'e',	CONVTYPE,
100*7c478bd9Sstevel@tonic-gate 	'g',	CONVTYPE,
101*7c478bd9Sstevel@tonic-gate 	0,	0
102*7c478bd9Sstevel@tonic-gate };
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate static int	scan(int, const char *, const char *);
105*7c478bd9Sstevel@tonic-gate static	int	gettoken(void);
106*7c478bd9Sstevel@tonic-gate static	void	whitespace(void);
107*7c478bd9Sstevel@tonic-gate static	int	match(const char *, char *);
108*7c478bd9Sstevel@tonic-gate static	long	unsigned	getnum(int, int, int);
109*7c478bd9Sstevel@tonic-gate static	int	getin(void);
110*7c478bd9Sstevel@tonic-gate static	void	unget(int);
111*7c478bd9Sstevel@tonic-gate #ifdef	__FLOAT__
112*7c478bd9Sstevel@tonic-gate static	double	lstrtod(void);
113*7c478bd9Sstevel@tonic-gate #endif
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate static	int	ungot;		/* getin/unget char */
116*7c478bd9Sstevel@tonic-gate static	FILE	*fpin;		/* input file pointer */
117*7c478bd9Sstevel@tonic-gate static	int	pflag;		/*indicator of conversion description present */
118*7c478bd9Sstevel@tonic-gate static	int	width;		/* field width value */
119*7c478bd9Sstevel@tonic-gate static	const	char	*fmtptr;	/* format string pointer */
120*7c478bd9Sstevel@tonic-gate static	int	charcnt;	/* number of characters scanned (for %n) */
121*7c478bd9Sstevel@tonic-gate static	int	from;		/* token type we've come from */
122*7c478bd9Sstevel@tonic-gate static	int	gfail;		/* getnum() fail flag, non-zero for fail */
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate /*
125*7c478bd9Sstevel@tonic-gate  * Convert formatted input from given input.
126*7c478bd9Sstevel@tonic-gate  * This is the workhorse for scanf, sscanf, and fscanf.
127*7c478bd9Sstevel@tonic-gate  * Returns the number of matched and assigned input items.
128*7c478bd9Sstevel@tonic-gate  */
129*7c478bd9Sstevel@tonic-gate int
mks_vfscanf(FILE * pfin,const char * fmt,va_list ap)130*7c478bd9Sstevel@tonic-gate mks_vfscanf(FILE *pfin, const char *fmt, va_list ap)
131*7c478bd9Sstevel@tonic-gate {
132*7c478bd9Sstevel@tonic-gate 	int	nitems;
133*7c478bd9Sstevel@tonic-gate 	int	ltoken;
134*7c478bd9Sstevel@tonic-gate 	int	c;
135*7c478bd9Sstevel@tonic-gate 	int	modconv;	/* flag indicating conversion modifier */
136*7c478bd9Sstevel@tonic-gate 	int	suppression;	/* flag to suppress conversion */
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	long unsigned number;	/* return value from getnumber */
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	ungot = EOF;
141*7c478bd9Sstevel@tonic-gate 	fpin = pfin;
142*7c478bd9Sstevel@tonic-gate 	fmtptr = fmt;
143*7c478bd9Sstevel@tonic-gate 	from = 'X';
144*7c478bd9Sstevel@tonic-gate 	nitems = 0;
145*7c478bd9Sstevel@tonic-gate 	charcnt = 0;
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	for (;;) {
148*7c478bd9Sstevel@tonic-gate 		if (from == 'X') {
149*7c478bd9Sstevel@tonic-gate 			pflag = 0;
150*7c478bd9Sstevel@tonic-gate 			modconv = 0;
151*7c478bd9Sstevel@tonic-gate 			suppression = 0;
152*7c478bd9Sstevel@tonic-gate 			width = 0;
153*7c478bd9Sstevel@tonic-gate 		}
154*7c478bd9Sstevel@tonic-gate 		ltoken = gettoken();
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 		switch (ltoken) {
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 		case 0:
159*7c478bd9Sstevel@tonic-gate 			goto retitems;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 		case MODCONVL:
162*7c478bd9Sstevel@tonic-gate 		case MODCONVH:
163*7c478bd9Sstevel@tonic-gate 			switch (from) {
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 			case 'A':
166*7c478bd9Sstevel@tonic-gate 			case 'D':
167*7c478bd9Sstevel@tonic-gate 			case 'P':
168*7c478bd9Sstevel@tonic-gate 				from = 'E';
169*7c478bd9Sstevel@tonic-gate 				modconv = ltoken;
170*7c478bd9Sstevel@tonic-gate 				break;
171*7c478bd9Sstevel@tonic-gate 			default:
172*7c478bd9Sstevel@tonic-gate 				from = 'X';
173*7c478bd9Sstevel@tonic-gate 				break;
174*7c478bd9Sstevel@tonic-gate 			}
175*7c478bd9Sstevel@tonic-gate 			break;
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 		case CONVTYPE:
178*7c478bd9Sstevel@tonic-gate 			switch (from) {
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 			int	intassign;
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 			case 'E':
183*7c478bd9Sstevel@tonic-gate 			case 'P':
184*7c478bd9Sstevel@tonic-gate 			case 'D':
185*7c478bd9Sstevel@tonic-gate 			case 'A':
186*7c478bd9Sstevel@tonic-gate 				from = 'X';
187*7c478bd9Sstevel@tonic-gate 				intassign = 1;
188*7c478bd9Sstevel@tonic-gate 				pflag = 0;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 				switch (lexp->name) {
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 				case 'd':
193*7c478bd9Sstevel@tonic-gate 					number = getnum(BASE10, width, SIGNED);
194*7c478bd9Sstevel@tonic-gate 					if (gfail)
195*7c478bd9Sstevel@tonic-gate 						goto retitems;
196*7c478bd9Sstevel@tonic-gate 					break;
197*7c478bd9Sstevel@tonic-gate 				case 'u':
198*7c478bd9Sstevel@tonic-gate 					number = getnum(BASE10, width, UNSIGNED);
199*7c478bd9Sstevel@tonic-gate 					if (gfail)
200*7c478bd9Sstevel@tonic-gate 						goto retitems;
201*7c478bd9Sstevel@tonic-gate 					break;
202*7c478bd9Sstevel@tonic-gate 				case 'x':
203*7c478bd9Sstevel@tonic-gate 					number = getnum(BASE16, width, SIGNED);
204*7c478bd9Sstevel@tonic-gate 					if (gfail)
205*7c478bd9Sstevel@tonic-gate 						goto retitems;
206*7c478bd9Sstevel@tonic-gate 					break;
207*7c478bd9Sstevel@tonic-gate 				case 'o':
208*7c478bd9Sstevel@tonic-gate 					number = getnum(BASE8, width, SIGNED);
209*7c478bd9Sstevel@tonic-gate 					if (gfail)
210*7c478bd9Sstevel@tonic-gate 						goto retitems;
211*7c478bd9Sstevel@tonic-gate 					break;
212*7c478bd9Sstevel@tonic-gate 				case 'i':
213*7c478bd9Sstevel@tonic-gate 					number = getnum(NOBASE, width, SIGNED);
214*7c478bd9Sstevel@tonic-gate 					if (gfail)
215*7c478bd9Sstevel@tonic-gate 						goto retitems;
216*7c478bd9Sstevel@tonic-gate 					break;
217*7c478bd9Sstevel@tonic-gate 				case 'c':
218*7c478bd9Sstevel@tonic-gate 				/* 'S' dummy entry (for multibyte characters) */
219*7c478bd9Sstevel@tonic-gate 				case 'S':
220*7c478bd9Sstevel@tonic-gate 				case 's': {
221*7c478bd9Sstevel@tonic-gate 					int gotitem = 0;
222*7c478bd9Sstevel@tonic-gate 					char	*str;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 					if (!suppression)
225*7c478bd9Sstevel@tonic-gate 						str = va_arg(ap, char *);
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 					/* Input whitespace is not skipped
228*7c478bd9Sstevel@tonic-gate 					 * for %c, which implies that %c
229*7c478bd9Sstevel@tonic-gate 					 * can return whitespace.
230*7c478bd9Sstevel@tonic-gate 					 */
231*7c478bd9Sstevel@tonic-gate 					if (lexp->name != 'c')
232*7c478bd9Sstevel@tonic-gate 						whitespace();
233*7c478bd9Sstevel@tonic-gate 					for (;;) {
234*7c478bd9Sstevel@tonic-gate 						c = getin();
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 						/* Only %s and %S stop on
237*7c478bd9Sstevel@tonic-gate 						 * whitespace.
238*7c478bd9Sstevel@tonic-gate 						 */
239*7c478bd9Sstevel@tonic-gate 						if (lexp->name != 'c' && isspace(c)) {
240*7c478bd9Sstevel@tonic-gate 							unget(c);
241*7c478bd9Sstevel@tonic-gate 							break;
242*7c478bd9Sstevel@tonic-gate 						}
243*7c478bd9Sstevel@tonic-gate 						if (c == EOF) {
244*7c478bd9Sstevel@tonic-gate 							if(!gotitem)
245*7c478bd9Sstevel@tonic-gate 								goto retitems;
246*7c478bd9Sstevel@tonic-gate 							break;
247*7c478bd9Sstevel@tonic-gate 						}
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 						gotitem = 1;
250*7c478bd9Sstevel@tonic-gate 						if (!suppression)
251*7c478bd9Sstevel@tonic-gate 							*str++ = c;
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 						if (width) {
254*7c478bd9Sstevel@tonic-gate 							if (--width == 0)
255*7c478bd9Sstevel@tonic-gate 								break;
256*7c478bd9Sstevel@tonic-gate 						}
257*7c478bd9Sstevel@tonic-gate 					}
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 					/*
260*7c478bd9Sstevel@tonic-gate 					 * ANSI C states that %c does not
261*7c478bd9Sstevel@tonic-gate 					 * terminate with a null character.
262*7c478bd9Sstevel@tonic-gate 					 */
263*7c478bd9Sstevel@tonic-gate 					if (!suppression && lexp->name != 'c')
264*7c478bd9Sstevel@tonic-gate 						*str = '\0';
265*7c478bd9Sstevel@tonic-gate 					intassign = 0;
266*7c478bd9Sstevel@tonic-gate 					break;
267*7c478bd9Sstevel@tonic-gate 				}
268*7c478bd9Sstevel@tonic-gate #ifdef	__FLOAT__
269*7c478bd9Sstevel@tonic-gate 				case 'f':
270*7c478bd9Sstevel@tonic-gate 				case 'g':
271*7c478bd9Sstevel@tonic-gate 				case 'e': {
272*7c478bd9Sstevel@tonic-gate 					double	fresult;
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 					fresult = lstrtod();
275*7c478bd9Sstevel@tonic-gate 					if(gfail)
276*7c478bd9Sstevel@tonic-gate 						goto retitems;
277*7c478bd9Sstevel@tonic-gate 					if(suppression)
278*7c478bd9Sstevel@tonic-gate 						break;
279*7c478bd9Sstevel@tonic-gate 					if (modconv == MODCONVL)
280*7c478bd9Sstevel@tonic-gate 						*(double *)va_arg(ap, double *) = fresult;
281*7c478bd9Sstevel@tonic-gate 					else
282*7c478bd9Sstevel@tonic-gate 						*(float *)va_arg(ap, float *) = (float)fresult;
283*7c478bd9Sstevel@tonic-gate 					/*FALLTHROUGH*/
284*7c478bd9Sstevel@tonic-gate 				}
285*7c478bd9Sstevel@tonic-gate #else	/* !__FLOAT__ */
286*7c478bd9Sstevel@tonic-gate 				case 'f':
287*7c478bd9Sstevel@tonic-gate 				case 'g':
288*7c478bd9Sstevel@tonic-gate 				case 'e':
289*7c478bd9Sstevel@tonic-gate #endif	/* __FLOAT__ */
290*7c478bd9Sstevel@tonic-gate 				default:
291*7c478bd9Sstevel@tonic-gate 					intassign = 0;
292*7c478bd9Sstevel@tonic-gate 					break;
293*7c478bd9Sstevel@tonic-gate 				}
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 				if (suppression)
296*7c478bd9Sstevel@tonic-gate 					break;
297*7c478bd9Sstevel@tonic-gate 				else
298*7c478bd9Sstevel@tonic-gate 					nitems++;
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 				if (intassign == 0)
301*7c478bd9Sstevel@tonic-gate 					break;
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 				switch (modconv) {
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 				case MODCONVH:
306*7c478bd9Sstevel@tonic-gate 					*(short *)va_arg(ap, short *) = (short)number;
307*7c478bd9Sstevel@tonic-gate 					break;
308*7c478bd9Sstevel@tonic-gate 				case MODCONVL:
309*7c478bd9Sstevel@tonic-gate 					*(long *)va_arg(ap, long *) = (long)number;
310*7c478bd9Sstevel@tonic-gate 					break;
311*7c478bd9Sstevel@tonic-gate 				default:
312*7c478bd9Sstevel@tonic-gate 					*(int *)va_arg(ap, int *) = (int)number;
313*7c478bd9Sstevel@tonic-gate 					break;
314*7c478bd9Sstevel@tonic-gate 				}
315*7c478bd9Sstevel@tonic-gate 				break;
316*7c478bd9Sstevel@tonic-gate 			default:
317*7c478bd9Sstevel@tonic-gate 				from = 'X';
318*7c478bd9Sstevel@tonic-gate 				break;
319*7c478bd9Sstevel@tonic-gate 			}
320*7c478bd9Sstevel@tonic-gate 			break;
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate 		case STAR:
323*7c478bd9Sstevel@tonic-gate 			if (from == 'P') {
324*7c478bd9Sstevel@tonic-gate 				from = 'A';
325*7c478bd9Sstevel@tonic-gate 				suppression = 1;
326*7c478bd9Sstevel@tonic-gate 			} else {
327*7c478bd9Sstevel@tonic-gate 				from = 'X';
328*7c478bd9Sstevel@tonic-gate 			}
329*7c478bd9Sstevel@tonic-gate 			break;
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 		case PERCENT:
332*7c478bd9Sstevel@tonic-gate 			if (from == 'P') {
333*7c478bd9Sstevel@tonic-gate 				from = 'X';
334*7c478bd9Sstevel@tonic-gate 				pflag = 0;
335*7c478bd9Sstevel@tonic-gate 				c = getin();
336*7c478bd9Sstevel@tonic-gate 				if (c != '%')
337*7c478bd9Sstevel@tonic-gate 					goto retitems;
338*7c478bd9Sstevel@tonic-gate 			} else {
339*7c478bd9Sstevel@tonic-gate 				from = 'X';
340*7c478bd9Sstevel@tonic-gate 			}
341*7c478bd9Sstevel@tonic-gate 			break;
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 		case NUMBER:
344*7c478bd9Sstevel@tonic-gate 			if (from == 'P' || from == 'A') {
345*7c478bd9Sstevel@tonic-gate 				from = 'D';
346*7c478bd9Sstevel@tonic-gate 			} else {
347*7c478bd9Sstevel@tonic-gate 				from = 'X';
348*7c478bd9Sstevel@tonic-gate 			}
349*7c478bd9Sstevel@tonic-gate 			break;
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 		case NSCAN:
352*7c478bd9Sstevel@tonic-gate 			if (from == 'P') {
353*7c478bd9Sstevel@tonic-gate 				pflag = 0;
354*7c478bd9Sstevel@tonic-gate 				if (!suppression) {
355*7c478bd9Sstevel@tonic-gate 					*(int *)va_arg(ap, int *) = charcnt;
356*7c478bd9Sstevel@tonic-gate 				}
357*7c478bd9Sstevel@tonic-gate 			}
358*7c478bd9Sstevel@tonic-gate 			from = 'X';
359*7c478bd9Sstevel@tonic-gate 			break;
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 		case BRACKET:
362*7c478bd9Sstevel@tonic-gate 			switch (from) {
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate 			case 'A':
365*7c478bd9Sstevel@tonic-gate 			case 'D':
366*7c478bd9Sstevel@tonic-gate 			case 'P': {
367*7c478bd9Sstevel@tonic-gate 				char *ptr;
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 				pflag = 0;
370*7c478bd9Sstevel@tonic-gate 				if (width == 0)
371*7c478bd9Sstevel@tonic-gate 					width = INT_MAX;
372*7c478bd9Sstevel@tonic-gate 				ptr = suppression ? NULL : va_arg(ap, char *);
373*7c478bd9Sstevel@tonic-gate 				if (match(fmtptr, ptr) && !feof(fpin)
374*7c478bd9Sstevel@tonic-gate 				&& !suppression)
375*7c478bd9Sstevel@tonic-gate 					nitems++;
376*7c478bd9Sstevel@tonic-gate 				while (*fmtptr++ != ']')
377*7c478bd9Sstevel@tonic-gate 					;
378*7c478bd9Sstevel@tonic-gate 				break;
379*7c478bd9Sstevel@tonic-gate 			}
380*7c478bd9Sstevel@tonic-gate 			default:
381*7c478bd9Sstevel@tonic-gate 				break;
382*7c478bd9Sstevel@tonic-gate 			}
383*7c478bd9Sstevel@tonic-gate 			from = 'X';
384*7c478bd9Sstevel@tonic-gate 			break;
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 		default:
387*7c478bd9Sstevel@tonic-gate 			c = *(fmtptr-1);
388*7c478bd9Sstevel@tonic-gate 			if (c == ' ' || c == '\t' || c == '\n' || c == '\f')
389*7c478bd9Sstevel@tonic-gate 				whitespace();
390*7c478bd9Sstevel@tonic-gate 			else {
391*7c478bd9Sstevel@tonic-gate 				c = getin();
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate 				if (c != *(fmtptr-1))
394*7c478bd9Sstevel@tonic-gate 					goto retitems;
395*7c478bd9Sstevel@tonic-gate 			}
396*7c478bd9Sstevel@tonic-gate 			from = 'X';
397*7c478bd9Sstevel@tonic-gate 			break;
398*7c478bd9Sstevel@tonic-gate 		}
399*7c478bd9Sstevel@tonic-gate 	}
400*7c478bd9Sstevel@tonic-gate retitems:
401*7c478bd9Sstevel@tonic-gate 	if (ungot != EOF) {
402*7c478bd9Sstevel@tonic-gate 		ungetc(ungot, fpin);
403*7c478bd9Sstevel@tonic-gate 		ungot = EOF;
404*7c478bd9Sstevel@tonic-gate 	}
405*7c478bd9Sstevel@tonic-gate 	return nitems==0 ? EOF : nitems;
406*7c478bd9Sstevel@tonic-gate }
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate static int
gettoken()409*7c478bd9Sstevel@tonic-gate gettoken()
410*7c478bd9Sstevel@tonic-gate {
411*7c478bd9Sstevel@tonic-gate 	char	c;
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 	if (*fmtptr == 0)
414*7c478bd9Sstevel@tonic-gate 		return 0;	/* return 0 for end of string */
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 	c = *fmtptr++;
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 	if (pflag) {
419*7c478bd9Sstevel@tonic-gate 		for(lexp=lexlist; lexp->name != 0; lexp++) {
420*7c478bd9Sstevel@tonic-gate 			if (c == lexp->name) {
421*7c478bd9Sstevel@tonic-gate 				if (lexp->type == NUMBER) {
422*7c478bd9Sstevel@tonic-gate 					width = (int) strtol(fmtptr-1, (char **)0, BASE10);
423*7c478bd9Sstevel@tonic-gate 					while (*fmtptr >= '0' && *fmtptr <= '9')
424*7c478bd9Sstevel@tonic-gate 						fmtptr++;
425*7c478bd9Sstevel@tonic-gate 				} else if (c == 'c') {
426*7c478bd9Sstevel@tonic-gate 					/* No width specified for %c, default
427*7c478bd9Sstevel@tonic-gate 					 * is one.
428*7c478bd9Sstevel@tonic-gate 					 */
429*7c478bd9Sstevel@tonic-gate 					width = 1;
430*7c478bd9Sstevel@tonic-gate 				}
431*7c478bd9Sstevel@tonic-gate 				return lexp->type;
432*7c478bd9Sstevel@tonic-gate 			}
433*7c478bd9Sstevel@tonic-gate 		}
434*7c478bd9Sstevel@tonic-gate 		return -1;
435*7c478bd9Sstevel@tonic-gate 	}
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate 	if (c == '%') {
438*7c478bd9Sstevel@tonic-gate 		pflag = 1;
439*7c478bd9Sstevel@tonic-gate 		from = 'P';
440*7c478bd9Sstevel@tonic-gate 		return gettoken();
441*7c478bd9Sstevel@tonic-gate 	}
442*7c478bd9Sstevel@tonic-gate 	return -1;
443*7c478bd9Sstevel@tonic-gate }
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate static void
whitespace()446*7c478bd9Sstevel@tonic-gate whitespace()
447*7c478bd9Sstevel@tonic-gate {
448*7c478bd9Sstevel@tonic-gate 	register int	c;
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 	do {
451*7c478bd9Sstevel@tonic-gate 		c = getin();
452*7c478bd9Sstevel@tonic-gate 	} while (isspace(c));
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate 	unget(c);
455*7c478bd9Sstevel@tonic-gate }
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate static int
scan(int ch,const char * str,const char * estr)458*7c478bd9Sstevel@tonic-gate scan(int ch, const char *str, const char *estr)
459*7c478bd9Sstevel@tonic-gate {
460*7c478bd9Sstevel@tonic-gate 	for (; str < estr; ++str)
461*7c478bd9Sstevel@tonic-gate 		if (*str == ch)
462*7c478bd9Sstevel@tonic-gate 			return 1;
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 	return 0;
465*7c478bd9Sstevel@tonic-gate }
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate static int
match(const char * str,char * outstr)468*7c478bd9Sstevel@tonic-gate match(const char *str, char *outstr)
469*7c478bd9Sstevel@tonic-gate {
470*7c478bd9Sstevel@tonic-gate 	int	complement;
471*7c478bd9Sstevel@tonic-gate 	int	i;
472*7c478bd9Sstevel@tonic-gate 	char	start, end;
473*7c478bd9Sstevel@tonic-gate 	int	c;
474*7c478bd9Sstevel@tonic-gate 	const	char	*bscan, *escan;
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate 	if (*str == '^') {
477*7c478bd9Sstevel@tonic-gate 		complement = 1;
478*7c478bd9Sstevel@tonic-gate 		str++;
479*7c478bd9Sstevel@tonic-gate 	} else
480*7c478bd9Sstevel@tonic-gate 		complement = 0;
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate 	start = *str++;
483*7c478bd9Sstevel@tonic-gate 	end = 0;
484*7c478bd9Sstevel@tonic-gate 	if (*str == '-') {
485*7c478bd9Sstevel@tonic-gate 		if (str[2] == ']')
486*7c478bd9Sstevel@tonic-gate 			end = str[1];
487*7c478bd9Sstevel@tonic-gate 	}
488*7c478bd9Sstevel@tonic-gate 	if (start > end) {
489*7c478bd9Sstevel@tonic-gate 		bscan = str - 1;
490*7c478bd9Sstevel@tonic-gate 		while (*str++ != ']')
491*7c478bd9Sstevel@tonic-gate 			;
492*7c478bd9Sstevel@tonic-gate 		escan = str - 1;
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate 		for (i=0; i<width; i++) {
495*7c478bd9Sstevel@tonic-gate 			if ((c = getin()) == EOF)
496*7c478bd9Sstevel@tonic-gate 				return 0;
497*7c478bd9Sstevel@tonic-gate 			if (!scan(c, bscan, escan) ^ complement)
498*7c478bd9Sstevel@tonic-gate 				break;
499*7c478bd9Sstevel@tonic-gate 			if (outstr != NULL)
500*7c478bd9Sstevel@tonic-gate 				*outstr++ = c;
501*7c478bd9Sstevel@tonic-gate 		}
502*7c478bd9Sstevel@tonic-gate 	} else {
503*7c478bd9Sstevel@tonic-gate 		for (i=0; i<width; i++) {
504*7c478bd9Sstevel@tonic-gate 			c = getin();
505*7c478bd9Sstevel@tonic-gate 			if (complement) {
506*7c478bd9Sstevel@tonic-gate 				if (c >= start && c <= end)
507*7c478bd9Sstevel@tonic-gate 					break;
508*7c478bd9Sstevel@tonic-gate 				else if (outstr != NULL)
509*7c478bd9Sstevel@tonic-gate 					*outstr++ = c;
510*7c478bd9Sstevel@tonic-gate 			} else {
511*7c478bd9Sstevel@tonic-gate 				if (c < start || c > end)
512*7c478bd9Sstevel@tonic-gate 					break;
513*7c478bd9Sstevel@tonic-gate 				else if (outstr != NULL)
514*7c478bd9Sstevel@tonic-gate 					*outstr++ = c;
515*7c478bd9Sstevel@tonic-gate 			}
516*7c478bd9Sstevel@tonic-gate 		}
517*7c478bd9Sstevel@tonic-gate 	}
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate 	if (i < width)
520*7c478bd9Sstevel@tonic-gate 		unget(c);
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate 	if (outstr != NULL)
523*7c478bd9Sstevel@tonic-gate 		*outstr = '\0';
524*7c478bd9Sstevel@tonic-gate 	return (i > 1);
525*7c478bd9Sstevel@tonic-gate }
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate /*
528*7c478bd9Sstevel@tonic-gate  * Get a number from the input stream.
529*7c478bd9Sstevel@tonic-gate  * The base, if zero, will be determined by the nature of the number.
530*7c478bd9Sstevel@tonic-gate  * A leading 0x means hexadecimal, a leading 0 for octal, otherwise decimal.
531*7c478bd9Sstevel@tonic-gate  *
532*7c478bd9Sstevel@tonic-gate  * if the width is 0 then the max input string length of number is used.
533*7c478bd9Sstevel@tonic-gate  *
534*7c478bd9Sstevel@tonic-gate  * The sign tell us that a signed number is expected (rather than the
535*7c478bd9Sstevel@tonic-gate  *	'u' conversion type which is unsigned).
536*7c478bd9Sstevel@tonic-gate  */
537*7c478bd9Sstevel@tonic-gate static long unsigned
getnum(int base,int width,int sign)538*7c478bd9Sstevel@tonic-gate getnum(int base, int width, int sign)
539*7c478bd9Sstevel@tonic-gate {
540*7c478bd9Sstevel@tonic-gate 	char	*s;
541*7c478bd9Sstevel@tonic-gate 	char	cbuf[CBUFSIZ];			/* char buffer for number */
542*7c478bd9Sstevel@tonic-gate 	int	w;
543*7c478bd9Sstevel@tonic-gate 	register int	c;
544*7c478bd9Sstevel@tonic-gate 	int	neg;
545*7c478bd9Sstevel@tonic-gate 	long	ret;
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 	gfail = 0;
548*7c478bd9Sstevel@tonic-gate 	whitespace();
549*7c478bd9Sstevel@tonic-gate 
550*7c478bd9Sstevel@tonic-gate 	if (width == 0)
551*7c478bd9Sstevel@tonic-gate 		width = sizeof cbuf;
552*7c478bd9Sstevel@tonic-gate 
553*7c478bd9Sstevel@tonic-gate 	neg = 0;
554*7c478bd9Sstevel@tonic-gate 	if (sign) {
555*7c478bd9Sstevel@tonic-gate 		c = getin();
556*7c478bd9Sstevel@tonic-gate 		if (c == '+' || c == '-')
557*7c478bd9Sstevel@tonic-gate 			neg = c=='-' ? 1 : 0;
558*7c478bd9Sstevel@tonic-gate 		else
559*7c478bd9Sstevel@tonic-gate 			unget(c);
560*7c478bd9Sstevel@tonic-gate 	}
561*7c478bd9Sstevel@tonic-gate 
562*7c478bd9Sstevel@tonic-gate 	if (base == 0) {
563*7c478bd9Sstevel@tonic-gate 		base = 10;
564*7c478bd9Sstevel@tonic-gate 		c = getin();
565*7c478bd9Sstevel@tonic-gate 		if (c == '0') {
566*7c478bd9Sstevel@tonic-gate 			base = 8;
567*7c478bd9Sstevel@tonic-gate 			c = getin();
568*7c478bd9Sstevel@tonic-gate 			if (c == 'X' || c == 'x')
569*7c478bd9Sstevel@tonic-gate 				base = 16;
570*7c478bd9Sstevel@tonic-gate 			else
571*7c478bd9Sstevel@tonic-gate 				unget(c);
572*7c478bd9Sstevel@tonic-gate 		} else
573*7c478bd9Sstevel@tonic-gate 			unget(c);
574*7c478bd9Sstevel@tonic-gate 	}
575*7c478bd9Sstevel@tonic-gate 	if (base == 10) {
576*7c478bd9Sstevel@tonic-gate 		w = 0;
577*7c478bd9Sstevel@tonic-gate 		s = cbuf;
578*7c478bd9Sstevel@tonic-gate 		while (w < width && w < sizeof cbuf) {
579*7c478bd9Sstevel@tonic-gate 			c = getin();
580*7c478bd9Sstevel@tonic-gate 			switch (c) {
581*7c478bd9Sstevel@tonic-gate 
582*7c478bd9Sstevel@tonic-gate 			case '0':
583*7c478bd9Sstevel@tonic-gate 			case '1':
584*7c478bd9Sstevel@tonic-gate 			case '2':
585*7c478bd9Sstevel@tonic-gate 			case '3':
586*7c478bd9Sstevel@tonic-gate 			case '4':
587*7c478bd9Sstevel@tonic-gate 			case '5':
588*7c478bd9Sstevel@tonic-gate 			case '6':
589*7c478bd9Sstevel@tonic-gate 			case '7':
590*7c478bd9Sstevel@tonic-gate 			case '8':
591*7c478bd9Sstevel@tonic-gate 			case '9':
592*7c478bd9Sstevel@tonic-gate 				*s++ = c;
593*7c478bd9Sstevel@tonic-gate 				w++;
594*7c478bd9Sstevel@tonic-gate 				continue;
595*7c478bd9Sstevel@tonic-gate 			default:
596*7c478bd9Sstevel@tonic-gate 				unget(c);
597*7c478bd9Sstevel@tonic-gate 				w = width;	/* force end of loop */
598*7c478bd9Sstevel@tonic-gate 				break;
599*7c478bd9Sstevel@tonic-gate 			}
600*7c478bd9Sstevel@tonic-gate 		}
601*7c478bd9Sstevel@tonic-gate 		*s = '\0';
602*7c478bd9Sstevel@tonic-gate 		ret = strtol(cbuf, (char **)0, 10);
603*7c478bd9Sstevel@tonic-gate 		goto retn;
604*7c478bd9Sstevel@tonic-gate 	}
605*7c478bd9Sstevel@tonic-gate 	if (base == 8) {
606*7c478bd9Sstevel@tonic-gate 		w = 0;
607*7c478bd9Sstevel@tonic-gate 		s = cbuf;
608*7c478bd9Sstevel@tonic-gate 		while (w < width && w < sizeof cbuf) {
609*7c478bd9Sstevel@tonic-gate 			c = getin();
610*7c478bd9Sstevel@tonic-gate 			switch (c) {
611*7c478bd9Sstevel@tonic-gate 
612*7c478bd9Sstevel@tonic-gate 			case '0':
613*7c478bd9Sstevel@tonic-gate 			case '1':
614*7c478bd9Sstevel@tonic-gate 			case '2':
615*7c478bd9Sstevel@tonic-gate 			case '3':
616*7c478bd9Sstevel@tonic-gate 			case '4':
617*7c478bd9Sstevel@tonic-gate 			case '5':
618*7c478bd9Sstevel@tonic-gate 			case '6':
619*7c478bd9Sstevel@tonic-gate 			case '7':
620*7c478bd9Sstevel@tonic-gate 				*s++ = c;
621*7c478bd9Sstevel@tonic-gate 				w++;
622*7c478bd9Sstevel@tonic-gate 				continue;
623*7c478bd9Sstevel@tonic-gate 			default:
624*7c478bd9Sstevel@tonic-gate 				unget(c);
625*7c478bd9Sstevel@tonic-gate 				w = width;	/* force end of loop */
626*7c478bd9Sstevel@tonic-gate 				break;
627*7c478bd9Sstevel@tonic-gate 			}
628*7c478bd9Sstevel@tonic-gate 		}
629*7c478bd9Sstevel@tonic-gate 		*s = '\0';
630*7c478bd9Sstevel@tonic-gate 		ret = strtol(cbuf, (char **)0, 8);
631*7c478bd9Sstevel@tonic-gate 		goto retn;
632*7c478bd9Sstevel@tonic-gate 	}
633*7c478bd9Sstevel@tonic-gate 	if (base == 16) {
634*7c478bd9Sstevel@tonic-gate 		w = 0;
635*7c478bd9Sstevel@tonic-gate 		s = cbuf;
636*7c478bd9Sstevel@tonic-gate 		while (w < width && w < sizeof cbuf) {
637*7c478bd9Sstevel@tonic-gate 			c = getin();
638*7c478bd9Sstevel@tonic-gate 			c = toupper(c);
639*7c478bd9Sstevel@tonic-gate 			switch (c) {
640*7c478bd9Sstevel@tonic-gate 
641*7c478bd9Sstevel@tonic-gate 			case '0':
642*7c478bd9Sstevel@tonic-gate 			case '1':
643*7c478bd9Sstevel@tonic-gate 			case '2':
644*7c478bd9Sstevel@tonic-gate 			case '3':
645*7c478bd9Sstevel@tonic-gate 			case '4':
646*7c478bd9Sstevel@tonic-gate 			case '5':
647*7c478bd9Sstevel@tonic-gate 			case '6':
648*7c478bd9Sstevel@tonic-gate 			case '7':
649*7c478bd9Sstevel@tonic-gate 			case '8':
650*7c478bd9Sstevel@tonic-gate 			case '9':
651*7c478bd9Sstevel@tonic-gate 			case 'A':
652*7c478bd9Sstevel@tonic-gate 			case 'B':
653*7c478bd9Sstevel@tonic-gate 			case 'C':
654*7c478bd9Sstevel@tonic-gate 			case 'D':
655*7c478bd9Sstevel@tonic-gate 			case 'E':
656*7c478bd9Sstevel@tonic-gate 			case 'F':
657*7c478bd9Sstevel@tonic-gate 				*s++ = c;
658*7c478bd9Sstevel@tonic-gate 				w++;
659*7c478bd9Sstevel@tonic-gate 				continue;
660*7c478bd9Sstevel@tonic-gate 			default:
661*7c478bd9Sstevel@tonic-gate 				unget(c);
662*7c478bd9Sstevel@tonic-gate 				w = width;	/* force end of loop */
663*7c478bd9Sstevel@tonic-gate 				break;
664*7c478bd9Sstevel@tonic-gate 			}
665*7c478bd9Sstevel@tonic-gate 		}
666*7c478bd9Sstevel@tonic-gate 		*s = '\0';
667*7c478bd9Sstevel@tonic-gate 		ret = strtol(cbuf, (char **)0, 16);
668*7c478bd9Sstevel@tonic-gate 		goto retn;
669*7c478bd9Sstevel@tonic-gate 	}
670*7c478bd9Sstevel@tonic-gate 
671*7c478bd9Sstevel@tonic-gate /*
672*7c478bd9Sstevel@tonic-gate  * if we get this far then a bad base was passed.
673*7c478bd9Sstevel@tonic-gate  */
674*7c478bd9Sstevel@tonic-gate 	gfail = -1;
675*7c478bd9Sstevel@tonic-gate 
676*7c478bd9Sstevel@tonic-gate retn:
677*7c478bd9Sstevel@tonic-gate 	if (*cbuf == '\0')	/* No number at all?? */
678*7c478bd9Sstevel@tonic-gate 		gfail = -1;
679*7c478bd9Sstevel@tonic-gate 	if (neg)
680*7c478bd9Sstevel@tonic-gate 		ret = -ret;
681*7c478bd9Sstevel@tonic-gate 	return ret;
682*7c478bd9Sstevel@tonic-gate }
683*7c478bd9Sstevel@tonic-gate 
684*7c478bd9Sstevel@tonic-gate #ifdef	__FLOAT__
685*7c478bd9Sstevel@tonic-gate static double
lstrtod()686*7c478bd9Sstevel@tonic-gate lstrtod()
687*7c478bd9Sstevel@tonic-gate {
688*7c478bd9Sstevel@tonic-gate 	int	slen;
689*7c478bd9Sstevel@tonic-gate 	int	neg, eneg;
690*7c478bd9Sstevel@tonic-gate 	char	cbuf[CBUFSIZ];
691*7c478bd9Sstevel@tonic-gate 	register int	c;
692*7c478bd9Sstevel@tonic-gate 	register char	*sp, *s1, *s2, *s3;
693*7c478bd9Sstevel@tonic-gate 	double	total, exp, tens;
694*7c478bd9Sstevel@tonic-gate 
695*7c478bd9Sstevel@tonic-gate 	neg = eneg = 1;
696*7c478bd9Sstevel@tonic-gate 	gfail = 0;
697*7c478bd9Sstevel@tonic-gate 
698*7c478bd9Sstevel@tonic-gate 	whitespace();
699*7c478bd9Sstevel@tonic-gate 
700*7c478bd9Sstevel@tonic-gate 	c = getin();
701*7c478bd9Sstevel@tonic-gate 
702*7c478bd9Sstevel@tonic-gate 	if (c == '-' || c == '+')
703*7c478bd9Sstevel@tonic-gate 		if (c == '-') {
704*7c478bd9Sstevel@tonic-gate 			neg = -1;
705*7c478bd9Sstevel@tonic-gate 			c = getin();
706*7c478bd9Sstevel@tonic-gate 		}
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 	sp = s1 = cbuf;
709*7c478bd9Sstevel@tonic-gate 	while (c >= '0' && c <= '9') {
710*7c478bd9Sstevel@tonic-gate 		*sp++ = c;
711*7c478bd9Sstevel@tonic-gate 		c = getin();
712*7c478bd9Sstevel@tonic-gate 	}
713*7c478bd9Sstevel@tonic-gate 
714*7c478bd9Sstevel@tonic-gate 	s2 = sp;
715*7c478bd9Sstevel@tonic-gate 	if (c == '.') {
716*7c478bd9Sstevel@tonic-gate 		c = getin();
717*7c478bd9Sstevel@tonic-gate 		while (c >= '0' && c <= '9') {
718*7c478bd9Sstevel@tonic-gate 			*sp++ = c;
719*7c478bd9Sstevel@tonic-gate 			c = getin();
720*7c478bd9Sstevel@tonic-gate 		}
721*7c478bd9Sstevel@tonic-gate 	}
722*7c478bd9Sstevel@tonic-gate 
723*7c478bd9Sstevel@tonic-gate 	s3 = sp;
724*7c478bd9Sstevel@tonic-gate 	if (c == 'e' || c == 'E') {
725*7c478bd9Sstevel@tonic-gate 		c = getin();
726*7c478bd9Sstevel@tonic-gate 		if (c == '-' || c == '+')
727*7c478bd9Sstevel@tonic-gate 			if (c == '-') {
728*7c478bd9Sstevel@tonic-gate 				eneg = -1;
729*7c478bd9Sstevel@tonic-gate 				c = getin();
730*7c478bd9Sstevel@tonic-gate 			}
731*7c478bd9Sstevel@tonic-gate 		while (c >= '0' && c <= '9') {
732*7c478bd9Sstevel@tonic-gate 			*sp++ = c;
733*7c478bd9Sstevel@tonic-gate 			c = getin();
734*7c478bd9Sstevel@tonic-gate 		}
735*7c478bd9Sstevel@tonic-gate 	}
736*7c478bd9Sstevel@tonic-gate 	*sp = '\0';
737*7c478bd9Sstevel@tonic-gate 
738*7c478bd9Sstevel@tonic-gate 	if (s1 == s2 && s2 == s3) {
739*7c478bd9Sstevel@tonic-gate 		gfail = -1;
740*7c478bd9Sstevel@tonic-gate 		return 0.0;
741*7c478bd9Sstevel@tonic-gate 	}
742*7c478bd9Sstevel@tonic-gate 	unget(c);
743*7c478bd9Sstevel@tonic-gate 
744*7c478bd9Sstevel@tonic-gate 	/*
745*7c478bd9Sstevel@tonic-gate 	 * convert the three strings (integer, fraction, and exponent)
746*7c478bd9Sstevel@tonic-gate 	 * into a floating point number.
747*7c478bd9Sstevel@tonic-gate 	 */
748*7c478bd9Sstevel@tonic-gate 
749*7c478bd9Sstevel@tonic-gate 	total = 0.0;
750*7c478bd9Sstevel@tonic-gate 	tens = 1.0;
751*7c478bd9Sstevel@tonic-gate 	for (sp=s2-1; sp >= s1; sp--) {
752*7c478bd9Sstevel@tonic-gate 		total += (*sp -'0') * tens;
753*7c478bd9Sstevel@tonic-gate 		tens *= 10.0;
754*7c478bd9Sstevel@tonic-gate 	}
755*7c478bd9Sstevel@tonic-gate 
756*7c478bd9Sstevel@tonic-gate 	tens = .1;
757*7c478bd9Sstevel@tonic-gate 	for (sp=s2; sp < s3; sp++) {
758*7c478bd9Sstevel@tonic-gate 		total += (*sp - '0') * tens;
759*7c478bd9Sstevel@tonic-gate 		tens /= 10.0;
760*7c478bd9Sstevel@tonic-gate 	}
761*7c478bd9Sstevel@tonic-gate 	total *= (double)neg;
762*7c478bd9Sstevel@tonic-gate 
763*7c478bd9Sstevel@tonic-gate 	exp = 0.0;
764*7c478bd9Sstevel@tonic-gate 	tens = 1.0;
765*7c478bd9Sstevel@tonic-gate 	if ((slen = strlen(s3)) > 0) {
766*7c478bd9Sstevel@tonic-gate 		sp = s3 + slen - 1;
767*7c478bd9Sstevel@tonic-gate 		for ( ; sp >= s3; sp--) {
768*7c478bd9Sstevel@tonic-gate 			exp += (*sp - '0') * tens;
769*7c478bd9Sstevel@tonic-gate 			tens *= 10.0;
770*7c478bd9Sstevel@tonic-gate 		}
771*7c478bd9Sstevel@tonic-gate 	}
772*7c478bd9Sstevel@tonic-gate 	*sp = '\0';
773*7c478bd9Sstevel@tonic-gate 
774*7c478bd9Sstevel@tonic-gate 	exp *= (double)eneg;
775*7c478bd9Sstevel@tonic-gate 
776*7c478bd9Sstevel@tonic-gate 	total *= pow(10.0, exp);
777*7c478bd9Sstevel@tonic-gate 
778*7c478bd9Sstevel@tonic-gate 	return total;
779*7c478bd9Sstevel@tonic-gate }
780*7c478bd9Sstevel@tonic-gate #endif	/* __FLOAT__ */
781*7c478bd9Sstevel@tonic-gate 
782*7c478bd9Sstevel@tonic-gate static	int
getin()783*7c478bd9Sstevel@tonic-gate getin()
784*7c478bd9Sstevel@tonic-gate {
785*7c478bd9Sstevel@tonic-gate 	int	c;
786*7c478bd9Sstevel@tonic-gate 
787*7c478bd9Sstevel@tonic-gate 	if (ungot != EOF) {
788*7c478bd9Sstevel@tonic-gate 		c = ungot;
789*7c478bd9Sstevel@tonic-gate 		ungot = EOF;
790*7c478bd9Sstevel@tonic-gate 	} else
791*7c478bd9Sstevel@tonic-gate 		c = getc(fpin);
792*7c478bd9Sstevel@tonic-gate 	charcnt++;
793*7c478bd9Sstevel@tonic-gate 	return c;
794*7c478bd9Sstevel@tonic-gate }
795*7c478bd9Sstevel@tonic-gate 
796*7c478bd9Sstevel@tonic-gate static void
unget(int c)797*7c478bd9Sstevel@tonic-gate unget(int c)
798*7c478bd9Sstevel@tonic-gate {
799*7c478bd9Sstevel@tonic-gate 	/* Dont' use ungetc because it doesn't work with m_fsopen */
800*7c478bd9Sstevel@tonic-gate 	ungot = c;
801*7c478bd9Sstevel@tonic-gate 	charcnt--;
802*7c478bd9Sstevel@tonic-gate }
803*7c478bd9Sstevel@tonic-gate 
804