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