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