Improved sigdwmblocks

This commit is contained in:
Ashish Kumar Yadav 2021-04-04 13:04:23 +05:30
parent 0e65963446
commit 894ca1e544

View file

@ -10,23 +10,26 @@
#define LOCKFILE "/tmp/dwmblocks.pid" #define LOCKFILE "/tmp/dwmblocks.pid"
int int
parsesignal(char *arg, int *s) parsesignal(char *arg)
{ {
int i = 0; int i = 0;
for (; *arg != '\0'; arg++) for (; *arg != '\0'; arg++)
if (*arg >= '0' && *arg <= '9') if (*arg >= '0' && *arg <= '9')
i = 10 * i + *arg - '0'; i = 10 * i + *arg - '0';
else else {
return 0; fputs("Usage: sigdwmblocks <signal> [<sigval>]\n", stderr);
if ((i += SIGRTMIN) > SIGRTMAX) exit(2);
return 0; }
*s = i; if ((i += SIGRTMIN) > SIGRTMAX) {
return 1; fputs("Error: <signal> out of range.\n", stderr);
exit(2);
}
return i;
} }
int int
parsesigval(char *arg, int *v) parsesigval(char *arg)
{ {
int s = 1, i = 0; int s = 1, i = 0;
@ -38,10 +41,11 @@ parsesigval(char *arg, int *v)
for (; *arg != '\0'; arg++) for (; *arg != '\0'; arg++)
if (*arg >= '0' && *arg <= '9') if (*arg >= '0' && *arg <= '9')
i = 10 * i + *arg - '0'; i = 10 * i + *arg - '0';
else else {
return 0; fputs("Usage: sigdwmblocks <signal> [<sigval>]\n", stderr);
*v = s * i; exit(2);
return 1; }
return s * i;
} }
void void
@ -89,16 +93,12 @@ main(int argc, char *argv[])
int sig; int sig;
union sigval sv; union sigval sv;
if (argc > 1 && parsesignal(argv[1], &sig)) { if (argc < 2 || argc > 3) {
if (argc == 2) { fputs("Usage: sigdwmblocks <signal> [<sigval>]\n", stderr);
sv.sival_int = NILL; return 2;
sendsignal(sig, sv);
return 0;
} else if (argc == 3 && parsesigval(argv[2], &(sv.sival_int))) {
sendsignal(sig, sv);
return 0;
}
} }
fprintf(stderr, "Usage: %s <signal> [<sigval>]\n", argv[0]); sig = parsesignal(argv[1]);
return 2; sv.sival_int = argc == 2 ? NILL : parsesigval(argv[2]);
sendsignal(sig, sv);
return 0;
} }