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