From fd562481f3ca0b7cd9b4af69bdb6b232a3011f35 Mon Sep 17 00:00:00 2001 From: FRIGN Date: Fri, 30 Jan 2015 16:52:44 +0100 Subject: [PATCH] Convert estrto{l, ul} to estrtonum Enough with this insanity! --- Makefile | 2 -- arg.h | 2 +- cols.c | 5 ++--- date.c | 3 ++- du.c | 4 ++-- expand.c | 5 ++--- fold.c | 5 +++-- head.c | 3 ++- libutil/estrtol.c | 27 --------------------------- libutil/estrtoul.c | 26 -------------------------- nice.c | 4 ++-- nl.c | 3 ++- renice.c | 2 +- seq.c | 5 +++-- split.c | 4 ++-- tail.c | 3 ++- touch.c | 3 ++- unexpand.c | 3 ++- util.h | 2 -- 19 files changed, 30 insertions(+), 81 deletions(-) delete mode 100644 libutil/estrtol.c delete mode 100644 libutil/estrtoul.c diff --git a/Makefile b/Makefile index ff53f08..a056916 100644 --- a/Makefile +++ b/Makefile @@ -39,8 +39,6 @@ LIBUTILSRC =\ libutil/eprintf.c\ libutil/eregcomp.c\ libutil/estrtod.c\ - libutil/estrtol.c\ - libutil/estrtoul.c\ libutil/fnck.c\ libutil/getlines.c\ libutil/human.c\ diff --git a/arg.h b/arg.h index 4df77a7..f1fbdf3 100644 --- a/arg.h +++ b/arg.h @@ -46,7 +46,7 @@ extern char *argv0; #define ARGC() argc_ -#define ARGNUMF(base) (brk_ = 1, estrtol(argv[0], (base))) +#define ARGNUMF(base) (brk_ = 1, estrtonum(argv[0], 0, INT_MAX)) #define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ ((x), abort(), (char *)0) :\ diff --git a/cols.c b/cols.c index cde9777..f89dfb1 100644 --- a/cols.c +++ b/cols.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include +#include #include #include #include @@ -35,9 +36,7 @@ main(int argc, char *argv[]) ARGBEGIN { case 'c': cflag = 1; - chars = estrtol(EARGF(usage()), 0); - if (chars < 3) - eprintf("%d: too few character columns"); + chars = estrtonum(EARGF(usage()), 3, LONG_MAX); break; default: usage(); diff --git a/date.c b/date.c index 95fa62f..db69091 100644 --- a/date.c +++ b/date.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#include #include #include #include @@ -25,7 +26,7 @@ main(int argc, char *argv[]) t = time(NULL); ARGBEGIN { case 'd': - t = estrtol(EARGF(usage()), 0); + t = estrtonum(EARGF(usage()), 0, LLONG_MAX); break; case 'u': tztime = gmtime; diff --git a/du.c b/du.c index b477da9..b900622 100644 --- a/du.c +++ b/du.c @@ -53,7 +53,7 @@ main(int argc, char *argv[]) break; case 'd': dflag = 1; - depth = estrtol(EARGF(usage()), 0); + depth = estrtonum(EARGF(usage()), 0, LONG_MAX); break; case 's': sflag = 1; @@ -73,7 +73,7 @@ main(int argc, char *argv[]) bsize = getenv("BLOCKSIZE"); if (bsize) - blksize = estrtol(bsize, 0); + blksize = estrtonum(bsize, 0, LONG_MAX); if (kflag) blksize = 1024; diff --git a/expand.c b/expand.c index 3135e33..5147f92 100644 --- a/expand.c +++ b/expand.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#include #include #include #include @@ -21,9 +22,7 @@ parselist(const char *s) if (*p == '\0') eprintf("empty field in tablist\n"); tablist = erealloc(tablist, (i + 1) * sizeof(*tablist)); - tablist[i] = estrtol(p, 10); - if (!tablist[i] || tablist[i] < 0) - eprintf("tab field must be positive\n"); + tablist[i] = estrtonum(p, 1, LLONG_MAX); if (i > 0 && tablist[i - 1] >= tablist[i]) eprintf("tablist must be ascending\n"); } diff --git a/fold.c b/fold.c index 9c4624a..06f09ca 100644 --- a/fold.c +++ b/fold.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include +#include #include #include #include @@ -83,10 +84,10 @@ main(int argc, char *argv[]) sflag = 1; break; case 'w': - width = estrtol(EARGF(usage()), 0); + width = estrtonum(EARGF(usage()), 1, LLONG_MAX); break; ARGNUM: - width = ARGNUMF(0); + width = ARGNUMF(10); break; default: usage(); diff --git a/head.c b/head.c index c47fb41..1a83a7e 100644 --- a/head.c +++ b/head.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#include #include #include #include @@ -40,7 +41,7 @@ main(int argc, char *argv[]) ARGBEGIN { case 'n': - n = estrtol(EARGF(usage()), 0); + n = estrtonum(EARGF(usage()), 0, LONG_MAX); break; ARGNUM: n = ARGNUMF(0); diff --git a/libutil/estrtol.c b/libutil/estrtol.c deleted file mode 100644 index 74c7fb4..0000000 --- a/libutil/estrtol.c +++ /dev/null @@ -1,27 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include -#include -#include - -#include "../util.h" - -long -estrtol(const char *s, int base) -{ - char *end; - long n; - - errno = 0; - n = strtol(s, &end, base); - if (*end != '\0') { - if (base == 0) - eprintf("%s: not an integer\n", s); - else - eprintf("%s: not a base %d integer\n", s, base); - } - if (errno != 0) - eprintf("%s:", s); - - return n; -} - diff --git a/libutil/estrtoul.c b/libutil/estrtoul.c deleted file mode 100644 index b8907be..0000000 --- a/libutil/estrtoul.c +++ /dev/null @@ -1,26 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include -#include -#include - -#include "../util.h" - -unsigned long -estrtoul(const char *s, int base) -{ - char *end; - unsigned long n; - - errno = 0; - n = strtoul(s, &end, base); - if (*end != '\0') { - if (base == 0) - eprintf("%s: not an integer\n", s); - else - eprintf("%s: not a base %d integer\n", s, base); - } - if (errno != 0) - eprintf("%s:", s); - - return n; -} diff --git a/nice.c b/nice.c index c1cc79b..261918f 100644 --- a/nice.c +++ b/nice.c @@ -12,7 +12,7 @@ static void usage(void) { - eprintf("usage: nice [-n inc] cmd [arg ...]\n"); + eprintf("usage: %s [-n inc] cmd [arg ...]\n", argv0); } int @@ -23,7 +23,7 @@ main(int argc, char *argv[]) ARGBEGIN { case 'n': - val = estrtol(EARGF(usage()), 10); + val = estrtonum(EARGF(usage()), PRIO_MIN, PRIO_MAX); break; default: usage(); diff --git a/nl.c b/nl.c index 921d756..9d9bef8 100644 --- a/nl.c +++ b/nl.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#include #include #include #include @@ -37,7 +38,7 @@ main(int argc, char *argv[]) usage(); break; case 'i': - incr = estrtol(EARGF(usage()), 0); + incr = estrtonum(EARGF(usage()), 0, LONG_MAX); break; case 's': sep = EARGF(usage()); diff --git a/renice.c b/renice.c index 1d301c6..53805db 100644 --- a/renice.c +++ b/renice.c @@ -48,7 +48,7 @@ main(int argc, char *argv[]) if (argc == 0 || !adj) usage(); - val = estrtol(adj, 10); + val = estrtonum(adj, PRIO_MIN, PRIO_MAX); for (i = 0; i < argc; i++) { who = -1; if (which == PRIO_USER) { diff --git a/seq.c b/seq.c index bff3af6..917c89a 100644 --- a/seq.c +++ b/seq.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#include #include #include #include @@ -97,7 +98,7 @@ digitsleft(const char *d) if (*d == '+') d++; exp = strpbrk(d, "eE"); - shift = exp ? estrtol(&exp[1], 10) : 0; + shift = exp ? estrtonum(&exp[1], -INT_MAX, INT_MAX) : 0; return MAX(0, strspn(d, "-0123456789") + shift); } @@ -109,7 +110,7 @@ digitsright(const char *d) int shift, after; exp = strpbrk(d, "eE"); - shift = exp ? estrtol(&exp[1], 10) : 0; + shift = exp ? estrtonum(&exp[1], -INT_MAX, INT_MAX) : 0; after = (d = strchr(d, '.')) ? strspn(&d[1], "0123456789") : 0; return MAX(0, after - shift); diff --git a/split.c b/split.c index 1ad7384..b71c795 100644 --- a/split.c +++ b/split.c @@ -63,10 +63,10 @@ main(int argc, char *argv[]) always = 0; tmp = ARGF(); if (tmp) - size = estrtol(tmp, 10); + size = estrtonum(tmp, 0, LLONG_MAX); break; case 'a': - slen = estrtol(EARGF(usage()), 10); + slen = estrtonum(EARGF(usage()), 0, INT_MAX); break; case 'd': base = 10; diff --git a/tail.c b/tail.c index c2bc6eb..494b6af 100644 --- a/tail.c +++ b/tail.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#include #include #include #include @@ -29,7 +30,7 @@ main(int argc, char *argv[]) ARGBEGIN { case 'n': lines = EARGF(usage()); - n = abs(estrtol(lines, 0)); + n = estrtonum(lines, 0, LONG_MAX); if (lines[0] == '+') tail = dropinit; break; diff --git a/touch.c b/touch.c index 41af0b2..753d4f5 100644 --- a/touch.c +++ b/touch.c @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include #include +#include #include #include #include @@ -64,7 +65,7 @@ main(int argc, char *argv[]) mflag = 1; break; case 't': - t = estrtol(EARGF(usage()), 0); + t = estrtonum(EARGF(usage()), 0, LLONG_MAX); break; default: usage(); diff --git a/unexpand.c b/unexpand.c index b4b57a0..839eac6 100644 --- a/unexpand.c +++ b/unexpand.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#include #include #include #include @@ -25,7 +26,7 @@ main(int argc, char *argv[]) ARGBEGIN { case 't': - tabsize = estrtol(EARGF(usage()), 0); + tabsize = estrtonum(EARGF(usage()), 0, INT_MAX); if (tabsize <= 0) eprintf("unexpand: invalid tabsize\n"); /* Fallthrough: -t implies -a */ diff --git a/util.h b/util.h index ca02be9..7db5f1d 100644 --- a/util.h +++ b/util.h @@ -31,8 +31,6 @@ void eprintf(const char *, ...); void weprintf(const char *, ...); double estrtod(const char *); -long estrtol(const char *, int); -unsigned long estrtoul(const char *, int); #undef strcasestr char *strcasestr(const char *, const char *);