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