aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

strtonum.c (2257B)


      1 /* $openbsd: strtonum.c,v 1.7 2013/04/17 18:40:58 tedu exp $ */
      2 
      3 /*
      4  * copyright (c) 2004 ted unangst and todd miller
      5  * all rights reserved
      6  *
      7  * permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE
     18  */
     19 
     20 #include <errno.h>
     21 #include <limits.h>
     22 #include <stdlib.h>
     23 
     24 #include "../util.h"
     25 
     26 #define INVALID  1
     27 #define TOOSMALL 2
     28 #define TOOLARGE 3
     29 
     30 long long
     31 strtonum(const char *numstr, long long minval, long long maxval, const char **errstrp)
     32 {
     33   long long ll    = 0;
     34   int       error = 0;
     35   char     *ep;
     36   struct errval {
     37     const char *errstr;
     38     int         err;
     39   } ev[4] = {
     40       {NULL, 0},
     41       {"invalid", EINVAL},
     42       {"too small", ERANGE},
     43       {"too large", ERANGE},
     44   };
     45 
     46   ev[0].err = errno;
     47   errno     = 0;
     48   if (minval > maxval) {
     49     error = INVALID;
     50   } else {
     51     ll = strtoll(numstr, &ep, 10);
     52     if (numstr == ep || *ep != '\0')
     53       error = INVALID;
     54     else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
     55       error = TOOSMALL;
     56     else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
     57       error = TOOLARGE;
     58   }
     59   if (errstrp != NULL)
     60     *errstrp = ev[error].errstr;
     61   errno = ev[error].err;
     62   if (error)
     63     ll = 0;
     64 
     65   return (ll);
     66 }
     67 
     68 long long
     69 enstrtonum(int status, const char *numstr, long long minval, long long maxval)
     70 {
     71   const char *errstr;
     72   long long   ll;
     73 
     74   ll = strtonum(numstr, minval, maxval, &errstr);
     75   if (errstr)
     76     enprintf(status, "strtonum %s: %s\n", numstr, errstr);
     77   return ll;
     78 }
     79 
     80 long long
     81 estrtonum(const char *numstr, long long minval, long long maxval)
     82 {
     83   return enstrtonum(1, numstr, minval, maxval);
     84 }