xref: /csrg-svn/bin/kill/kill.c (revision 42534)
11033Sbill /*
235204Sbostic  * Copyright (c) 1988 Regents of the University of California.
335204Sbostic  * All rights reserved.
435204Sbostic  *
5*42534Sbostic  * %sccs.include.redist.c%
61033Sbill  */
71033Sbill 
835204Sbostic #ifndef lint
935204Sbostic char copyright[] =
1035204Sbostic "@(#) Copyright (c) 1988 Regents of the University of California.\n\
1135204Sbostic  All rights reserved.\n";
1235204Sbostic #endif /* not lint */
1335204Sbostic 
1435204Sbostic #ifndef lint
15*42534Sbostic static char sccsid[] = "@(#)kill.c	4.6 (Berkeley) 05/31/90";
1635204Sbostic #endif /* not lint */
1735204Sbostic 
181033Sbill #include <signal.h>
1935204Sbostic #include <stdio.h>
201363Sbill #include <ctype.h>
211033Sbill 
2235204Sbostic static char *signals[] = {
2335204Sbostic 	"hup", "int", "quit", "ill", "trap", "iot",		/*  1 - 6  */
2435204Sbostic 	"emt", "fpe", "kill", "bus", "segv", "sys",		/*  7 - 12 */
2535204Sbostic 	"pipe", "alrm",  "term", "urg", "stop", "tstp",		/* 13 - 18 */
2635204Sbostic 	"cont", "chld", "ttin", "ttou", "io", "xcpu",		/* 19 - 24 */
2735204Sbostic 	"xfsz", "vtalrm", "prof", "winch", "29", "usr1",	/* 25 - 30 */
2835204Sbostic 	"usr2", NULL,						/* 31 - 32 */
2935204Sbostic 	};
301363Sbill 
311033Sbill main(argc, argv)
3235204Sbostic 	int argc;
3335204Sbostic 	char **argv;
341033Sbill {
3535204Sbostic 	register int numsig;
3635204Sbostic 	register char **p;
3735204Sbostic 	int errors;
381033Sbill 
3935204Sbostic 	if (argc < 2)
4035204Sbostic 		usage();
4135204Sbostic 
4235204Sbostic 	if (!strcmp(*++argv, "-l")) {
4335204Sbostic 		printsig();
4435204Sbostic 		exit(0);
451033Sbill 	}
4635204Sbostic 
4735204Sbostic 	numsig = SIGTERM;
4835204Sbostic 	if (**argv == '-') {
4935204Sbostic 		++*argv;
5035204Sbostic 		if (isalpha(**argv)) {
5135204Sbostic 			if (!strncasecmp(*argv, "sig", 3))
5235204Sbostic 				*argv += 3;
5335204Sbostic 			for (p = signals;; ++p) {
5435204Sbostic 				if (!*p)
5535204Sbostic 					goto error;
5635204Sbostic 				if (!strcasecmp(*p, *argv)) {
5735204Sbostic 					numsig = p - signals + 1;
5835204Sbostic 					break;
5935204Sbostic 				}
601363Sbill 			}
6135204Sbostic 		}
6235204Sbostic 		else if (isdigit(**argv)) {
6335204Sbostic 			numsig = atoi(*argv);
6435204Sbostic 			if (numsig <= 0 || numsig > NSIG)
6535204Sbostic 				goto error;
6635204Sbostic 		}
6735204Sbostic 		else {
6835204Sbostic error:			printf("kill: unknown signal %s; valid signals:\n", *argv);
6935204Sbostic 			printsig();
701363Sbill 			exit(1);
711363Sbill 		}
7235204Sbostic 		++argv;
7335204Sbostic 	}
7435204Sbostic 
7535204Sbostic 	if (!*argv)
7635204Sbostic 		usage();
7735204Sbostic 
7835204Sbostic 	for (errors = 0; *argv; ++argv) {
7935204Sbostic 		if (!isdigit(**argv))
8035204Sbostic 			usage();
8135204Sbostic 		if (kill(atoi(*argv), numsig) == -1) {
8235204Sbostic 			perror(*argv);
8335204Sbostic 			errors = 1;
841033Sbill 		}
851033Sbill 	}
8635204Sbostic 	exit(errors);
871033Sbill }
8835204Sbostic 
8935204Sbostic static
9035204Sbostic printsig()
9135204Sbostic {
9235204Sbostic 	register char **p;
9335204Sbostic 
9435204Sbostic 	for (p = signals; *p; ++p) {
9535204Sbostic 		printf("%s ", *p);
9635204Sbostic 		if ((p - signals) == NSIG / 2 - 1)
9735204Sbostic 			printf("\n");
9835204Sbostic 	}
9935204Sbostic 	printf("\n");
10035204Sbostic }
10135204Sbostic 
10235204Sbostic static
10335204Sbostic usage()
10435204Sbostic {
10535204Sbostic 	printf("usage: kill [-l] [-sig] pid ...\n");
10635204Sbostic 	exit(2);
10735204Sbostic }
108