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