diff --git a/sigdwmblocks.c b/sigdwmblocks.c index 7c07104..3709c14 100644 --- a/sigdwmblocks.c +++ b/sigdwmblocks.c @@ -10,8 +10,42 @@ #define NILL INT_MIN #define LOCKFILE "/tmp/dwmblocks.pid" +int +parsesignal(char *c, int *s) +{ + for (*s = 0; *c != '\0'; c++) + if (*c >= '0' && *c <= '9') { + *s *= 10; + *s += *c - '0'; + } else + return 0; + if ((*s += SIGRTMIN) > SIGRTMAX) + return 0; + return 1; +} + +int +parsesigval(char *c, int *v) +{ + int s = 1; + + if (*c == '-') { + s = -1; + c++; + } else if (*c == '+') + c++; + for (*v = 0; *c != '\0'; c++) + if (*c >= '0' && *c <= '9') { + *v *= 10; + *v += *c - '0'; + } else + return 0; + *v *= s; + return 1; +} + void -sendsignal(int signum, union sigval sv) +sendsignal(int sig, union sigval sv) { int fd; struct flock fl; @@ -37,11 +71,8 @@ sendsignal(int signum, union sigval sv) fputs("Error: no running instance of dwmblocks.\n", stderr); exit(3); } - if (sigqueue(fl.l_pid, signum, sv) == -1) { - if (errno == EINVAL) { - fputs("Error: invalid signal provided in argument.\n", stderr); - exit(2); - } else if (errno == ESRCH) { + if (sigqueue(fl.l_pid, sig, sv) == -1) { + if (errno == ESRCH) { fputs("Error: no running instance of dwmblocks.\n", stderr); exit(3); } else { @@ -54,21 +85,17 @@ sendsignal(int signum, union sigval sv) int main(int argc, char *argv[]) { - if (argc > 1) { - int signal; - union sigval sv; + int sig; + union sigval sv; - if (sscanf(argv[1], "%d", &signal) == 1 && - signal > 0 && (signal += SIGRTMIN) <= SIGRTMAX) { - if (argc == 2) { - sv.sival_int = NILL; - sendsignal(signal, sv); - return 0; - } else if (argc == 3 && - sscanf(argv[2], "%d", &(sv.sival_int)) == 1) { - sendsignal(signal, sv); - return 0; - } + if (argc > 1 && parsesignal(argv[1], &sig)) { + if (argc == 2) { + sv.sival_int = NILL; + sendsignal(sig, sv); + return 0; + } else if (argc == 3 && parsesigval(argv[2], &(sv.sival_int))) { + sendsignal(sig, sv); + return 0; } } fprintf(stderr, "Usage: %s []\n", argv[0]);