Improved parser and cleanup - sigdwmblocks.c

This commit is contained in:
Ashish Kumar Yadav 2020-12-08 12:04:12 +05:30
parent 41bc318526
commit b7afa41264

View file

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