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