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