[hackers] [sbase] Add name2sig() and simplify kill(1) || sin

From: <git_AT_suckless.org>
Date: Tue, 11 Nov 2014 17:29:06 +0100

commit e0b0741ba1c533bbcf7e9aef217aa5f26e2cdaa4
Author: sin <sin_AT_2f30.org>
Date: Tue Nov 11 16:27:17 2014 +0000

    Add name2sig() and simplify kill(1)

diff --git a/kill.c b/kill.c
index eb4659a..f792159 100644
--- a/kill.c
+++ b/kill.c
_AT_@ -23,6 +23,7 @@ struct {
 };
 
 const char *sig2name(int);
+int name2sig(const char *);
 
 static void
 usage(void)
_AT_@ -54,14 +55,9 @@ main(int argc, char *argv[])
         } else if (argv[0][0] == '-' && isdigit(argv[0][1])) {
                 /* handle XSI extension -signum */
                 errno = 0;
- sig = strtol(&argv[0][1], &end, 0);
+ sig = strtol(&argv[0][1], &end, 10);
                 if (*end != '\0' || errno != 0)
- eprintf("%d: bad signal number\n", sig);
- for (i = 0; i < LEN(sigs); i++)
- if (sigs[i].sig == sig || sig == 0)
- break;
- if (i == LEN(sigs))
- eprintf("%d: bad signal number\n", sig);
+ eprintf("%s: bad signal number\n", &argv[0][1]);
                 argc--;
                 argv++;
         } else if (strcmp(argv[0], "-l") == 0) {
_AT_@ -74,16 +70,10 @@ main(int argc, char *argv[])
                 } else if (argc > 1)
                         usage();
                 errno = 0;
- sig = strtol(argv[0], &end, 0);
- if (*end == '\0' && errno == 0) {
- name = sig2name(sig);
- if (!name)
- eprintf("%d: bad signal number\n", sig);
- else
- puts(name);
- } else {
- eprintf("%s: bad signal name\n", argv[0]);
- }
+ sig = strtol(argv[0], &end, 10);
+ if (*end != '\0' || errno != 0)
+ eprintf("%s: bad signal number\n", argv[0]);
+ puts(sig2name(sig));
                 exit(0);
         } else {
                 if (strcmp(argv[0], "-s") == 0) {
_AT_@ -96,18 +86,7 @@ main(int argc, char *argv[])
                         /* assume XSI extension -signame */
                         name = &argv[0][1];
                 }
- if (strcmp(name, "0") == 0) {
- sig = 0;
- } else {
- for (i = 0; i < LEN(sigs); i++) {
- if (strcasecmp(sigs[i].name, name) == 0) {
- sig = sigs[i].sig;
- break;
- }
- }
- if (i == LEN(sigs))
- eprintf("%s: bad signal name\n", name);
- }
+ sig = strcmp(name, "0") == 0 ? 0 : name2sig(name);
                 argc--;
                 argv++;
         }
_AT_@ -117,7 +96,7 @@ main(int argc, char *argv[])
 
         for (; argc; argc--, argv++) {
                 errno = 0;
- pid = strtol(argv[0], &end, 0);
+ pid = strtol(argv[0], &end, 10);
                 if (*end == '\0' && errno == 0) {
                         if (kill(pid, sig) < 0) {
                                 weprintf("kill %d:", pid);
_AT_@ -142,5 +121,20 @@ sig2name(int sig)
         for (i = 0; i < LEN(sigs); i++)
                 if (sigs[i].sig == sig)
                         return sigs[i].name;
+ eprintf("%d: bad signal number\n", sig);
+ /* unreachable */
         return NULL;
 }
+
+int
+name2sig(const char *name)
+{
+ size_t i;
+
+ for (i = 0; i < LEN(sigs); i++)
+ if (strcasecmp(sigs[i].name, name) == 0)
+ return sigs[i].sig;
+ eprintf("%s: bad signal name\n", name);
+ /* unreachable */
+ return -1;
+}
Received on Tue Nov 11 2014 - 17:29:06 CET

This archive was generated by hypermail 2.3.0 : Tue Nov 11 2014 - 17:36:10 CET