refactor strnum

This commit is contained in:
Connor Lane Smith 2011-06-10 05:41:40 +01:00
parent 70ba7a6e62
commit 7c251bcd4f
10 changed files with 18 additions and 39 deletions

View File

@ -9,6 +9,7 @@ LIB = \
util/eprintf.o \ util/eprintf.o \
util/putword.o \ util/putword.o \
util/recurse.o \ util/recurse.o \
util/strnum.o \
SRC = \ SRC = \
basename.c \ basename.c \

6
date.c
View File

@ -9,7 +9,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char buf[BUFSIZ], c; char buf[BUFSIZ], c;
char *end, *fmt = "%c"; char *fmt = "%c";
struct tm *now = NULL; struct tm *now = NULL;
time_t t; time_t t;
@ -17,9 +17,7 @@ main(int argc, char *argv[])
while((c = getopt(argc, argv, "d:")) != -1) while((c = getopt(argc, argv, "d:")) != -1)
switch(c) { switch(c) {
case 'd': case 'd':
t = strtol(optarg, &end, 0); t = strnum(optarg, 0);
if(*end != '\0')
eprintf("%s: not a number\n", optarg);
break; break;
default: default:
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

6
fold.c
View File

@ -16,7 +16,7 @@ static bool sflag = false;
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char c, *end; char c;
long width = 80; long width = 80;
FILE *fp; FILE *fp;
@ -29,9 +29,7 @@ main(int argc, char *argv[])
sflag = true; sflag = true;
break; break;
case 'w': case 'w':
width = strtol(optarg, &end, 0); width = strnum(optarg, 0);
if(*end != '\0')
eprintf("%s: not a number\n", optarg);
break; break;
default: default:
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

6
head.c
View File

@ -10,16 +10,14 @@ static void head(FILE *, const char *, long);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *end, c; char c;
long n = 10; long n = 10;
FILE *fp; FILE *fp;
while((c = getopt(argc, argv, "n:")) != -1) while((c = getopt(argc, argv, "n:")) != -1)
switch(c) { switch(c) {
case 'n': case 'n':
n = strtol(optarg, &end, 0); n = strnum(optarg, 0);
if(*end != '\0')
eprintf("%s: not a number\n", optarg);
break; break;
default: default:
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

14
kill.c
View File

@ -49,26 +49,18 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if(lflag) { if(lflag) {
if(optind == argc-1) { 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
eprintf("usage: %s [-s signal] [pid...]\n" eprintf("usage: %s [-s signal] [pid...]\n"
" %s -l [signum]\n", argv[0], argv[0]); " %s -l [signum]\n", argv[0], argv[0]);
sig = (optind == argc) ? 0 : strnum(argv[optind], 0);
for(i = 0; i < LEN(sigs); i++) for(i = 0; i < LEN(sigs); i++)
if(sigs[i].sig == sig || sig == 0) if(sigs[i].sig == sig || sig == 0)
putword(sigs[i].name); putword(sigs[i].name);
putchar('\n'); putchar('\n');
} }
else for(; optind < argc; optind++) { else for(; optind < argc; optind++) {
pid = strtol(argv[optind], &end, 0); pid = strnum(argv[optind], 0);
if(*end != '\0')
eprintf("%s: not a number\n", argv[optind]);
if(kill(pid, sig) == -1) if(kill(pid, sig) == -1)
eprintf("kill %d:", pid); eprintf("kill %d:", pid);
} }

6
nl.c
View File

@ -17,7 +17,7 @@ static regex_t preg;
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char c, *end; char c;
FILE *fp; FILE *fp;
while((c = getopt(argc, argv, "b:i:s:")) != -1) 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]); eprintf("usage: %s [-b mode] [-i increment] [-s separator] [file...]\n", argv[0]);
break; break;
case 'i': case 'i':
incr = strtol(optarg, &end, 0); incr = strnum(optarg, 0);
if(*end != '\0')
eprintf("%s: not a number\n", optarg);
break; break;
case 's': case 's':
sep = optarg; sep = optarg;

View File

@ -6,7 +6,6 @@
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *end;
unsigned int seconds; unsigned int seconds;
if(getopt(argc, argv, "") != -1) if(getopt(argc, argv, "") != -1)
@ -14,9 +13,7 @@ main(int argc, char *argv[])
if(optind != argc-1) if(optind != argc-1)
eprintf("usage: %s seconds\n", argv[0]); eprintf("usage: %s seconds\n", argv[0]);
seconds = strtol(argv[optind], &end, 0); seconds = strnum(argv[optind], 0);
if(*end != '\0')
eprintf("%s: not a number\n", argv[optind]);
while((seconds = sleep(seconds)) > 0) while((seconds = sleep(seconds)) > 0)
; ;
return EXIT_SUCCESS; return EXIT_SUCCESS;

6
tail.c
View File

@ -12,7 +12,7 @@ static void taketail(FILE *, const char *, long);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *end, c; char c;
long n = 10; long n = 10;
FILE *fp; FILE *fp;
void (*tail)(FILE *, const char *, long) = taketail; void (*tail)(FILE *, const char *, long) = taketail;
@ -20,9 +20,7 @@ main(int argc, char *argv[])
while((c = getopt(argc, argv, "n:")) != -1) while((c = getopt(argc, argv, "n:")) != -1)
switch(c) { switch(c) {
case 'n': case 'n':
n = abs(strtol(optarg, &end, 0)); n = abs(strnum(optarg, 0));
if(*end != '\0')
eprintf("%s: not a number\n", optarg);
if(optarg[0] == '+') if(optarg[0] == '+')
tail = dropinit; tail = dropinit;
break; break;

View File

@ -17,7 +17,7 @@ static time_t t;
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *end, c; char c;
t = time(NULL); t = time(NULL);
while((c = getopt(argc, argv, "ct:")) != -1) while((c = getopt(argc, argv, "ct:")) != -1)
@ -26,9 +26,7 @@ main(int argc, char *argv[])
cflag = true; cflag = true;
break; break;
case 't': case 't':
t = strtol(optarg, &end, 0); t = strnum(optarg, 0);
if(*end != '\0')
eprintf("%s: not a number\n", optarg);
break; break;
default: default:
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

1
util.h
View File

@ -7,3 +7,4 @@ void enmasse(int, char **, int (*)(const char *, const char *));
void eprintf(const char *, ...); void eprintf(const char *, ...);
void putword(const char *); void putword(const char *);
void recurse(const char *, void (*)(const char *)); void recurse(const char *, void (*)(const char *));
long strnum(const char *, int);