[hackers] [sbase] refactor strnum || Connor Lane Smith

From: <hg_AT_suckless.org>
Date: Fri, 10 Jun 2011 06:41:47 +0200 (CEST)

changeset: 71:1227130bdeae
tag: tip
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Fri Jun 10 05:41:40 2011 +0100
files: Makefile date.c fold.c head.c kill.c nl.c sleep.c tail.c touch.c util.h
description:
refactor strnum

diff -r d0c99f546aef -r 1227130bdeae Makefile
--- a/Makefile Fri Jun 10 04:36:40 2011 +0100
+++ b/Makefile Fri Jun 10 05:41:40 2011 +0100
@@ -9,6 +9,7 @@
         util/eprintf.o \
         util/putword.o \
         util/recurse.o \
+ util/strnum.o \
 
 SRC = \
         basename.c \
diff -r d0c99f546aef -r 1227130bdeae date.c
--- a/date.c Fri Jun 10 04:36:40 2011 +0100
+++ b/date.c Fri Jun 10 05:41:40 2011 +0100
@@ -9,7 +9,7 @@
 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 @@
         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 -r d0c99f546aef -r 1227130bdeae fold.c
--- a/fold.c Fri Jun 10 04:36:40 2011 +0100
+++ b/fold.c Fri Jun 10 05:41:40 2011 +0100
@@ -16,7 +16,7 @@
 int
 main(int argc, char *argv[])
 {
- char c, *end;
+ char c;
         long width = 80;
         FILE *fp;
 
@@ -29,9 +29,7 @@
                         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 -r d0c99f546aef -r 1227130bdeae head.c
--- a/head.c Fri Jun 10 04:36:40 2011 +0100
+++ b/head.c Fri Jun 10 05:41:40 2011 +0100
@@ -10,16 +10,14 @@
 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 -r d0c99f546aef -r 1227130bdeae kill.c
--- a/kill.c Fri Jun 10 04:36:40 2011 +0100
+++ b/kill.c Fri Jun 10 05:41:40 2011 +0100
@@ -49,26 +49,18 @@
                         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 -r d0c99f546aef -r 1227130bdeae nl.c
--- a/nl.c Fri Jun 10 04:36:40 2011 +0100
+++ b/nl.c Fri Jun 10 05:41:40 2011 +0100
@@ -17,7 +17,7 @@
 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 @@
                                 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 -r d0c99f546aef -r 1227130bdeae sleep.c
--- a/sleep.c Fri Jun 10 04:36:40 2011 +0100
+++ b/sleep.c Fri Jun 10 05:41:40 2011 +0100
@@ -6,7 +6,6 @@
 int
 main(int argc, char *argv[])
 {
- char *end;
         unsigned int seconds;
 
         if(getopt(argc, argv, "") != -1)
@@ -14,9 +13,7 @@
         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 -r d0c99f546aef -r 1227130bdeae tail.c
--- a/tail.c Fri Jun 10 04:36:40 2011 +0100
+++ b/tail.c Fri Jun 10 05:41:40 2011 +0100
@@ -12,7 +12,7 @@
 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 @@
         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 -r d0c99f546aef -r 1227130bdeae touch.c
--- a/touch.c Fri Jun 10 04:36:40 2011 +0100
+++ b/touch.c Fri Jun 10 05:41:40 2011 +0100
@@ -17,7 +17,7 @@
 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 @@
                         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 -r d0c99f546aef -r 1227130bdeae util.h
--- a/util.h Fri Jun 10 04:36:40 2011 +0100
+++ b/util.h Fri Jun 10 05:41:40 2011 +0100
@@ -7,3 +7,4 @@
 void eprintf(const char *, ...);
 void putword(const char *);
 void recurse(const char *, void (*)(const char *));
+long strnum(const char *, int);
Received on Fri Jun 10 2011 - 06:41:47 CEST

This archive was generated by hypermail 2.2.0 : Fri Jun 10 2011 - 06:48:05 CEST