[hackers] [sbase] Convert estrto{l, ul} to estrtonum || FRIGN

From: <git_AT_suckless.org>
Date: Fri, 30 Jan 2015 17:48:20 +0100 (CET)

commit d25314dbd07f64f31ae0595acbbf89cf639ca53d
Author: FRIGN <dev_AT_frign.de>
Date: Fri Jan 30 16:52:44 2015 +0100

    Convert estrto{l, ul} to estrtonum
    
    Enough with this insanity!

diff --git a/Makefile b/Makefile
index ff53f08..a056916 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -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
_AT_@ -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
_AT_@ -1,5 +1,6 @@
 /* See LICENSE file for copyright and license details. */
 #include <assert.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
_AT_@ -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
_AT_@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
_AT_@ -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
_AT_@ -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;
_AT_@ -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
_AT_@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
_AT_@ -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
_AT_@ -1,5 +1,6 @@
 /* See LICENSE file for copyright and license details. */
 #include <ctype.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
_AT_@ -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
_AT_@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
_AT_@ -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
_AT_@ -1,27 +0,0 @@
-/* See LICENSE file for copyright and license details. */
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#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
_AT_@ -1,26 +0,0 @@
-/* See LICENSE file for copyright and license details. */
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#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
_AT_@ -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
_AT_@ -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
_AT_@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
+#include <limits.h>
 #include <regex.h>
 #include <stdio.h>
 #include <stdlib.h>
_AT_@ -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
_AT_@ -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
_AT_@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
_AT_@ -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);
 }
_AT_@ -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
_AT_@ -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
_AT_@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
_AT_@ -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
_AT_@ -1,6 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 #include <errno.h>
 #include <fcntl.h>
+#include <limits.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <time.h>
_AT_@ -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
_AT_@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <wchar.h>
_AT_@ -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
_AT_@ -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 *);
Received on Fri Jan 30 2015 - 17:48:20 CET

This archive was generated by hypermail 2.3.0 : Fri Jan 30 2015 - 18:00:12 CET