tech-userlevel archive

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

Re: strtonum(3) from OpenBSD?

OpenBSD has the strtonum(3) function to reliably convert string values
to an
integer since the 3.6 release.

I don't personally remember the previous thread about it, but the
various immediate problems I see with it:

Instead of returning an error code it returns a string, assuming the
only consumers are humans; also this error string is non-localizable.
Also, although it claims to be safer this is only a utility wrapper
around strtoll(3).

I find a check like

strtonum(str, 0, 128, &errstr);
if (errstr) {

more readable than the somewhat clumsy

          errno = 0;
           lval = strtol(buf, &ep, 10);
           if (buf[0] == '\0' || *ep != '\0')
                   goto not_a_number;
if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
                   goto out_of_range;

construct that is needed with strtol.

That said, if it is needed for portability because many programs expect
it in libc, it might be useful...

Yes, already more than one program apparently use it.  And at least one
more once we have it ;)

Home | Main Index | Thread Index | Old Index