The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_scanf.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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  * $FreeBSD$
   37  * From: Id: vfscanf.c,v 1.13 1998/09/25 12:20:27 obrien Exp 
   38  */
   39 
   40 #include <sys/param.h>
   41 #include <sys/systm.h>
   42 #include <sys/kernel.h>
   43 #include <machine/limits.h>
   44 
   45 /*
   46  * Note that stdarg.h and the ANSI style va_start macro is used for both
   47  * ANSI and traditional C compilers.
   48  */
   49 #include <machine/stdarg.h>
   50 
   51 #define BUF             32      /* Maximum length of numeric string. */
   52 
   53 /*
   54  * Flags used during conversion.
   55  */
   56 #define LONG            0x01    /* l: long or double */
   57 #define SHORT           0x04    /* h: short */
   58 #define SUPPRESS        0x08    /* suppress assignment */
   59 #define POINTER         0x10    /* weird %p pointer (`fake hex') */
   60 #define NOSKIP          0x20    /* do not skip blanks */
   61 #define QUAD            0x400
   62 
   63 /*
   64  * The following are used in numeric conversions only:
   65  * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
   66  * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
   67  */
   68 #define SIGNOK          0x40    /* +/- is (still) legal */
   69 #define NDIGITS         0x80    /* no digits detected */
   70 
   71 #define DPTOK           0x100   /* (float) decimal point is still legal */
   72 #define EXPOK           0x200   /* (float) exponent (e+3, etc) still legal */
   73 
   74 #define PFXOK           0x100   /* 0x prefix is (still) legal */
   75 #define NZDIGITS        0x200   /* no zero digits detected */
   76 
   77 /*
   78  * Conversion types.
   79  */
   80 #define CT_CHAR         0       /* %c conversion */
   81 #define CT_CCL          1       /* %[...] conversion */
   82 #define CT_STRING       2       /* %s conversion */
   83 #define CT_INT          3       /* integer, i.e., strtoq or strtouq */
   84 typedef u_quad_t (*ccfntype)(const char *, char **, int);
   85 
   86 #define isspace(c)      ((c) == ' ' || (c) == '\t' || \
   87                          (c) == '\r' || (c) == '\n')
   88 #define isascii(c)      (((c) & ~0x7f) == 0)
   89 #define isupper(c)      ((c) >= 'A' && (c) <= 'Z')
   90 #define islower(c)      ((c) >= 'a' && (c) <= 'z')
   91 #define isalpha(c)      (isupper(c) || (islower(c)))
   92 #define isdigit(c)      ((c) >= '' && (c) <= '9')
   93 
   94 static u_char *__sccl(char *, u_char *);
   95 
   96 int
   97 sscanf(const char *ibuf, const char *fmt, ...)
   98 {
   99         va_list ap;
  100         int ret;
  101         
  102         va_start(ap, fmt);
  103         ret = vsscanf(ibuf, fmt, ap);
  104         va_end(ap);
  105         return(ret);
  106 }
  107 
  108 int
  109 vsscanf(const char *inp, char const *fmt0, va_list ap)
  110 {
  111         int inr;
  112         u_char *fmt = (u_char *)fmt0;
  113         int c;                  /* character from format, or conversion */
  114         size_t width;           /* field width, or 0 */
  115         char *p;                /* points into all kinds of strings */
  116         int n;                  /* handy integer */
  117         int flags;              /* flags as defined above */
  118         char *p0;               /* saves original value of p when necessary */
  119         int nassigned;          /* number of fields assigned */
  120         int nconversions;       /* number of conversions */
  121         int nread;              /* number of characters consumed from fp */
  122         int base;               /* base argument to strtoq/strtouq */
  123         ccfntype ccfn;          /* conversion function (strtoq/strtouq) */
  124         char ccltab[256];       /* character class table for %[...] */
  125         char buf[BUF];          /* buffer for numeric conversions */
  126 
  127         /* `basefix' is used to avoid `if' tests in the integer scanner */
  128         static short basefix[17] =
  129                 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  130 
  131         inr = strlen(inp);
  132         
  133         nassigned = 0;
  134         nconversions = 0;
  135         nread = 0;
  136         base = 0;               /* XXX just to keep gcc happy */
  137         ccfn = NULL;            /* XXX just to keep gcc happy */
  138         for (;;) {
  139                 c = *fmt++;
  140                 if (c == 0)
  141                         return (nassigned);
  142                 if (isspace(c)) {
  143                         while (inr > 0 && isspace(*inp))
  144                                 nread++, inr--, inp++;
  145                         continue;
  146                 }
  147                 if (c != '%')
  148                         goto literal;
  149                 width = 0;
  150                 flags = 0;
  151                 /*
  152                  * switch on the format.  continue if done;
  153                  * break once format type is derived.
  154                  */
  155 again:          c = *fmt++;
  156                 switch (c) {
  157                 case '%':
  158 literal:
  159                         if (inr <= 0)
  160                                 goto input_failure;
  161                         if (*inp != c)
  162                                 goto match_failure;
  163                         inr--, inp++;
  164                         nread++;
  165                         continue;
  166 
  167                 case '*':
  168                         flags |= SUPPRESS;
  169                         goto again;
  170                 case 'l':
  171                         flags |= LONG;
  172                         goto again;
  173                 case 'q':
  174                         flags |= QUAD;
  175                         goto again;
  176                 case 'h':
  177                         flags |= SHORT;
  178                         goto again;
  179 
  180                 case '': case '1': case '2': case '3': case '4':
  181                 case '5': case '6': case '7': case '8': case '9':
  182                         width = width * 10 + c - '';
  183                         goto again;
  184 
  185                 /*
  186                  * Conversions.
  187                  *
  188                  */
  189                 case 'd':
  190                         c = CT_INT;
  191                         ccfn = (ccfntype)strtoq;
  192                         base = 10;
  193                         break;
  194 
  195                 case 'i':
  196                         c = CT_INT;
  197                         ccfn = (ccfntype)strtoq;
  198                         base = 0;
  199                         break;
  200 
  201                 case 'o':
  202                         c = CT_INT;
  203                         ccfn = strtouq;
  204                         base = 8;
  205                         break;
  206 
  207                 case 'u':
  208                         c = CT_INT;
  209                         ccfn = strtouq;
  210                         base = 10;
  211                         break;
  212 
  213                 case 'x':
  214                         flags |= PFXOK; /* enable 0x prefixing */
  215                         c = CT_INT;
  216                         ccfn = strtouq;
  217                         base = 16;
  218                         break;
  219 
  220                 case 's':
  221                         c = CT_STRING;
  222                         break;
  223 
  224                 case '[':
  225                         fmt = __sccl(ccltab, fmt);
  226                         flags |= NOSKIP;
  227                         c = CT_CCL;
  228                         break;
  229 
  230                 case 'c':
  231                         flags |= NOSKIP;
  232                         c = CT_CHAR;
  233                         break;
  234 
  235                 case 'p':       /* pointer format is like hex */
  236                         flags |= POINTER | PFXOK;
  237                         c = CT_INT;
  238                         ccfn = strtouq;
  239                         base = 16;
  240                         break;
  241 
  242                 case 'n':
  243                         nconversions++;
  244                         if (flags & SUPPRESS)   /* ??? */
  245                                 continue;
  246                         if (flags & SHORT)
  247                                 *va_arg(ap, short *) = nread;
  248                         else if (flags & LONG)
  249                                 *va_arg(ap, long *) = nread;
  250                         else if (flags & QUAD)
  251                                 *va_arg(ap, quad_t *) = nread;
  252                         else
  253                                 *va_arg(ap, int *) = nread;
  254                         continue;
  255                 }
  256 
  257                 /*
  258                  * We have a conversion that requires input.
  259                  */
  260                 if (inr <= 0)
  261                         goto input_failure;
  262 
  263                 /*
  264                  * Consume leading white space, except for formats
  265                  * that suppress this.
  266                  */
  267                 if ((flags & NOSKIP) == 0) {
  268                         while (isspace(*inp)) {
  269                                 nread++;
  270                                 if (--inr > 0)
  271                                         inp++;
  272                                 else 
  273                                         goto input_failure;
  274                         }
  275                         /*
  276                          * Note that there is at least one character in
  277                          * the buffer, so conversions that do not set NOSKIP
  278                          * can no longer result in an input failure.
  279                          */
  280                 }
  281 
  282                 /*
  283                  * Do the conversion.
  284                  */
  285                 switch (c) {
  286 
  287                 case CT_CHAR:
  288                         /* scan arbitrary characters (sets NOSKIP) */
  289                         if (width == 0)
  290                                 width = 1;
  291                         if (flags & SUPPRESS) {
  292                                 size_t sum = 0;
  293                                 for (;;) {
  294                                         if ((n = inr) < width) {
  295                                                 sum += n;
  296                                                 width -= n;
  297                                                 inp += n;
  298                                                 if (sum == 0)
  299                                                         goto input_failure;
  300                                                         break;
  301                                         } else {
  302                                                 sum += width;
  303                                                 inr -= width;
  304                                                 inp += width;
  305                                                 break;
  306                                         }
  307                                 }
  308                                 nread += sum;
  309                         } else {
  310                                 bcopy(inp, va_arg(ap, char *), width);
  311                                 inr -= width;
  312                                 inp += width;
  313                                 nread += width;
  314                                 nassigned++;
  315                         }
  316                         nconversions++;
  317                         break;
  318 
  319                 case CT_CCL:
  320                         /* scan a (nonempty) character class (sets NOSKIP) */
  321                         if (width == 0)
  322                                 width = (size_t)~0;     /* `infinity' */
  323                         /* take only those things in the class */
  324                         if (flags & SUPPRESS) {
  325                                 n = 0;
  326                                 while (ccltab[(unsigned char)*inp]) {
  327                                         n++, inr--, inp++;
  328                                         if (--width == 0)
  329                                                 break;
  330                                         if (inr <= 0) {
  331                                                 if (n == 0)
  332                                                         goto input_failure;
  333                                                 break;
  334                                         }
  335                                 }
  336                                 if (n == 0)
  337                                         goto match_failure;
  338                         } else {
  339                                 p0 = p = va_arg(ap, char *);
  340                                 while (ccltab[(unsigned char)*inp]) {
  341                                         inr--;
  342                                         *p++ = *inp++;
  343                                         if (--width == 0)
  344                                                 break;
  345                                         if (inr <= 0) {
  346                                                 if (p == p0)
  347                                                         goto input_failure;
  348                                                 break;
  349                                         }
  350                                 }
  351                                 n = p - p0;
  352                                 if (n == 0)
  353                                         goto match_failure;
  354                                 *p = 0;
  355                                 nassigned++;
  356                         }
  357                         nread += n;
  358                         nconversions++;
  359                         break;
  360 
  361                 case CT_STRING:
  362                         /* like CCL, but zero-length string OK, & no NOSKIP */
  363                         if (width == 0)
  364                                 width = (size_t)~0;
  365                         if (flags & SUPPRESS) {
  366                                 n = 0;
  367                                 while (!isspace(*inp)) {
  368                                         n++, inr--, inp++;
  369                                         if (--width == 0)
  370                                                 break;
  371                                         if (inr <= 0)
  372                                                 break;
  373                                 }
  374                                 nread += n;
  375                         } else {
  376                                 p0 = p = va_arg(ap, char *);
  377                                 while (!isspace(*inp)) {
  378                                         inr--;
  379                                         *p++ = *inp++;
  380                                         if (--width == 0)
  381                                                 break;
  382                                         if (inr <= 0)
  383                                                 break;
  384                                 }
  385                                 *p = 0;
  386                                 nread += p - p0;
  387                                 nassigned++;
  388                         }
  389                         nconversions++;
  390                         continue;
  391 
  392                 case CT_INT:
  393                         /* scan an integer as if by strtoq/strtouq */
  394 #ifdef hardway
  395                         if (width == 0 || width > sizeof(buf) - 1)
  396                                 width = sizeof(buf) - 1;
  397 #else
  398                         /* size_t is unsigned, hence this optimisation */
  399                         if (--width > sizeof(buf) - 2)
  400                                 width = sizeof(buf) - 2;
  401                         width++;
  402 #endif
  403                         flags |= SIGNOK | NDIGITS | NZDIGITS;
  404                         for (p = buf; width; width--) {
  405                                 c = *inp;
  406                                 /*
  407                                  * Switch on the character; `goto ok'
  408                                  * if we accept it as a part of number.
  409                                  */
  410                                 switch (c) {
  411 
  412                                 /*
  413                                  * The digit 0 is always legal, but is
  414                                  * special.  For %i conversions, if no
  415                                  * digits (zero or nonzero) have been
  416                                  * scanned (only signs), we will have
  417                                  * base==0.  In that case, we should set
  418                                  * it to 8 and enable 0x prefixing.
  419                                  * Also, if we have not scanned zero digits
  420                                  * before this, do not turn off prefixing
  421                                  * (someone else will turn it off if we
  422                                  * have scanned any nonzero digits).
  423                                  */
  424                                 case '':
  425                                         if (base == 0) {
  426                                                 base = 8;
  427                                                 flags |= PFXOK;
  428                                         }
  429                                         if (flags & NZDIGITS)
  430                                             flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
  431                                         else
  432                                             flags &= ~(SIGNOK|PFXOK|NDIGITS);
  433                                         goto ok;
  434 
  435                                 /* 1 through 7 always legal */
  436                                 case '1': case '2': case '3':
  437                                 case '4': case '5': case '6': case '7':
  438                                         base = basefix[base];
  439                                         flags &= ~(SIGNOK | PFXOK | NDIGITS);
  440                                         goto ok;
  441 
  442                                 /* digits 8 and 9 ok iff decimal or hex */
  443                                 case '8': case '9':
  444                                         base = basefix[base];
  445                                         if (base <= 8)
  446                                                 break;  /* not legal here */
  447                                         flags &= ~(SIGNOK | PFXOK | NDIGITS);
  448                                         goto ok;
  449 
  450                                 /* letters ok iff hex */
  451                                 case 'A': case 'B': case 'C':
  452                                 case 'D': case 'E': case 'F':
  453                                 case 'a': case 'b': case 'c':
  454                                 case 'd': case 'e': case 'f':
  455                                         /* no need to fix base here */
  456                                         if (base <= 10)
  457                                                 break;  /* not legal here */
  458                                         flags &= ~(SIGNOK | PFXOK | NDIGITS);
  459                                         goto ok;
  460 
  461                                 /* sign ok only as first character */
  462                                 case '+': case '-':
  463                                         if (flags & SIGNOK) {
  464                                                 flags &= ~SIGNOK;
  465                                                 goto ok;
  466                                         }
  467                                         break;
  468 
  469                                 /* x ok iff flag still set & 2nd char */
  470                                 case 'x': case 'X':
  471                                         if (flags & PFXOK && p == buf + 1) {
  472                                                 base = 16;      /* if %i */
  473                                                 flags &= ~PFXOK;
  474                                                 goto ok;
  475                                         }
  476                                         break;
  477                                 }
  478 
  479                                 /*
  480                                  * If we got here, c is not a legal character
  481                                  * for a number.  Stop accumulating digits.
  482                                  */
  483                                 break;
  484                 ok:
  485                                 /*
  486                                  * c is legal: store it and look at the next.
  487                                  */
  488                                 *p++ = c;
  489                                 if (--inr > 0)
  490                                         inp++;
  491                                 else 
  492                                         break;          /* end of input */
  493                         }
  494                         /*
  495                          * If we had only a sign, it is no good; push
  496                          * back the sign.  If the number ends in `x',
  497                          * it was [sign] '' 'x', so push back the x
  498                          * and treat it as [sign] ''.
  499                          */
  500                         if (flags & NDIGITS) {
  501                                 if (p > buf) {
  502                                         inp--;
  503                                         inr++;
  504                                 }
  505                                 goto match_failure;
  506                         }
  507                         c = ((u_char *)p)[-1];
  508                         if (c == 'x' || c == 'X') {
  509                                 --p;
  510                                 inp--;
  511                                 inr++;
  512                         }
  513                         if ((flags & SUPPRESS) == 0) {
  514                                 u_quad_t res;
  515 
  516                                 *p = 0;
  517                                 res = (*ccfn)(buf, (char **)NULL, base);
  518                                 if (flags & POINTER)
  519                                         *va_arg(ap, void **) =
  520                                                 (void *)(u_long)res;
  521                                 else if (flags & SHORT)
  522                                         *va_arg(ap, short *) = res;
  523                                 else if (flags & LONG)
  524                                         *va_arg(ap, long *) = res;
  525                                 else if (flags & QUAD)
  526                                         *va_arg(ap, quad_t *) = res;
  527                                 else
  528                                         *va_arg(ap, int *) = res;
  529                                 nassigned++;
  530                         }
  531                         nread += p - buf;
  532                         nconversions++;
  533                         break;
  534 
  535                 }
  536         }
  537 input_failure:
  538         return (nconversions != 0 ? nassigned : -1);
  539 match_failure:
  540         return (nassigned);
  541 }
  542 
  543 /*
  544  * Fill in the given table from the scanset at the given format
  545  * (just after `[').  Return a pointer to the character past the
  546  * closing `]'.  The table has a 1 wherever characters should be
  547  * considered part of the scanset.
  548  */
  549 static u_char *
  550 __sccl(char *tab, u_char *fmt)
  551 {
  552         int c, n, v;
  553 
  554         /* first `clear' the whole table */
  555         c = *fmt++;             /* first char hat => negated scanset */
  556         if (c == '^') {
  557                 v = 1;          /* default => accept */
  558                 c = *fmt++;     /* get new first char */
  559         } else
  560                 v = 0;          /* default => reject */
  561 
  562         /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
  563         for (n = 0; n < 256; n++)
  564                      tab[n] = v;        /* memset(tab, v, 256) */
  565 
  566         if (c == 0)
  567                 return (fmt - 1);/* format ended before closing ] */
  568 
  569         /*
  570          * Now set the entries corresponding to the actual scanset
  571          * to the opposite of the above.
  572          *
  573          * The first character may be ']' (or '-') without being special;
  574          * the last character may be '-'.
  575          */
  576         v = 1 - v;
  577         for (;;) {
  578                 tab[c] = v;             /* take character c */
  579 doswitch:
  580                 n = *fmt++;             /* and examine the next */
  581                 switch (n) {
  582 
  583                 case 0:                 /* format ended too soon */
  584                         return (fmt - 1);
  585 
  586                 case '-':
  587                         /*
  588                          * A scanset of the form
  589                          *      [01+-]
  590                          * is defined as `the digit 0, the digit 1,
  591                          * the character +, the character -', but
  592                          * the effect of a scanset such as
  593                          *      [a-zA-Z0-9]
  594                          * is implementation defined.  The V7 Unix
  595                          * scanf treats `a-z' as `the letters a through
  596                          * z', but treats `a-a' as `the letter a, the
  597                          * character -, and the letter a'.
  598                          *
  599                          * For compatibility, the `-' is not considerd
  600                          * to define a range if the character following
  601                          * it is either a close bracket (required by ANSI)
  602                          * or is not numerically greater than the character
  603                          * we just stored in the table (c).
  604                          */
  605                         n = *fmt;
  606                         if (n == ']' || n < c) {
  607                                 c = '-';
  608                                 break;  /* resume the for(;;) */
  609                         }
  610                         fmt++;
  611                         /* fill in the range */
  612                         do {
  613                             tab[++c] = v;
  614                         } while (c < n);
  615                         c = n;
  616                         /*
  617                          * Alas, the V7 Unix scanf also treats formats
  618                          * such as [a-c-e] as `the letters a through e'.
  619                          * This too is permitted by the standard....
  620                          */
  621                         goto doswitch;
  622                         break;
  623 
  624                 case ']':               /* end of scanset */
  625                         return (fmt);
  626 
  627                 default:                /* just another character */
  628                         c = n;
  629                         break;
  630                 }
  631         }
  632         /* NOTREACHED */
  633 }
  634 

Cache object: e2185d1b7d091e720228a6aa5b689f96


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.