refactor strnum
This commit is contained in:
parent
70ba7a6e62
commit
7c251bcd4f
1
Makefile
1
Makefile
|
@ -9,6 +9,7 @@ LIB = \
|
|||
util/eprintf.o \
|
||||
util/putword.o \
|
||||
util/recurse.o \
|
||||
util/strnum.o \
|
||||
|
||||
SRC = \
|
||||
basename.c \
|
||||
|
|
6
date.c
6
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);
|
||||
|
|
6
fold.c
6
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);
|
||||
|
|
6
head.c
6
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);
|
||||
|
|
14
kill.c
14
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);
|
||||
}
|
||||
|
|
6
nl.c
6
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;
|
||||
|
|
5
sleep.c
5
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;
|
||||
|
|
6
tail.c
6
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;
|
||||
|
|
6
touch.c
6
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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user