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