Mercurial > hg > toybox
diff lib/lib.c @ 1638:184c98250cc5 draft
strtol() doesn't return error indicator for overflow, it just sets errno. So add estrtol() (which clears errno first), and xstrtol() (which error_exit()s on overflow).
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 01 Jan 2015 16:28:51 -0600 |
parents | 45fb262230c9 |
children | 10922d83392a |
line wrap: on
line diff
--- a/lib/lib.c Thu Jan 01 16:19:40 2015 -0600 +++ b/lib/lib.c Thu Jan 01 16:28:51 2015 -0600 @@ -233,13 +233,30 @@ return rlist; } +long estrtol(char *str, char **end, int base) +{ + errno = 0; + + return strtol(str, end, base); +} + +long xstrtol(char *str, char **end, int base) +{ + long l = estrtol(str, end, base); + + if (errno) perror_exit("%s", str); + + return l; +} + // atol() with the kilo/mega/giga/tera/peta/exa extensions. // (zetta and yotta don't fit in 64 bits.) long atolx(char *numstr) { char *c, *suffixes="cbkmgtpe", *end; - long val = strtol(numstr, &c, 0); + long val; + val = xstrtol(numstr, &c, 0); if (*c) { if (c != numstr && (end = strchr(suffixes, tolower(*c)))) { int shift = end-suffixes-2; @@ -685,8 +702,9 @@ if (pidstr) { char *s; - i = strtol(pidstr, &s, 10); - if (!*s) return i; + + i = estrtol(pidstr, &s, 10); + if (!errno && !*s) return i; if (!strncasecmp(pidstr, "sig", 3)) pidstr+=3; } @@ -715,8 +733,8 @@ // Handle octal mode if (isdigit(*str)) { - mode = strtol(str, &s, 8); - if (*s || (mode & ~(07777))) goto barf; + mode = estrtol(str, &s, 8); + if (errno || *s || (mode & ~(07777))) goto barf; return mode | extrabits; }