vfscanf.c revision 124175
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) 38static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93"; 39#endif /* LIBC_SCCS and not lint */ 40#include <sys/cdefs.h> 41__FBSDID("$FreeBSD: head/lib/libc/stdio/vfscanf.c 124175 2004-01-06 18:32:24Z nectar $"); 42 43#include "namespace.h" 44#include <ctype.h> 45#include <inttypes.h> 46#include <stdio.h> 47#include <stdlib.h> 48#include <stddef.h> 49#include <stdarg.h> 50#include <string.h> 51#include <wchar.h> 52#include <wctype.h> 53#include "un-namespace.h" 54 55#include "collate.h" 56#include "libc_private.h" 57#include "local.h" 58 59#define FLOATING_POINT 60 61#ifdef FLOATING_POINT 62#include <locale.h> 63#endif 64 65#define BUF 513 /* Maximum length of numeric string. */ 66 67/* 68 * Flags used during conversion. 69 */ 70#define LONG 0x01 /* l: long or double */ 71#define LONGDBL 0x02 /* L: long double */ 72#define SHORT 0x04 /* h: short */ 73#define SUPPRESS 0x08 /* *: suppress assignment */ 74#define POINTER 0x10 /* p: void * (as hex) */ 75#define NOSKIP 0x20 /* [ or c: do not skip blanks */ 76#define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */ 77#define INTMAXT 0x800 /* j: intmax_t */ 78#define PTRDIFFT 0x1000 /* t: ptrdiff_t */ 79#define SIZET 0x2000 /* z: size_t */ 80#define SHORTSHORT 0x4000 /* hh: char */ 81#define UNSIGNED 0x8000 /* %[oupxX] conversions */ 82 83/* 84 * The following are used in integral conversions only: 85 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS 86 */ 87#define SIGNOK 0x40 /* +/- is (still) legal */ 88#define NDIGITS 0x80 /* no digits detected */ 89#define PFXOK 0x100 /* 0x prefix is (still) legal */ 90#define NZDIGITS 0x200 /* no zero digits detected */ 91 92/* 93 * Conversion types. 94 */ 95#define CT_CHAR 0 /* %c conversion */ 96#define CT_CCL 1 /* %[...] conversion */ 97#define CT_STRING 2 /* %s conversion */ 98#define CT_INT 3 /* %[dioupxX] conversion */ 99#define CT_FLOAT 4 /* %[efgEFG] conversion */ 100 101static const u_char *__sccl(char *, const u_char *); 102static int parsefloat(FILE *, char *, char *); 103 104int __scanfdebug = 0; 105 106__weak_reference(__vfscanf, vfscanf); 107 108/* 109 * __vfscanf - MT-safe version 110 */ 111int 112__vfscanf(FILE *fp, char const *fmt0, va_list ap) 113{ 114 int ret; 115 116 FLOCKFILE(fp); 117 ret = __svfscanf(fp, fmt0, ap); 118 FUNLOCKFILE(fp); 119 return (ret); 120} 121 122/* 123 * __svfscanf - non-MT-safe version of __vfscanf 124 */ 125int 126__svfscanf(FILE *fp, const char *fmt0, va_list ap) 127{ 128 const u_char *fmt = (const u_char *)fmt0; 129 int c; /* character from format, or conversion */ 130 size_t width; /* field width, or 0 */ 131 char *p; /* points into all kinds of strings */ 132 int n; /* handy integer */ 133 int flags; /* flags as defined above */ 134 char *p0; /* saves original value of p when necessary */ 135 int nassigned; /* number of fields assigned */ 136 int nconversions; /* number of conversions */ 137 int nread; /* number of characters consumed from fp */ 138 int base; /* base argument to conversion function */ 139 char ccltab[256]; /* character class table for %[...] */ 140 char buf[BUF]; /* buffer for numeric and mb conversions */ 141 wchar_t *wcp; /* handy wide character pointer */ 142 wchar_t *wcp0; /* saves original value of wcp */ 143 size_t nconv; /* length of multibyte sequence converted */ 144 145 /* `basefix' is used to avoid `if' tests in the integer scanner */ 146 static short basefix[17] = 147 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; 148 149 ORIENT(fp, -1); 150 151 nassigned = 0; 152 nconversions = 0; 153 nread = 0; 154 for (;;) { 155 c = *fmt++; 156 if (c == 0) 157 return (nassigned); 158 if (isspace(c)) { 159 while ((fp->_r > 0 || __srefill(fp) == 0) && isspace(*fp->_p)) 160 nread++, fp->_r--, fp->_p++; 161 continue; 162 } 163 if (c != '%') 164 goto literal; 165 width = 0; 166 flags = 0; 167 /* 168 * switch on the format. continue if done; 169 * break once format type is derived. 170 */ 171again: c = *fmt++; 172 switch (c) { 173 case '%': 174literal: 175 if (fp->_r <= 0 && __srefill(fp)) 176 goto input_failure; 177 if (*fp->_p != c) 178 goto match_failure; 179 fp->_r--, fp->_p++; 180 nread++; 181 continue; 182 183 case '*': 184 flags |= SUPPRESS; 185 goto again; 186 case 'j': 187 flags |= INTMAXT; 188 goto again; 189 case 'l': 190 if (flags & LONG) { 191 flags &= ~LONG; 192 flags |= LONGLONG; 193 } else 194 flags |= LONG; 195 goto again; 196 case 'q': 197 flags |= LONGLONG; /* not quite */ 198 goto again; 199 case 't': 200 flags |= PTRDIFFT; 201 goto again; 202 case 'z': 203 flags |= SIZET; 204 goto again; 205 case 'L': 206 flags |= LONGDBL; 207 goto again; 208 case 'h': 209 if (flags & SHORT) { 210 flags &= ~SHORT; 211 flags |= SHORTSHORT; 212 } else 213 flags |= SHORT; 214 goto again; 215 216 case '0': case '1': case '2': case '3': case '4': 217 case '5': case '6': case '7': case '8': case '9': 218 width = width * 10 + c - '0'; 219 goto again; 220 221 /* 222 * Conversions. 223 */ 224 case 'd': 225 c = CT_INT; 226 base = 10; 227 break; 228 229 case 'i': 230 c = CT_INT; 231 base = 0; 232 break; 233 234 case 'o': 235 c = CT_INT; 236 flags |= UNSIGNED; 237 base = 8; 238 break; 239 240 case 'u': 241 c = CT_INT; 242 flags |= UNSIGNED; 243 base = 10; 244 break; 245 246 case 'X': 247 case 'x': 248 flags |= PFXOK; /* enable 0x prefixing */ 249 c = CT_INT; 250 flags |= UNSIGNED; 251 base = 16; 252 break; 253 254#ifdef FLOATING_POINT 255 case 'A': case 'E': case 'F': case 'G': 256 case 'a': case 'e': case 'f': case 'g': 257 c = CT_FLOAT; 258 break; 259#endif 260 261 case 'S': 262 flags |= LONG; 263 /* FALLTHROUGH */ 264 case 's': 265 c = CT_STRING; 266 break; 267 268 case '[': 269 fmt = __sccl(ccltab, fmt); 270 flags |= NOSKIP; 271 c = CT_CCL; 272 break; 273 274 case 'C': 275 flags |= LONG; 276 /* FALLTHROUGH */ 277 case 'c': 278 flags |= NOSKIP; 279 c = CT_CHAR; 280 break; 281 282 case 'p': /* pointer format is like hex */ 283 flags |= POINTER | PFXOK; 284 c = CT_INT; /* assumes sizeof(uintmax_t) */ 285 flags |= UNSIGNED; /* >= sizeof(uintptr_t) */ 286 base = 16; 287 break; 288 289 case 'n': 290 nconversions++; 291 if (flags & SUPPRESS) /* ??? */ 292 continue; 293 if (flags & SHORTSHORT) 294 *va_arg(ap, char *) = nread; 295 else if (flags & SHORT) 296 *va_arg(ap, short *) = nread; 297 else if (flags & LONG) 298 *va_arg(ap, long *) = nread; 299 else if (flags & LONGLONG) 300 *va_arg(ap, long long *) = nread; 301 else if (flags & INTMAXT) 302 *va_arg(ap, intmax_t *) = nread; 303 else if (flags & SIZET) 304 *va_arg(ap, size_t *) = nread; 305 else if (flags & PTRDIFFT) 306 *va_arg(ap, ptrdiff_t *) = nread; 307 else 308 *va_arg(ap, int *) = nread; 309 continue; 310 311 default: 312 goto match_failure; 313 314 /* 315 * Disgusting backwards compatibility hack. XXX 316 */ 317 case '\0': /* compat */ 318 return (EOF); 319 } 320 321 /* 322 * We have a conversion that requires input. 323 */ 324 if (fp->_r <= 0 && __srefill(fp)) 325 goto input_failure; 326 327 /* 328 * Consume leading white space, except for formats 329 * that suppress this. 330 */ 331 if ((flags & NOSKIP) == 0) { 332 while (isspace(*fp->_p)) { 333 nread++; 334 if (--fp->_r > 0) 335 fp->_p++; 336 else if (__srefill(fp)) 337 goto input_failure; 338 } 339 /* 340 * Note that there is at least one character in 341 * the buffer, so conversions that do not set NOSKIP 342 * ca no longer result in an input failure. 343 */ 344 } 345 346 /* 347 * Do the conversion. 348 */ 349 switch (c) { 350 351 case CT_CHAR: 352 /* scan arbitrary characters (sets NOSKIP) */ 353 if (width == 0) 354 width = 1; 355 if (flags & LONG) { 356 if ((flags & SUPPRESS) == 0) 357 wcp = va_arg(ap, wchar_t *); 358 else 359 wcp = NULL; 360 n = 0; 361 while (width != 0) { 362 if (n == MB_CUR_MAX) { 363 fp->_flags |= __SERR; 364 goto input_failure; 365 } 366 buf[n++] = *fp->_p; 367 fp->_p++; 368 fp->_r--; 369 nconv = mbrtowc(wcp, buf, n, NULL); 370 if (nconv == (size_t)-1) { 371 fp->_flags |= __SERR; 372 goto input_failure; 373 } 374 if (nconv == 0 && !(flags & SUPPRESS)) 375 *wcp = L'\0'; 376 if (nconv != (size_t)-2) { 377 nread += n; 378 width--; 379 if (!(flags & SUPPRESS)) 380 wcp++; 381 n = 0; 382 } 383 if (fp->_r <= 0 && __srefill(fp)) { 384 if (n != 0) { 385 fp->_flags |= __SERR; 386 goto input_failure; 387 } 388 break; 389 } 390 } 391 if (!(flags & SUPPRESS)) 392 nassigned++; 393 } else if (flags & SUPPRESS) { 394 size_t sum = 0; 395 for (;;) { 396 if ((n = fp->_r) < width) { 397 sum += n; 398 width -= n; 399 fp->_p += n; 400 if (__srefill(fp)) { 401 if (sum == 0) 402 goto input_failure; 403 break; 404 } 405 } else { 406 sum += width; 407 fp->_r -= width; 408 fp->_p += width; 409 break; 410 } 411 } 412 nread += sum; 413 } else { 414 size_t r = fread((void *)va_arg(ap, char *), 1, 415 width, fp); 416 417 if (r == 0) 418 goto input_failure; 419 nread += r; 420 nassigned++; 421 } 422 nconversions++; 423 break; 424 425 case CT_CCL: 426 /* scan a (nonempty) character class (sets NOSKIP) */ 427 if (width == 0) 428 width = (size_t)~0; /* `infinity' */ 429 /* take only those things in the class */ 430 if (flags & LONG) { 431 wchar_t twc; 432 int nchars; 433 434 if ((flags & SUPPRESS) == 0) 435 wcp = wcp0 = va_arg(ap, wchar_t *); 436 else 437 wcp = wcp0 = &twc; 438 n = 0; 439 nchars = 0; 440 while (width != 0) { 441 if (n == MB_CUR_MAX) { 442 fp->_flags |= __SERR; 443 goto input_failure; 444 } 445 buf[n++] = *fp->_p; 446 fp->_p++; 447 fp->_r--; 448 nconv = mbrtowc(wcp, buf, n, NULL); 449 if (nconv == (size_t)-1) { 450 fp->_flags |= __SERR; 451 goto input_failure; 452 } 453 if (nconv == 0) 454 *wcp = L'\0'; 455 if (nconv != (size_t)-2) { 456 if (wctob(*wcp) != EOF && 457 !ccltab[wctob(*wcp)]) { 458 while (n != 0) { 459 n--; 460 __ungetc(buf[n], 461 fp); 462 } 463 break; 464 } 465 nread += n; 466 width--; 467 if (!(flags & SUPPRESS)) 468 wcp++; 469 nchars++; 470 n = 0; 471 } 472 if (fp->_r <= 0 && __srefill(fp)) { 473 if (n != 0) { 474 fp->_flags |= __SERR; 475 goto input_failure; 476 } 477 break; 478 } 479 } 480 if (n != 0) { 481 fp->_flags |= __SERR; 482 goto input_failure; 483 } 484 n = nchars; 485 if (n == 0) 486 goto match_failure; 487 if (!(flags & SUPPRESS)) { 488 *wcp = L'\0'; 489 nassigned++; 490 } 491 } else if (flags & SUPPRESS) { 492 n = 0; 493 while (ccltab[*fp->_p]) { 494 n++, fp->_r--, fp->_p++; 495 if (--width == 0) 496 break; 497 if (fp->_r <= 0 && __srefill(fp)) { 498 if (n == 0) 499 goto input_failure; 500 break; 501 } 502 } 503 if (n == 0) 504 goto match_failure; 505 } else { 506 p0 = p = va_arg(ap, char *); 507 while (ccltab[*fp->_p]) { 508 fp->_r--; 509 *p++ = *fp->_p++; 510 if (--width == 0) 511 break; 512 if (fp->_r <= 0 && __srefill(fp)) { 513 if (p == p0) 514 goto input_failure; 515 break; 516 } 517 } 518 n = p - p0; 519 if (n == 0) 520 goto match_failure; 521 *p = 0; 522 nassigned++; 523 } 524 nread += n; 525 nconversions++; 526 break; 527 528 case CT_STRING: 529 /* like CCL, but zero-length string OK, & no NOSKIP */ 530 if (width == 0) 531 width = (size_t)~0; 532 if (flags & LONG) { 533 wchar_t twc; 534 535 if ((flags & SUPPRESS) == 0) 536 wcp = va_arg(ap, wchar_t *); 537 else 538 wcp = &twc; 539 n = 0; 540 while (!isspace(*fp->_p) && width != 0) { 541 if (n == MB_CUR_MAX) { 542 fp->_flags |= __SERR; 543 goto input_failure; 544 } 545 buf[n++] = *fp->_p; 546 fp->_p++; 547 fp->_r--; 548 nconv = mbrtowc(wcp, buf, n, NULL); 549 if (nconv == (size_t)-1) { 550 fp->_flags |= __SERR; 551 goto input_failure; 552 } 553 if (nconv == 0) 554 *wcp = L'\0'; 555 if (nconv != (size_t)-2) { 556 if (iswspace(*wcp)) { 557 while (n != 0) { 558 n--; 559 __ungetc(buf[n], 560 fp); 561 } 562 break; 563 } 564 nread += n; 565 width--; 566 if (!(flags & SUPPRESS)) 567 wcp++; 568 n = 0; 569 } 570 if (fp->_r <= 0 && __srefill(fp)) { 571 if (n != 0) { 572 fp->_flags |= __SERR; 573 goto input_failure; 574 } 575 break; 576 } 577 } 578 if (!(flags & SUPPRESS)) { 579 *wcp = L'\0'; 580 nassigned++; 581 } 582 } else if (flags & SUPPRESS) { 583 n = 0; 584 while (!isspace(*fp->_p)) { 585 n++, fp->_r--, fp->_p++; 586 if (--width == 0) 587 break; 588 if (fp->_r <= 0 && __srefill(fp)) 589 break; 590 } 591 nread += n; 592 } else { 593 p0 = p = va_arg(ap, char *); 594 while (!isspace(*fp->_p)) { 595 fp->_r--; 596 *p++ = *fp->_p++; 597 if (--width == 0) 598 break; 599 if (fp->_r <= 0 && __srefill(fp)) 600 break; 601 } 602 *p = 0; 603 nread += p - p0; 604 nassigned++; 605 } 606 nconversions++; 607 continue; 608 609 case CT_INT: 610 /* scan an integer as if by the conversion function */ 611#ifdef hardway 612 if (width == 0 || width > sizeof(buf) - 1) 613 width = sizeof(buf) - 1; 614#else 615 /* size_t is unsigned, hence this optimisation */ 616 if (--width > sizeof(buf) - 2) 617 width = sizeof(buf) - 2; 618 width++; 619#endif 620 flags |= SIGNOK | NDIGITS | NZDIGITS; 621 for (p = buf; width; width--) { 622 c = *fp->_p; 623 /* 624 * Switch on the character; `goto ok' 625 * if we accept it as a part of number. 626 */ 627 switch (c) { 628 629 /* 630 * The digit 0 is always legal, but is 631 * special. For %i conversions, if no 632 * digits (zero or nonzero) have been 633 * scanned (only signs), we will have 634 * base==0. In that case, we should set 635 * it to 8 and enable 0x prefixing. 636 * Also, if we have not scanned zero digits 637 * before this, do not turn off prefixing 638 * (someone else will turn it off if we 639 * have scanned any nonzero digits). 640 */ 641 case '0': 642 if (base == 0) { 643 base = 8; 644 flags |= PFXOK; 645 } 646 if (flags & NZDIGITS) 647 flags &= ~(SIGNOK|NZDIGITS|NDIGITS); 648 else 649 flags &= ~(SIGNOK|PFXOK|NDIGITS); 650 goto ok; 651 652 /* 1 through 7 always legal */ 653 case '1': case '2': case '3': 654 case '4': case '5': case '6': case '7': 655 base = basefix[base]; 656 flags &= ~(SIGNOK | PFXOK | NDIGITS); 657 goto ok; 658 659 /* digits 8 and 9 ok iff decimal or hex */ 660 case '8': case '9': 661 base = basefix[base]; 662 if (base <= 8) 663 break; /* not legal here */ 664 flags &= ~(SIGNOK | PFXOK | NDIGITS); 665 goto ok; 666 667 /* letters ok iff hex */ 668 case 'A': case 'B': case 'C': 669 case 'D': case 'E': case 'F': 670 case 'a': case 'b': case 'c': 671 case 'd': case 'e': case 'f': 672 /* no need to fix base here */ 673 if (base <= 10) 674 break; /* not legal here */ 675 flags &= ~(SIGNOK | PFXOK | NDIGITS); 676 goto ok; 677 678 /* sign ok only as first character */ 679 case '+': case '-': 680 if (flags & SIGNOK) { 681 flags &= ~SIGNOK; 682 goto ok; 683 } 684 break; 685 686 /* x ok iff flag still set & 2nd char */ 687 case 'x': case 'X': 688 if (flags & PFXOK && p == buf + 1) { 689 base = 16; /* if %i */ 690 flags &= ~PFXOK; 691 goto ok; 692 } 693 break; 694 } 695 696 /* 697 * If we got here, c is not a legal character 698 * for a number. Stop accumulating digits. 699 */ 700 break; 701 ok: 702 /* 703 * c is legal: store it and look at the next. 704 */ 705 *p++ = c; 706 if (--fp->_r > 0) 707 fp->_p++; 708 else if (__srefill(fp)) 709 break; /* EOF */ 710 } 711 /* 712 * If we had only a sign, it is no good; push 713 * back the sign. If the number ends in `x', 714 * it was [sign] '0' 'x', so push back the x 715 * and treat it as [sign] '0'. 716 */ 717 if (flags & NDIGITS) { 718 if (p > buf) 719 (void) __ungetc(*(u_char *)--p, fp); 720 goto match_failure; 721 } 722 c = ((u_char *)p)[-1]; 723 if (c == 'x' || c == 'X') { 724 --p; 725 (void) __ungetc(c, fp); 726 } 727 if ((flags & SUPPRESS) == 0) { 728 uintmax_t res; 729 730 *p = 0; 731 if ((flags & UNSIGNED) == 0) 732 res = strtoimax(buf, (char **)NULL, base); 733 else 734 res = strtoumax(buf, (char **)NULL, base); 735 if (flags & POINTER) 736 *va_arg(ap, void **) = 737 (void *)(uintptr_t)res; 738 else if (flags & SHORTSHORT) 739 *va_arg(ap, char *) = res; 740 else if (flags & SHORT) 741 *va_arg(ap, short *) = res; 742 else if (flags & LONG) 743 *va_arg(ap, long *) = res; 744 else if (flags & LONGLONG) 745 *va_arg(ap, long long *) = res; 746 else if (flags & INTMAXT) 747 *va_arg(ap, intmax_t *) = res; 748 else if (flags & PTRDIFFT) 749 *va_arg(ap, ptrdiff_t *) = res; 750 else if (flags & SIZET) 751 *va_arg(ap, size_t *) = res; 752 else 753 *va_arg(ap, int *) = res; 754 nassigned++; 755 } 756 nread += p - buf; 757 nconversions++; 758 break; 759 760#ifdef FLOATING_POINT 761 case CT_FLOAT: 762 /* scan a floating point number as if by strtod */ 763 if (width == 0 || width > sizeof(buf) - 1) 764 width = sizeof(buf) - 1; 765 if ((width = parsefloat(fp, buf, buf + width)) == 0) 766 goto match_failure; 767 if ((flags & SUPPRESS) == 0) { 768 if (flags & LONGDBL) { 769 long double res = strtold(buf, &p); 770 *va_arg(ap, long double *) = res; 771 } else if (flags & LONG) { 772 double res = strtod(buf, &p); 773 *va_arg(ap, double *) = res; 774 } else { 775 float res = strtof(buf, &p); 776 *va_arg(ap, float *) = res; 777 } 778 if (__scanfdebug && p - buf != width) 779 abort(); 780 nassigned++; 781 } 782 nread += width; 783 nconversions++; 784 break; 785#endif /* FLOATING_POINT */ 786 } 787 } 788input_failure: 789 return (nconversions != 0 ? nassigned : EOF); 790match_failure: 791 return (nassigned); 792} 793 794/* 795 * Fill in the given table from the scanset at the given format 796 * (just after `['). Return a pointer to the character past the 797 * closing `]'. The table has a 1 wherever characters should be 798 * considered part of the scanset. 799 */ 800static const u_char * 801__sccl(tab, fmt) 802 char *tab; 803 const u_char *fmt; 804{ 805 int c, n, v, i; 806 807 /* first `clear' the whole table */ 808 c = *fmt++; /* first char hat => negated scanset */ 809 if (c == '^') { 810 v = 1; /* default => accept */ 811 c = *fmt++; /* get new first char */ 812 } else 813 v = 0; /* default => reject */ 814 815 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */ 816 (void) memset(tab, v, 256); 817 818 if (c == 0) 819 return (fmt - 1);/* format ended before closing ] */ 820 821 /* 822 * Now set the entries corresponding to the actual scanset 823 * to the opposite of the above. 824 * 825 * The first character may be ']' (or '-') without being special; 826 * the last character may be '-'. 827 */ 828 v = 1 - v; 829 for (;;) { 830 tab[c] = v; /* take character c */ 831doswitch: 832 n = *fmt++; /* and examine the next */ 833 switch (n) { 834 835 case 0: /* format ended too soon */ 836 return (fmt - 1); 837 838 case '-': 839 /* 840 * A scanset of the form 841 * [01+-] 842 * is defined as `the digit 0, the digit 1, 843 * the character +, the character -', but 844 * the effect of a scanset such as 845 * [a-zA-Z0-9] 846 * is implementation defined. The V7 Unix 847 * scanf treats `a-z' as `the letters a through 848 * z', but treats `a-a' as `the letter a, the 849 * character -, and the letter a'. 850 * 851 * For compatibility, the `-' is not considerd 852 * to define a range if the character following 853 * it is either a close bracket (required by ANSI) 854 * or is not numerically greater than the character 855 * we just stored in the table (c). 856 */ 857 n = *fmt; 858 if (n == ']' 859 || (__collate_load_error ? n < c : 860 __collate_range_cmp (n, c) < 0 861 ) 862 ) { 863 c = '-'; 864 break; /* resume the for(;;) */ 865 } 866 fmt++; 867 /* fill in the range */ 868 if (__collate_load_error) { 869 do { 870 tab[++c] = v; 871 } while (c < n); 872 } else { 873 for (i = 0; i < 256; i ++) 874 if ( __collate_range_cmp (c, i) < 0 875 && __collate_range_cmp (i, n) <= 0 876 ) 877 tab[i] = v; 878 } 879#if 1 /* XXX another disgusting compatibility hack */ 880 c = n; 881 /* 882 * Alas, the V7 Unix scanf also treats formats 883 * such as [a-c-e] as `the letters a through e'. 884 * This too is permitted by the standard.... 885 */ 886 goto doswitch; 887#else 888 c = *fmt++; 889 if (c == 0) 890 return (fmt - 1); 891 if (c == ']') 892 return (fmt); 893#endif 894 break; 895 896 case ']': /* end of scanset */ 897 return (fmt); 898 899 default: /* just another character */ 900 c = n; 901 break; 902 } 903 } 904 /* NOTREACHED */ 905} 906 907#ifdef FLOATING_POINT 908static int 909parsefloat(FILE *fp, char *buf, char *end) 910{ 911 char *commit, *p; 912 int infnanpos = 0; 913 enum { 914 S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX, 915 S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS 916 } state = S_START; 917 unsigned char c; 918 char decpt = *localeconv()->decimal_point; 919 _Bool gotmantdig = 0, ishex = 0; 920 921 /* 922 * We set commit = p whenever the string we have read so far 923 * constitutes a valid representation of a floating point 924 * number by itself. At some point, the parse will complete 925 * or fail, and we will ungetc() back to the last commit point. 926 * To ensure that the file offset gets updated properly, it is 927 * always necessary to read at least one character that doesn't 928 * match; thus, we can't short-circuit "infinity" or "nan(...)". 929 */ 930 commit = buf - 1; 931 for (p = buf; p < end; ) { 932 c = *fp->_p; 933reswitch: 934 switch (state) { 935 case S_START: 936 state = S_GOTSIGN; 937 if (c == '-' || c == '+') 938 break; 939 else 940 goto reswitch; 941 case S_GOTSIGN: 942 switch (c) { 943 case '0': 944 state = S_MAYBEHEX; 945 commit = p; 946 break; 947 case 'I': 948 case 'i': 949 state = S_INF; 950 break; 951 case 'N': 952 case 'n': 953 state = S_NAN; 954 break; 955 default: 956 state = S_DIGITS; 957 goto reswitch; 958 } 959 break; 960 case S_INF: 961 if (infnanpos > 6 || 962 (c != "nfinity"[infnanpos] && 963 c != "NFINITY"[infnanpos])) 964 goto parsedone; 965 if (infnanpos == 1 || infnanpos == 6) 966 commit = p; /* inf or infinity */ 967 infnanpos++; 968 break; 969 case S_NAN: 970 switch (infnanpos) { 971 case -1: /* XXX kludge to deal with nan(...) */ 972 goto parsedone; 973 case 0: 974 if (c != 'A' && c != 'a') 975 goto parsedone; 976 break; 977 case 1: 978 if (c != 'N' && c != 'n') 979 goto parsedone; 980 else 981 commit = p; 982 break; 983 case 2: 984 if (c != '(') 985 goto parsedone; 986 break; 987 default: 988 if (c == ')') { 989 commit = p; 990 infnanpos = -2; 991 } else if (!isalnum(c) && c != '_') 992 goto parsedone; 993 break; 994 } 995 infnanpos++; 996 break; 997 case S_MAYBEHEX: 998 state = S_DIGITS; 999 if (c == 'X' || c == 'x') { 1000 ishex = 1; 1001 break; 1002 } else { /* we saw a '0', but no 'x' */ 1003 gotmantdig = 1; 1004 goto reswitch; 1005 } 1006 case S_DIGITS: 1007 if ((ishex && isxdigit(c)) || isdigit(c)) 1008 gotmantdig = 1; 1009 else { 1010 state = S_FRAC; 1011 if (c != decpt) 1012 goto reswitch; 1013 } 1014 if (gotmantdig) 1015 commit = p; 1016 break; 1017 case S_FRAC: 1018 if (((c == 'E' || c == 'e') && !ishex) || 1019 ((c == 'P' || c == 'p') && ishex)) { 1020 if (!gotmantdig) 1021 goto parsedone; 1022 else 1023 state = S_EXP; 1024 } else if ((ishex && isxdigit(c)) || isdigit(c)) { 1025 commit = p; 1026 gotmantdig = 1; 1027 } else 1028 goto parsedone; 1029 break; 1030 case S_EXP: 1031 state = S_EXPDIGITS; 1032 if (c == '-' || c == '+') 1033 break; 1034 else 1035 goto reswitch; 1036 case S_EXPDIGITS: 1037 if (isdigit(c)) 1038 commit = p; 1039 else 1040 goto parsedone; 1041 break; 1042 default: 1043 abort(); 1044 } 1045 *p++ = c; 1046 if (--fp->_r > 0) 1047 fp->_p++; 1048 else if (__srefill(fp)) 1049 break; /* EOF */ 1050 } 1051 1052parsedone: 1053 while (commit < --p) 1054 __ungetc(*(u_char *)p, fp); 1055 *++commit = '\0'; 1056 return (commit - buf); 1057} 1058#endif 1059