| STRTOL(3) | AerieBSD 1.0 Refernce Manual | STRTOL(3) |
| Function | underflow | overflow |
| strtol(Ta Dv LONG_MIN Ta Dv LONG_MAX); | ||
| strtoll(Ta Dv LLONG_MIN Ta Dv LLONG_MAX); | ||
| strtoimax(Ta Dv INTMAX_MIN Ta Dv INTMAX_MAX); | ||
| strtoq(Ta Dv LLONG_MIN Ta Dv LLONG_MAX); |
char *ep; long lval; \&... errno = 0; lval = strtol(buf, &ep, 10); if (buf[0] == '\e0' || *ep != '\e0') goto not_a_number; if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) goto out_of_range;
This example will accept 12 but not 12foo or 12\en. If trailing whitespace is acceptable, further checks must be done on *ep; alternately, use sscanf(3). If strtol(); is being used instead of atoi(3), error checking is further complicated because the desired return value is an int rather than a long; however, on some architectures integers and long integers are the same size. Thus the following is necessary:
char *ep;
int ival;
long lval;
\&...
errno = 0;
lval = strtol(buf, &ep, 10);
if (buf[0] == '\e0' || *ep != '\e0')
goto not_a_number;
if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
(lval > INT_MAX || lval < INT_MIN))
goto out_of_range;
ival = lval;
| AerieBSD 1.0 Reference Manual | August 26 2008 | STRTOL(3) |