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 static char rcsid[] = "$OpenBSD: vfscanf.c,v 1.3 1996/09/15 09:31:46 tholo Exp $"; 39 #endif /* LIBC_SCCS and not lint */ 40 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <ctype.h> 44 #if __STDC__ 45 #include <stdarg.h> 46 #else 47 #include <varargs.h> 48 #endif 49 #include "local.h" 50 51 #ifdef FLOATING_POINT 52 #include "floatio.h" 53 #endif 54 55 #define BUF 513 /* Maximum length of numeric string. */ 56 57 /* 58 * Flags used during conversion. 59 */ 60 #define LONG 0x01 /* l: long or double */ 61 #define LONGDBL 0x02 /* L: long double; unimplemented */ 62 #define SHORT 0x04 /* h: short */ 63 #define QUAD 0x08 /* q: quad */ 64 #define SUPPRESS 0x10 /* suppress assignment */ 65 #define POINTER 0x20 /* weird %p pointer (`fake hex') */ 66 #define NOSKIP 0x40 /* do not skip blanks */ 67 68 /* 69 * The following are used in numeric conversions only: 70 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point; 71 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral. 72 */ 73 #define SIGNOK 0x080 /* +/- is (still) legal */ 74 #define NDIGITS 0x100 /* no digits detected */ 75 76 #define DPTOK 0x200 /* (float) decimal point is still legal */ 77 #define EXPOK 0x400 /* (float) exponent (e+3, etc) still legal */ 78 79 #define PFXOK 0x200 /* 0x prefix is (still) legal */ 80 #define NZDIGITS 0x400 /* no zero digits detected */ 81 82 /* 83 * Conversion types. 84 */ 85 #define CT_CHAR 0 /* %c conversion */ 86 #define CT_CCL 1 /* %[...] conversion */ 87 #define CT_STRING 2 /* %s conversion */ 88 #define CT_INT 3 /* integer, i.e., strtoq or strtouq */ 89 #define CT_FLOAT 4 /* floating, i.e., strtod */ 90 91 #define u_char unsigned char 92 #define u_long unsigned long 93 94 static u_char *__sccl(); 95 96 /* 97 * vfscanf 98 */ 99 __svfscanf(fp, fmt0, ap) 100 register FILE *fp; 101 char const *fmt0; 102 _BSD_VA_LIST_ ap; 103 { 104 register u_char *fmt = (u_char *)fmt0; 105 register int c; /* character from format, or conversion */ 106 register size_t width; /* field width, or 0 */ 107 register char *p; /* points into all kinds of strings */ 108 register int n; /* handy integer */ 109 register int flags; /* flags as defined above */ 110 register char *p0; /* saves original value of p when necessary */ 111 int nassigned; /* number of fields assigned */ 112 int nread; /* number of characters consumed from fp */ 113 int base; /* base argument to strtoq/strtouq */ 114 u_quad_t (*ccfn)(); /* conversion function (strtoq/strtouq) */ 115 char ccltab[256]; /* character class table for %[...] */ 116 char buf[BUF]; /* buffer for numeric conversions */ 117 118 /* `basefix' is used to avoid `if' tests in the integer scanner */ 119 static short basefix[17] = 120 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; 121 122 nassigned = 0; 123 nread = 0; 124 base = 0; /* XXX just to keep gcc happy */ 125 ccfn = NULL; /* XXX just to keep gcc happy */ 126 for (;;) { 127 c = *fmt++; 128 if (c == 0) 129 return (nassigned); 130 if (isspace(c)) { 131 for (;;) { 132 if (fp->_r <= 0 && __srefill(fp)) 133 return (nassigned); 134 if (!isspace(*fp->_p)) 135 break; 136 nread++, fp->_r--, fp->_p++; 137 } 138 continue; 139 } 140 if (c != '%') 141 goto literal; 142 width = 0; 143 flags = 0; 144 /* 145 * switch on the format. continue if done; 146 * break once format type is derived. 147 */ 148 again: c = *fmt++; 149 switch (c) { 150 case '%': 151 literal: 152 if (fp->_r <= 0 && __srefill(fp)) 153 goto input_failure; 154 if (*fp->_p != c) 155 goto match_failure; 156 fp->_r--, fp->_p++; 157 nread++; 158 continue; 159 160 case '*': 161 flags |= SUPPRESS; 162 goto again; 163 case 'L': 164 flags |= LONGDBL; 165 goto again; 166 case 'h': 167 flags |= SHORT; 168 goto again; 169 case 'l': 170 if (*fmt == 'l') { 171 fmt++; 172 flags |= QUAD; 173 } else { 174 flags |= LONG; 175 } 176 goto again; 177 case 'q': 178 flags |= QUAD; 179 goto again; 180 181 case '0': case '1': case '2': case '3': case '4': 182 case '5': case '6': case '7': case '8': case '9': 183 width = width * 10 + c - '0'; 184 goto again; 185 186 /* 187 * Conversions. 188 * Those marked `compat' are for 4.[123]BSD compatibility. 189 * 190 * (According to ANSI, E and X formats are supposed 191 * to the same as e and x. Sorry about that.) 192 */ 193 case 'D': /* compat */ 194 flags |= LONG; 195 /* FALLTHROUGH */ 196 case 'd': 197 c = CT_INT; 198 ccfn = (u_quad_t (*)())strtoq; 199 base = 10; 200 break; 201 202 case 'i': 203 c = CT_INT; 204 ccfn = (u_quad_t (*)())strtoq; 205 base = 0; 206 break; 207 208 case 'O': /* compat */ 209 flags |= LONG; 210 /* FALLTHROUGH */ 211 case 'o': 212 c = CT_INT; 213 ccfn = strtouq; 214 base = 8; 215 break; 216 217 case 'u': 218 c = CT_INT; 219 ccfn = strtouq; 220 base = 10; 221 break; 222 223 case 'X': 224 case 'x': 225 flags |= PFXOK; /* enable 0x prefixing */ 226 c = CT_INT; 227 ccfn = strtouq; 228 base = 16; 229 break; 230 231 #ifdef FLOATING_POINT 232 case 'E': 233 case 'G': 234 case 'e': 235 case 'f': 236 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 = strtouq; 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_quad_t (*)())strtoq; 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 strtoq/strtouq */ 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_quad_t res; 545 546 *p = 0; 547 res = (*ccfn)(buf, (char **)NULL, base); 548 if (flags & POINTER) 549 *va_arg(ap, void **) = 550 (void *)(long)res; 551 else if (flags & QUAD) 552 *va_arg(ap, quad_t *) = res; 553 else if (flags & LONG) 554 *va_arg(ap, long *) = res; 555 else if (flags & SHORT) 556 *va_arg(ap, short *) = res; 557 else 558 *va_arg(ap, int *) = res; 559 nassigned++; 560 } 561 nread += p - buf; 562 break; 563 564 #ifdef FLOATING_POINT 565 case CT_FLOAT: 566 /* scan a floating point number as if by strtod */ 567 #ifdef hardway 568 if (width == 0 || width > sizeof(buf) - 1) 569 width = sizeof(buf) - 1; 570 #else 571 /* size_t is unsigned, hence this optimisation */ 572 if (--width > sizeof(buf) - 2) 573 width = sizeof(buf) - 2; 574 width++; 575 #endif 576 flags |= SIGNOK | NDIGITS | DPTOK | EXPOK; 577 for (p = buf; width; width--) { 578 c = *fp->_p; 579 /* 580 * This code mimicks the integer conversion 581 * code, but is much simpler. 582 */ 583 switch (c) { 584 585 case '0': case '1': case '2': case '3': 586 case '4': case '5': case '6': case '7': 587 case '8': case '9': 588 flags &= ~(SIGNOK | NDIGITS); 589 goto fok; 590 591 case '+': case '-': 592 if (flags & SIGNOK) { 593 flags &= ~SIGNOK; 594 goto fok; 595 } 596 break; 597 case '.': 598 if (flags & DPTOK) { 599 flags &= ~(SIGNOK | DPTOK); 600 goto fok; 601 } 602 break; 603 case 'e': case 'E': 604 /* no exponent without some digits */ 605 if ((flags&(NDIGITS|EXPOK)) == EXPOK) { 606 flags = 607 (flags & ~(EXPOK|DPTOK)) | 608 SIGNOK | NDIGITS; 609 goto fok; 610 } 611 break; 612 } 613 break; 614 fok: 615 *p++ = c; 616 if (--fp->_r > 0) 617 fp->_p++; 618 else if (__srefill(fp)) 619 break; /* EOF */ 620 } 621 /* 622 * If no digits, might be missing exponent digits 623 * (just give back the exponent) or might be missing 624 * regular digits, but had sign and/or decimal point. 625 */ 626 if (flags & NDIGITS) { 627 if (flags & EXPOK) { 628 /* no digits at all */ 629 while (p > buf) 630 ungetc(*(u_char *)--p, fp); 631 goto match_failure; 632 } 633 /* just a bad exponent (e and maybe sign) */ 634 c = *(u_char *)--p; 635 if (c != 'e' && c != 'E') { 636 (void) ungetc(c, fp);/* sign */ 637 c = *(u_char *)--p; 638 } 639 (void) ungetc(c, fp); 640 } 641 if ((flags & SUPPRESS) == 0) { 642 double res; 643 644 *p = 0; 645 res = strtod(buf, (char **) NULL); 646 if (flags & LONGDBL) 647 *va_arg(ap, long double *) = res; 648 else if (flags & LONG) 649 *va_arg(ap, double *) = res; 650 else 651 *va_arg(ap, float *) = res; 652 nassigned++; 653 } 654 nread += p - buf; 655 break; 656 #endif /* FLOATING_POINT */ 657 } 658 } 659 input_failure: 660 return (nassigned ? nassigned : -1); 661 match_failure: 662 return (nassigned); 663 } 664 665 /* 666 * Fill in the given table from the scanset at the given format 667 * (just after `['). Return a pointer to the character past the 668 * closing `]'. The table has a 1 wherever characters should be 669 * considered part of the scanset. 670 */ 671 static u_char * 672 __sccl(tab, fmt) 673 register char *tab; 674 register u_char *fmt; 675 { 676 register int c, n, v; 677 678 /* first `clear' the whole table */ 679 c = *fmt++; /* first char hat => negated scanset */ 680 if (c == '^') { 681 v = 1; /* default => accept */ 682 c = *fmt++; /* get new first char */ 683 } else 684 v = 0; /* default => reject */ 685 /* should probably use memset here */ 686 for (n = 0; n < 256; n++) 687 tab[n] = v; 688 if (c == 0) 689 return (fmt - 1);/* format ended before closing ] */ 690 691 /* 692 * Now set the entries corresponding to the actual scanset 693 * to the opposite of the above. 694 * 695 * The first character may be ']' (or '-') without being special; 696 * the last character may be '-'. 697 */ 698 v = 1 - v; 699 for (;;) { 700 tab[c] = v; /* take character c */ 701 doswitch: 702 n = *fmt++; /* and examine the next */ 703 switch (n) { 704 705 case 0: /* format ended too soon */ 706 return (fmt - 1); 707 708 case '-': 709 /* 710 * A scanset of the form 711 * [01+-] 712 * is defined as `the digit 0, the digit 1, 713 * the character +, the character -', but 714 * the effect of a scanset such as 715 * [a-zA-Z0-9] 716 * is implementation defined. The V7 Unix 717 * scanf treats `a-z' as `the letters a through 718 * z', but treats `a-a' as `the letter a, the 719 * character -, and the letter a'. 720 * 721 * For compatibility, the `-' is not considerd 722 * to define a range if the character following 723 * it is either a close bracket (required by ANSI) 724 * or is not numerically greater than the character 725 * we just stored in the table (c). 726 */ 727 n = *fmt; 728 if (n == ']' || n < c) { 729 c = '-'; 730 break; /* resume the for(;;) */ 731 } 732 fmt++; 733 do { /* fill in the range */ 734 tab[++c] = v; 735 } while (c < n); 736 #if 1 /* XXX another disgusting compatibility hack */ 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