kill: cleanup

This commit is contained in:
K. Lange 2018-10-08 10:23:26 +09:00
parent 9c30e8263d
commit c1c5e7ccf4

View File

@ -12,6 +12,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <ctype.h>
#include <errno.h>
struct sig_def { struct sig_def {
int sig; int sig;
@ -73,53 +75,57 @@ void usage(char * argv[]) {
int main(int argc, char * argv[]) { int main(int argc, char * argv[]) {
int signum = SIGKILL; int signum = SIGKILL;
int pid = 0; int pid = 0;
int i = 1;
if (argc < 2) { if (argc < 2) {
usage(argv); usage(argv);
return 1; return 1;
} }
if (argc > 2) { if (argv[1][0] == '-') {
if (argv[1][0] == '-') { signum = -1;
signum = -1; if (strlen(argv[1]+1) > 3 && strstr(argv[1]+1,"SIG") == (argv[1]+1)) {
if (strlen(argv[1]+1) > 3 && strstr(argv[1]+1,"SIG") == (argv[1]+1)) { struct sig_def * s = signals;
while (s->name) {
if (!strcmp(argv[1]+4,s->name)) {
signum = s->sig;
break;
}
s++;
}
} else {
if (!isdigit(argv[1][1] < '0')) {
struct sig_def * s = signals; struct sig_def * s = signals;
while (s->name) { while (s->name) {
if (!strcmp(argv[1]+4,s->name)) { if (!strcmp(argv[1]+1,s->name)) {
signum = s->sig; signum = s->sig;
break; break;
} }
s++; s++;
} }
} else { } else {
if (argv[1][1] < '0' || argv[1][1] > '9') { signum = atoi(argv[1]+1);
struct sig_def * s = signals;
while (s->name) {
if (!strcmp(argv[1]+1,s->name)) {
signum = s->sig;
break;
}
s++;
}
} else {
signum = atoi(argv[1]+1);
}
} }
if (signum == -1) { }
fprintf(stderr,"%s: %s: invalid signal specification\n",argv[0],argv[1]+1); if (signum == -1) {
return 1; fprintf(stderr,"%s: %s: invalid signal specification\n",argv[0],argv[1]+1);
}
} else {
usage(argv);
return 1; return 1;
} }
pid = atoi(argv[2]); i++;
} else if (argc == 2) {
pid = atoi(argv[1]);
} }
if (pid) { if (i < argc) {
kill(pid, signum); pid = atoi(argv[i]);
if (pid) {
if (kill(pid, signum) < 0) {
fprintf(stderr, "%s: kill: %s\n", argv[0], strerror(errno));
}
} else {
fprintf(stderr, "%s: invalid pid\n", argv[0]);
}
} else {
usage(argv);
return 1;
} }
return 0; return 0;