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