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/putword.o \
util/recurse.o \
util/strnum.o \
SRC = \
basename.c \

6
date.c
View File

@ -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);

6
fold.c
View File

@ -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);

6
head.c
View File

@ -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);

14
kill.c
View File

@ -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);
}

6
nl.c
View File

@ -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;

View File

@ -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;

6
tail.c
View File

@ -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;

View File

@ -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);

1
util.h
View File

@ -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);