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