1276331Snwhitehorn/*- 2276331Snwhitehorn * Copyright (c) 1990, 1993 3276331Snwhitehorn * The Regents of the University of California. All rights reserved. 4276331Snwhitehorn * 5276331Snwhitehorn * This code is derived from software contributed to Berkeley by 6276331Snwhitehorn * Chris Torek. 7276331Snwhitehorn * 8276331Snwhitehorn * Redistribution and use in source and binary forms, with or without 9276331Snwhitehorn * modification, are permitted provided that the following conditions 10276331Snwhitehorn * are met: 11276331Snwhitehorn * 1. Redistributions of source code must retain the above copyright 12276331Snwhitehorn * notice, this list of conditions and the following disclaimer. 13276331Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright 14276331Snwhitehorn * notice, this list of conditions and the following disclaimer in the 15276331Snwhitehorn * documentation and/or other materials provided with the distribution. 16276331Snwhitehorn * 4. Neither the name of the University nor the names of its contributors 17276331Snwhitehorn * may be used to endorse or promote products derived from this software 18276331Snwhitehorn * without specific prior written permission. 19276331Snwhitehorn * 20276331Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21276331Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22276331Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23276331Snwhitehorn * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24276331Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25276331Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26276331Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27276331Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28276331Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29276331Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30276331Snwhitehorn * SUCH DAMAGE. 31276331Snwhitehorn * 32276331Snwhitehorn * From: Id: vfscanf.c,v 1.13 1998/09/25 12:20:27 obrien Exp 33276331Snwhitehorn * From: static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 34276331Snwhitehorn * From: static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93"; 35276331Snwhitehorn */ 36276331Snwhitehorn 37276507Snwhitehorn#include <sys/cdefs.h> 38276331Snwhitehorn__FBSDID("$FreeBSD: releng/10.3/sys/kern/subr_scanf.c 230587 2012-01-26 16:35:09Z ken $"); 39276331Snwhitehorn 40276331Snwhitehorn#include <sys/param.h> 41276331Snwhitehorn#include <sys/systm.h> 42276331Snwhitehorn#include <sys/ctype.h> 43276331Snwhitehorn#include <sys/limits.h> 44276331Snwhitehorn 45276331Snwhitehorn/* 46276331Snwhitehorn * Note that stdarg.h and the ANSI style va_start macro is used for both 47276331Snwhitehorn * ANSI and traditional C compilers. 48276331Snwhitehorn */ 49276331Snwhitehorn#include <machine/stdarg.h> 50276331Snwhitehorn 51276331Snwhitehorn#define BUF 32 /* Maximum length of numeric string. */ 52276331Snwhitehorn 53276331Snwhitehorn/* 54276331Snwhitehorn * Flags used during conversion. 55276331Snwhitehorn */ 56276331Snwhitehorn#define LONG 0x01 /* l: long or double */ 57276331Snwhitehorn#define SHORT 0x04 /* h: short */ 58276331Snwhitehorn#define SUPPRESS 0x08 /* suppress assignment */ 59276331Snwhitehorn#define POINTER 0x10 /* weird %p pointer (`fake hex') */ 60276331Snwhitehorn#define NOSKIP 0x20 /* do not skip blanks */ 61276331Snwhitehorn#define QUAD 0x400 62276331Snwhitehorn#define SHORTSHORT 0x4000 /** hh: char */ 63276331Snwhitehorn 64276331Snwhitehorn/* 65276331Snwhitehorn * The following are used in numeric conversions only: 66276331Snwhitehorn * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point; 67276331Snwhitehorn * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral. 68276331Snwhitehorn */ 69276331Snwhitehorn#define SIGNOK 0x40 /* +/- is (still) legal */ 70276331Snwhitehorn#define NDIGITS 0x80 /* no digits detected */ 71276331Snwhitehorn 72276331Snwhitehorn#define DPTOK 0x100 /* (float) decimal point is still legal */ 73276331Snwhitehorn#define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */ 74276331Snwhitehorn 75276331Snwhitehorn#define PFXOK 0x100 /* 0x prefix is (still) legal */ 76276331Snwhitehorn#define NZDIGITS 0x200 /* no zero digits detected */ 77276331Snwhitehorn 78276331Snwhitehorn/* 79276331Snwhitehorn * Conversion types. 80276331Snwhitehorn */ 81276331Snwhitehorn#define CT_CHAR 0 /* %c conversion */ 82276331Snwhitehorn#define CT_CCL 1 /* %[...] conversion */ 83276331Snwhitehorn#define CT_STRING 2 /* %s conversion */ 84276331Snwhitehorn#define CT_INT 3 /* integer, i.e., strtoq or strtouq */ 85276331Snwhitehorntypedef u_quad_t (*ccfntype)(const char *, char **, int); 86276331Snwhitehorn 87276331Snwhitehornstatic const u_char *__sccl(char *, const u_char *); 88276331Snwhitehorn 89276331Snwhitehornint 90276331Snwhitehornsscanf(const char *ibuf, const char *fmt, ...) 91276331Snwhitehorn{ 92276331Snwhitehorn va_list ap; 93276331Snwhitehorn int ret; 94276331Snwhitehorn 95276331Snwhitehorn va_start(ap, fmt); 96276331Snwhitehorn ret = vsscanf(ibuf, fmt, ap); 97276331Snwhitehorn va_end(ap); 98276331Snwhitehorn return(ret); 99276331Snwhitehorn} 100276331Snwhitehorn 101276331Snwhitehornint 102276331Snwhitehornvsscanf(const char *inp, char const *fmt0, va_list ap) 103276331Snwhitehorn{ 104276331Snwhitehorn int inr; 105276331Snwhitehorn const u_char *fmt = (const u_char *)fmt0; 106276331Snwhitehorn int c; /* character from format, or conversion */ 107276331Snwhitehorn size_t width; /* field width, or 0 */ 108276331Snwhitehorn char *p; /* points into all kinds of strings */ 109276331Snwhitehorn int n; /* handy integer */ 110276331Snwhitehorn int flags; /* flags as defined above */ 111276331Snwhitehorn char *p0; /* saves original value of p when necessary */ 112276331Snwhitehorn int nassigned; /* number of fields assigned */ 113276331Snwhitehorn int nconversions; /* number of conversions */ 114276331Snwhitehorn int nread; /* number of characters consumed from fp */ 115276331Snwhitehorn int base; /* base argument to strtoq/strtouq */ 116276331Snwhitehorn ccfntype ccfn; /* conversion function (strtoq/strtouq) */ 117276331Snwhitehorn char ccltab[256]; /* character class table for %[...] */ 118276331Snwhitehorn char buf[BUF]; /* buffer for numeric conversions */ 119276331Snwhitehorn 120276331Snwhitehorn /* `basefix' is used to avoid `if' tests in the integer scanner */ 121276331Snwhitehorn static short basefix[17] = 122276331Snwhitehorn { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; 123276331Snwhitehorn 124276331Snwhitehorn inr = strlen(inp); 125276331Snwhitehorn 126276331Snwhitehorn nassigned = 0; 127276331Snwhitehorn nconversions = 0; 128276331Snwhitehorn nread = 0; 129276331Snwhitehorn base = 0; /* XXX just to keep gcc happy */ 130276331Snwhitehorn ccfn = NULL; /* XXX just to keep gcc happy */ 131276331Snwhitehorn for (;;) { 132276331Snwhitehorn c = *fmt++; 133276331Snwhitehorn if (c == 0) 134276331Snwhitehorn return (nassigned); 135276331Snwhitehorn if (isspace(c)) { 136276331Snwhitehorn while (inr > 0 && isspace(*inp)) 137276331Snwhitehorn nread++, inr--, inp++; 138276331Snwhitehorn continue; 139276331Snwhitehorn } 140276331Snwhitehorn if (c != '%') 141276331Snwhitehorn goto literal; 142276331Snwhitehorn width = 0; 143276331Snwhitehorn flags = 0; 144276331Snwhitehorn /* 145276331Snwhitehorn * switch on the format. continue if done; 146276331Snwhitehorn * break once format type is derived. 147276331Snwhitehorn */ 148276331Snwhitehornagain: c = *fmt++; 149276331Snwhitehorn switch (c) { 150276331Snwhitehorn case '%': 151276331Snwhitehornliteral: 152276331Snwhitehorn if (inr <= 0) 153276331Snwhitehorn goto input_failure; 154276331Snwhitehorn if (*inp != c) 155276331Snwhitehorn goto match_failure; 156276331Snwhitehorn inr--, inp++; 157276331Snwhitehorn nread++; 158276331Snwhitehorn continue; 159276331Snwhitehorn 160276331Snwhitehorn case '*': 161276331Snwhitehorn flags |= SUPPRESS; 162276331Snwhitehorn goto again; 163276331Snwhitehorn case 'l': 164276331Snwhitehorn if (flags & LONG){ 165276331Snwhitehorn flags &= ~LONG; 166276331Snwhitehorn flags |= QUAD; 167276331Snwhitehorn } else { 168276331Snwhitehorn flags |= LONG; 169276331Snwhitehorn } 170276331Snwhitehorn goto again; 171276331Snwhitehorn case 'q': 172276331Snwhitehorn flags |= QUAD; 173276331Snwhitehorn goto again; 174276331Snwhitehorn case 'h': 175276331Snwhitehorn if (flags & SHORT){ 176276331Snwhitehorn flags &= ~SHORT; 177276331Snwhitehorn flags |= SHORTSHORT; 178276331Snwhitehorn } else { 179276331Snwhitehorn flags |= SHORT; 180276331Snwhitehorn } 181276331Snwhitehorn goto again; 182276331Snwhitehorn 183276331Snwhitehorn case '0': case '1': case '2': case '3': case '4': 184276331Snwhitehorn case '5': case '6': case '7': case '8': case '9': 185276331Snwhitehorn width = width * 10 + c - '0'; 186276331Snwhitehorn goto again; 187276331Snwhitehorn 188276331Snwhitehorn /* 189276331Snwhitehorn * Conversions. 190276331Snwhitehorn * 191276331Snwhitehorn */ 192276331Snwhitehorn case 'd': 193276331Snwhitehorn c = CT_INT; 194276331Snwhitehorn ccfn = (ccfntype)strtoq; 195276331Snwhitehorn base = 10; 196276331Snwhitehorn break; 197276331Snwhitehorn 198276331Snwhitehorn case 'i': 199276331Snwhitehorn c = CT_INT; 200276331Snwhitehorn ccfn = (ccfntype)strtoq; 201276331Snwhitehorn base = 0; 202276331Snwhitehorn break; 203276331Snwhitehorn 204276331Snwhitehorn case 'o': 205276331Snwhitehorn c = CT_INT; 206276331Snwhitehorn ccfn = strtouq; 207276331Snwhitehorn base = 8; 208276331Snwhitehorn break; 209276331Snwhitehorn 210276331Snwhitehorn case 'u': 211276331Snwhitehorn c = CT_INT; 212276331Snwhitehorn ccfn = strtouq; 213276331Snwhitehorn base = 10; 214276331Snwhitehorn break; 215276331Snwhitehorn 216276331Snwhitehorn case 'x': 217276331Snwhitehorn flags |= PFXOK; /* enable 0x prefixing */ 218276331Snwhitehorn c = CT_INT; 219276331Snwhitehorn ccfn = strtouq; 220276331Snwhitehorn base = 16; 221276331Snwhitehorn break; 222276331Snwhitehorn 223276331Snwhitehorn case 's': 224276331Snwhitehorn c = CT_STRING; 225276331Snwhitehorn break; 226276331Snwhitehorn 227276331Snwhitehorn case '[': 228276331Snwhitehorn fmt = __sccl(ccltab, fmt); 229276331Snwhitehorn flags |= NOSKIP; 230276331Snwhitehorn c = CT_CCL; 231276331Snwhitehorn break; 232276331Snwhitehorn 233276331Snwhitehorn case 'c': 234276331Snwhitehorn flags |= NOSKIP; 235276331Snwhitehorn c = CT_CHAR; 236276331Snwhitehorn break; 237276331Snwhitehorn 238276331Snwhitehorn case 'p': /* pointer format is like hex */ 239276331Snwhitehorn flags |= POINTER | PFXOK; 240276331Snwhitehorn c = CT_INT; 241276331Snwhitehorn ccfn = strtouq; 242276331Snwhitehorn base = 16; 243276331Snwhitehorn break; 244276331Snwhitehorn 245276331Snwhitehorn case 'n': 246276506Snwhitehorn nconversions++; 247276331Snwhitehorn if (flags & SUPPRESS) /* ??? */ 248276331Snwhitehorn continue; 249276331Snwhitehorn if (flags & SHORTSHORT) 250276331Snwhitehorn *va_arg(ap, char *) = nread; 251276331Snwhitehorn else if (flags & SHORT) 252276331Snwhitehorn *va_arg(ap, short *) = nread; 253276331Snwhitehorn else if (flags & LONG) 254276506Snwhitehorn *va_arg(ap, long *) = nread; 255276331Snwhitehorn else if (flags & QUAD) 256276331Snwhitehorn *va_arg(ap, quad_t *) = nread; 257276331Snwhitehorn else 258276331Snwhitehorn *va_arg(ap, int *) = nread; 259276331Snwhitehorn continue; 260276331Snwhitehorn } 261276331Snwhitehorn 262276331Snwhitehorn /* 263276331Snwhitehorn * We have a conversion that requires input. 264276331Snwhitehorn */ 265276331Snwhitehorn if (inr <= 0) 266276331Snwhitehorn goto input_failure; 267276331Snwhitehorn 268276331Snwhitehorn /* 269276331Snwhitehorn * Consume leading white space, except for formats 270276331Snwhitehorn * that suppress this. 271276331Snwhitehorn */ 272276331Snwhitehorn if ((flags & NOSKIP) == 0) { 273276331Snwhitehorn while (isspace(*inp)) { 274276331Snwhitehorn nread++; 275276331Snwhitehorn if (--inr > 0) 276276331Snwhitehorn inp++; 277276331Snwhitehorn else 278276331Snwhitehorn goto input_failure; 279276331Snwhitehorn } 280276331Snwhitehorn /* 281276331Snwhitehorn * Note that there is at least one character in 282276331Snwhitehorn * the buffer, so conversions that do not set NOSKIP 283276331Snwhitehorn * can no longer result in an input failure. 284276331Snwhitehorn */ 285276331Snwhitehorn } 286276331Snwhitehorn 287276331Snwhitehorn /* 288276331Snwhitehorn * Do the conversion. 289276331Snwhitehorn */ 290276506Snwhitehorn switch (c) { 291276506Snwhitehorn 292276506Snwhitehorn case CT_CHAR: 293276506Snwhitehorn /* scan arbitrary characters (sets NOSKIP) */ 294276506Snwhitehorn if (width == 0) 295276331Snwhitehorn width = 1; 296276331Snwhitehorn if (flags & SUPPRESS) { 297276331Snwhitehorn size_t sum = 0; 298276331Snwhitehorn for (;;) { 299276331Snwhitehorn if ((n = inr) < width) { 300276331Snwhitehorn sum += n; 301276331Snwhitehorn width -= n; 302276331Snwhitehorn inp += n; 303276331Snwhitehorn if (sum == 0) 304276331Snwhitehorn goto input_failure; 305276506Snwhitehorn break; 306276506Snwhitehorn } else { 307276331Snwhitehorn sum += width; 308276331Snwhitehorn inr -= width; 309276331Snwhitehorn inp += width; 310276331Snwhitehorn break; 311276506Snwhitehorn } 312276331Snwhitehorn } 313276331Snwhitehorn nread += sum; 314276331Snwhitehorn } else { 315276331Snwhitehorn bcopy(inp, va_arg(ap, char *), width); 316276331Snwhitehorn inr -= width; 317276331Snwhitehorn inp += width; 318276331Snwhitehorn nread += width; 319276331Snwhitehorn nassigned++; 320276331Snwhitehorn } 321276331Snwhitehorn nconversions++; 322276331Snwhitehorn break; 323276331Snwhitehorn 324276331Snwhitehorn case CT_CCL: 325276331Snwhitehorn /* scan a (nonempty) character class (sets NOSKIP) */ 326276331Snwhitehorn if (width == 0) 327276331Snwhitehorn width = (size_t)~0; /* `infinity' */ 328276331Snwhitehorn /* take only those things in the class */ 329276331Snwhitehorn if (flags & SUPPRESS) { 330276331Snwhitehorn n = 0; 331276331Snwhitehorn while (ccltab[(unsigned char)*inp]) { 332276331Snwhitehorn n++, inr--, inp++; 333276506Snwhitehorn if (--width == 0) 334276331Snwhitehorn break; 335276506Snwhitehorn if (inr <= 0) { 336276331Snwhitehorn if (n == 0) 337276331Snwhitehorn goto input_failure; 338276331Snwhitehorn break; 339276506Snwhitehorn } 340276331Snwhitehorn } 341276506Snwhitehorn if (n == 0) 342276331Snwhitehorn goto match_failure; 343276331Snwhitehorn } else { 344 p0 = p = va_arg(ap, char *); 345 while (ccltab[(unsigned char)*inp]) { 346 inr--; 347 *p++ = *inp++; 348 if (--width == 0) 349 break; 350 if (inr <= 0) { 351 if (p == p0) 352 goto input_failure; 353 break; 354 } 355 } 356 n = p - p0; 357 if (n == 0) 358 goto match_failure; 359 *p = 0; 360 nassigned++; 361 } 362 nread += n; 363 nconversions++; 364 break; 365 366 case CT_STRING: 367 /* like CCL, but zero-length string OK, & no NOSKIP */ 368 if (width == 0) 369 width = (size_t)~0; 370 if (flags & SUPPRESS) { 371 n = 0; 372 while (!isspace(*inp)) { 373 n++, inr--, inp++; 374 if (--width == 0) 375 break; 376 if (inr <= 0) 377 break; 378 } 379 nread += n; 380 } else { 381 p0 = p = va_arg(ap, char *); 382 while (!isspace(*inp)) { 383 inr--; 384 *p++ = *inp++; 385 if (--width == 0) 386 break; 387 if (inr <= 0) 388 break; 389 } 390 *p = 0; 391 nread += p - p0; 392 nassigned++; 393 } 394 nconversions++; 395 continue; 396 397 case CT_INT: 398 /* scan an integer as if by strtoq/strtouq */ 399#ifdef hardway 400 if (width == 0 || width > sizeof(buf) - 1) 401 width = sizeof(buf) - 1; 402#else 403 /* size_t is unsigned, hence this optimisation */ 404 if (--width > sizeof(buf) - 2) 405 width = sizeof(buf) - 2; 406 width++; 407#endif 408 flags |= SIGNOK | NDIGITS | NZDIGITS; 409 for (p = buf; width; width--) { 410 c = *inp; 411 /* 412 * Switch on the character; `goto ok' 413 * if we accept it as a part of number. 414 */ 415 switch (c) { 416 417 /* 418 * The digit 0 is always legal, but is 419 * special. For %i conversions, if no 420 * digits (zero or nonzero) have been 421 * scanned (only signs), we will have 422 * base==0. In that case, we should set 423 * it to 8 and enable 0x prefixing. 424 * Also, if we have not scanned zero digits 425 * before this, do not turn off prefixing 426 * (someone else will turn it off if we 427 * have scanned any nonzero digits). 428 */ 429 case '0': 430 if (base == 0) { 431 base = 8; 432 flags |= PFXOK; 433 } 434 if (flags & NZDIGITS) 435 flags &= ~(SIGNOK|NZDIGITS|NDIGITS); 436 else 437 flags &= ~(SIGNOK|PFXOK|NDIGITS); 438 goto ok; 439 440 /* 1 through 7 always legal */ 441 case '1': case '2': case '3': 442 case '4': case '5': case '6': case '7': 443 base = basefix[base]; 444 flags &= ~(SIGNOK | PFXOK | NDIGITS); 445 goto ok; 446 447 /* digits 8 and 9 ok iff decimal or hex */ 448 case '8': case '9': 449 base = basefix[base]; 450 if (base <= 8) 451 break; /* not legal here */ 452 flags &= ~(SIGNOK | PFXOK | NDIGITS); 453 goto ok; 454 455 /* letters ok iff hex */ 456 case 'A': case 'B': case 'C': 457 case 'D': case 'E': case 'F': 458 case 'a': case 'b': case 'c': 459 case 'd': case 'e': case 'f': 460 /* no need to fix base here */ 461 if (base <= 10) 462 break; /* not legal here */ 463 flags &= ~(SIGNOK | PFXOK | NDIGITS); 464 goto ok; 465 466 /* sign ok only as first character */ 467 case '+': case '-': 468 if (flags & SIGNOK) { 469 flags &= ~SIGNOK; 470 goto ok; 471 } 472 break; 473 474 /* x ok iff flag still set & 2nd char */ 475 case 'x': case 'X': 476 if (flags & PFXOK && p == buf + 1) { 477 base = 16; /* if %i */ 478 flags &= ~PFXOK; 479 goto ok; 480 } 481 break; 482 } 483 484 /* 485 * If we got here, c is not a legal character 486 * for a number. Stop accumulating digits. 487 */ 488 break; 489 ok: 490 /* 491 * c is legal: store it and look at the next. 492 */ 493 *p++ = c; 494 if (--inr > 0) 495 inp++; 496 else 497 break; /* end of input */ 498 } 499 /* 500 * If we had only a sign, it is no good; push 501 * back the sign. If the number ends in `x', 502 * it was [sign] '0' 'x', so push back the x 503 * and treat it as [sign] '0'. 504 */ 505 if (flags & NDIGITS) { 506 if (p > buf) { 507 inp--; 508 inr++; 509 } 510 goto match_failure; 511 } 512 c = ((u_char *)p)[-1]; 513 if (c == 'x' || c == 'X') { 514 --p; 515 inp--; 516 inr++; 517 } 518 if ((flags & SUPPRESS) == 0) { 519 u_quad_t res; 520 521 *p = 0; 522 res = (*ccfn)(buf, (char **)NULL, base); 523 if (flags & POINTER) 524 *va_arg(ap, void **) = 525 (void *)(uintptr_t)res; 526 else if (flags & SHORTSHORT) 527 *va_arg(ap, char *) = res; 528 else if (flags & SHORT) 529 *va_arg(ap, short *) = res; 530 else if (flags & LONG) 531 *va_arg(ap, long *) = res; 532 else if (flags & QUAD) 533 *va_arg(ap, quad_t *) = res; 534 else 535 *va_arg(ap, int *) = res; 536 nassigned++; 537 } 538 nread += p - buf; 539 nconversions++; 540 break; 541 542 } 543 } 544input_failure: 545 return (nconversions != 0 ? nassigned : -1); 546match_failure: 547 return (nassigned); 548} 549 550/* 551 * Fill in the given table from the scanset at the given format 552 * (just after `['). Return a pointer to the character past the 553 * closing `]'. The table has a 1 wherever characters should be 554 * considered part of the scanset. 555 */ 556static const u_char * 557__sccl(char *tab, const u_char *fmt) 558{ 559 int c, n, v; 560 561 /* first `clear' the whole table */ 562 c = *fmt++; /* first char hat => negated scanset */ 563 if (c == '^') { 564 v = 1; /* default => accept */ 565 c = *fmt++; /* get new first char */ 566 } else 567 v = 0; /* default => reject */ 568 569 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */ 570 for (n = 0; n < 256; n++) 571 tab[n] = v; /* memset(tab, v, 256) */ 572 573 if (c == 0) 574 return (fmt - 1);/* format ended before closing ] */ 575 576 /* 577 * Now set the entries corresponding to the actual scanset 578 * to the opposite of the above. 579 * 580 * The first character may be ']' (or '-') without being special; 581 * the last character may be '-'. 582 */ 583 v = 1 - v; 584 for (;;) { 585 tab[c] = v; /* take character c */ 586doswitch: 587 n = *fmt++; /* and examine the next */ 588 switch (n) { 589 590 case 0: /* format ended too soon */ 591 return (fmt - 1); 592 593 case '-': 594 /* 595 * A scanset of the form 596 * [01+-] 597 * is defined as `the digit 0, the digit 1, 598 * the character +, the character -', but 599 * the effect of a scanset such as 600 * [a-zA-Z0-9] 601 * is implementation defined. The V7 Unix 602 * scanf treats `a-z' as `the letters a through 603 * z', but treats `a-a' as `the letter a, the 604 * character -, and the letter a'. 605 * 606 * For compatibility, the `-' is not considerd 607 * to define a range if the character following 608 * it is either a close bracket (required by ANSI) 609 * or is not numerically greater than the character 610 * we just stored in the table (c). 611 */ 612 n = *fmt; 613 if (n == ']' || n < c) { 614 c = '-'; 615 break; /* resume the for(;;) */ 616 } 617 fmt++; 618 /* fill in the range */ 619 do { 620 tab[++c] = v; 621 } while (c < n); 622 c = n; 623 /* 624 * Alas, the V7 Unix scanf also treats formats 625 * such as [a-c-e] as `the letters a through e'. 626 * This too is permitted by the standard.... 627 */ 628 goto doswitch; 629 break; 630 631 case ']': /* end of scanset */ 632 return (fmt); 633 634 default: /* just another character */ 635 c = n; 636 break; 637 } 638 } 639 /* NOTREACHED */ 640} 641 642