tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: strtonum(3) from OpenBSD?

On Tue, 23 Jun 2009 16:28:11 +0200
Marc Balmer <> wrote:

> OpenBSD has the strtonum(3) function to reliably convert string values  
> to an
> integer since the 3.6 release.
> I could make good use of this function in NetBSD; has it been  
> discussed before
> whether to add it to NetBSD?
> If there are no particular reasons why it is not in NetBSD, I would  
> like to make
> an effort to port it and mail a diff.

Hi Marc!

Yeah, I also think we could use something like that. Currently I use the 
following in my own stuff to parse a string to int:

static int                      
parseint(const char *str)
        char            *ep;            
        long            lval;
        errno = 0;
        lval = strtol(str, &ep, 10);    
        if (str[0] == '\0' || *ep != '\0')
                errx(1, "not a number");
        if (errno == ERANGE) {
                if (lval >= INT_MAX)
                        errx(1, "out of range, maximum is %d", INT_MAX);
                else if (lval <= INT_MIN)
                        errx(1, "out of range, minimum is %d", INT_MIN);
        return (int) lval;

I guess

  i = (int) strtonum(str, INT_MIN, INT_MAX, &err);

would do the same in fewer lines, and ssh, bioctl and sdiff in our tree are 
already using this function it seems.

Adam Hoka <>
Adam Hoka <>
Adam Hoka <>

Home | Main Index | Thread Index | Old Index