sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 7c251bcd4fe460d8c6a5bfddeca11db9296d3c7d
parent 70ba7a6e623a87723e61eb2899d3d92800336022
Author: Connor Lane Smith <cls@lubutu.com>
Date:   Fri, 10 Jun 2011 05:41:40 +0100

refactor strnum
Diffstat:
MMakefile | 1+
Mdate.c | 6++----
Mfold.c | 6++----
Mhead.c | 6++----
Mkill.c | 14+++-----------
Mnl.c | 6++----
Msleep.c | 5+----
Mtail.c | 6++----
Mtouch.c | 6++----
Mutil.h | 1+
10 files changed, 18 insertions(+), 39 deletions(-)

diff --git a/Makefile b/Makefile @@ -9,6 +9,7 @@ LIB = \ util/eprintf.o \ util/putword.o \ util/recurse.o \ + util/strnum.o \ SRC = \ basename.c \ diff --git a/date.c b/date.c @@ -9,7 +9,7 @@ int main(int argc, char *argv[]) { char buf[BUFSIZ], c; - char *end, *fmt = "%c"; + char *fmt = "%c"; struct tm *now = NULL; time_t t; @@ -17,9 +17,7 @@ main(int argc, char *argv[]) while((c = getopt(argc, argv, "d:")) != -1) switch(c) { case 'd': - t = strtol(optarg, &end, 0); - if(*end != '\0') - eprintf("%s: not a number\n", optarg); + t = strnum(optarg, 0); break; default: exit(EXIT_FAILURE); diff --git a/fold.c b/fold.c @@ -16,7 +16,7 @@ static bool sflag = false; int main(int argc, char *argv[]) { - char c, *end; + char c; long width = 80; FILE *fp; @@ -29,9 +29,7 @@ main(int argc, char *argv[]) sflag = true; break; case 'w': - width = strtol(optarg, &end, 0); - if(*end != '\0') - eprintf("%s: not a number\n", optarg); + width = strnum(optarg, 0); break; default: exit(EXIT_FAILURE); diff --git a/head.c b/head.c @@ -10,16 +10,14 @@ static void head(FILE *, const char *, long); int main(int argc, char *argv[]) { - char *end, c; + char c; long n = 10; FILE *fp; while((c = getopt(argc, argv, "n:")) != -1) switch(c) { case 'n': - n = strtol(optarg, &end, 0); - if(*end != '\0') - eprintf("%s: not a number\n", optarg); + n = strnum(optarg, 0); break; default: exit(EXIT_FAILURE); diff --git a/kill.c b/kill.c @@ -49,26 +49,18 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } if(lflag) { - if(optind == argc-1) { - sig = strtol(argv[optind], &end, 0); - if(*end != '\0') - eprintf("%s: not a number\n", argv[optind]); - } - else if(optind == argc) - sig = 0; - else + if(optind < argc-1) eprintf("usage: %s [-s signal] [pid...]\n" " %s -l [signum]\n", argv[0], argv[0]); + sig = (optind == argc) ? 0 : strnum(argv[optind], 0); for(i = 0; i < LEN(sigs); i++) if(sigs[i].sig == sig || sig == 0) putword(sigs[i].name); putchar('\n'); } else for(; optind < argc; optind++) { - pid = strtol(argv[optind], &end, 0); - if(*end != '\0') - eprintf("%s: not a number\n", argv[optind]); + pid = strnum(argv[optind], 0); if(kill(pid, sig) == -1) eprintf("kill %d:", pid); } diff --git a/nl.c b/nl.c @@ -17,7 +17,7 @@ static regex_t preg; int main(int argc, char *argv[]) { - char c, *end; + char c; FILE *fp; while((c = getopt(argc, argv, "b:i:s:")) != -1) @@ -30,9 +30,7 @@ main(int argc, char *argv[]) eprintf("usage: %s [-b mode] [-i increment] [-s separator] [file...]\n", argv[0]); break; case 'i': - incr = strtol(optarg, &end, 0); - if(*end != '\0') - eprintf("%s: not a number\n", optarg); + incr = strnum(optarg, 0); break; case 's': sep = optarg; diff --git a/sleep.c b/sleep.c @@ -6,7 +6,6 @@ int main(int argc, char *argv[]) { - char *end; unsigned int seconds; if(getopt(argc, argv, "") != -1) @@ -14,9 +13,7 @@ main(int argc, char *argv[]) if(optind != argc-1) eprintf("usage: %s seconds\n", argv[0]); - seconds = strtol(argv[optind], &end, 0); - if(*end != '\0') - eprintf("%s: not a number\n", argv[optind]); + seconds = strnum(argv[optind], 0); while((seconds = sleep(seconds)) > 0) ; return EXIT_SUCCESS; diff --git a/tail.c b/tail.c @@ -12,7 +12,7 @@ static void taketail(FILE *, const char *, long); int main(int argc, char *argv[]) { - char *end, c; + char c; long n = 10; FILE *fp; void (*tail)(FILE *, const char *, long) = taketail; @@ -20,9 +20,7 @@ main(int argc, char *argv[]) while((c = getopt(argc, argv, "n:")) != -1) switch(c) { case 'n': - n = abs(strtol(optarg, &end, 0)); - if(*end != '\0') - eprintf("%s: not a number\n", optarg); + n = abs(strnum(optarg, 0)); if(optarg[0] == '+') tail = dropinit; break; diff --git a/touch.c b/touch.c @@ -17,7 +17,7 @@ static time_t t; int main(int argc, char *argv[]) { - char *end, c; + char c; t = time(NULL); while((c = getopt(argc, argv, "ct:")) != -1) @@ -26,9 +26,7 @@ main(int argc, char *argv[]) cflag = true; break; case 't': - t = strtol(optarg, &end, 0); - if(*end != '\0') - eprintf("%s: not a number\n", optarg); + t = strnum(optarg, 0); break; default: exit(EXIT_FAILURE); diff --git a/util.h b/util.h @@ -7,3 +7,4 @@ void enmasse(int, char **, int (*)(const char *, const char *)); void eprintf(const char *, ...); void putword(const char *); void recurse(const char *, void (*)(const char *)); +long strnum(const char *, int);