Simplify logic in kill(1)

This commit is contained in:
sin 2014-11-11 17:28:27 +00:00
parent e4e47a617e
commit f06ada4592

44
kill.c
View File

@ -37,7 +37,6 @@ int
main(int argc, char *argv[])
{
char *end;
const char *name;
int ret = 0;
int sig = SIGTERM;
pid_t pid;
@ -48,19 +47,7 @@ main(int argc, char *argv[])
usage();
argc--, argv++;
if (strcmp(argv[0], "--") == 0) {
argc--, argv++;
} else if (argv[0][0] == '-' && isdigit(argv[0][1])) {
/* handle XSI extension -signum */
errno = 0;
sig = strtol(&argv[0][1], &end, 10);
if (*end != '\0' || errno != 0)
eprintf("%s: bad signal number\n", &argv[0][1]);
sig2name(sig);
argc--, argv++;
} else if (strcmp(argv[0], "-l") == 0) {
argc--, argv++;
if (argc > 0 && strcmp(argv[0], "--") == 0)
if (strcmp(argv[0], "-l") == 0) {
argc--, argv++;
if (argc == 0) {
for (i = 0; i < LEN(sigs); i++)
@ -76,21 +63,32 @@ main(int argc, char *argv[])
sig = WTERMSIG(sig);
puts(sig2name(sig));
exit(0);
} else {
}
if (strcmp(argv[0], "-s") == 0) {
argc--, argv++;
if (argc > 0 && strcmp(argv[0], "--") == 0)
argc--, argv++;
if (argc == 0)
usage();
name = argv[0];
} else {
/* assume XSI extension -signame */
name = &argv[0][1];
}
sig = strcmp(name, "0") == 0 ? 0 : name2sig(name);
sig = strcmp(argv[0], "0") == 0 ? 0 : name2sig(argv[0]);
argc--, argv++;
} else if (argv[0][0] == '-') {
if (isdigit(argv[0][1])) {
/* handle XSI extension -signum */
errno = 0;
sig = strtol(&argv[0][1], &end, 10);
if (*end != '\0' || errno != 0)
eprintf("%s: bad signal number\n", &argv[0][1]);
sig2name(sig);
argc--, argv++;
} else if (argv[0][1] != '-') {
/* handle XSI extension -signame */
sig = strcmp(&argv[0][1], "0") == 0 ? 0 : name2sig(&argv[0][1]);
argc--, argv++;
}
}
if (argc > 0 && strcmp(argv[0], "--") == 0)
argc--, argv++;
if (argc == 0)
usage();