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