xref: /csrg-svn/bin/kill/kill.c (revision 46654)
11033Sbill /*
235204Sbostic  * Copyright (c) 1988 Regents of the University of California.
335204Sbostic  * All rights reserved.
435204Sbostic  *
542534Sbostic  * %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*46654Sbostic static char sccsid[] = "@(#)kill.c	4.7 (Berkeley) 02/25/91";
1635204Sbostic #endif /* not lint */
1735204Sbostic 
181033Sbill #include <signal.h>
1935204Sbostic #include <stdio.h>
20*46654Sbostic #include <stdlib.h>
21*46654Sbostic #include <string.h>
221363Sbill #include <ctype.h>
231033Sbill 
2435204Sbostic static char *signals[] = {
2535204Sbostic 	"hup", "int", "quit", "ill", "trap", "iot",		/*  1 - 6  */
2635204Sbostic 	"emt", "fpe", "kill", "bus", "segv", "sys",		/*  7 - 12 */
2735204Sbostic 	"pipe", "alrm",  "term", "urg", "stop", "tstp",		/* 13 - 18 */
2835204Sbostic 	"cont", "chld", "ttin", "ttou", "io", "xcpu",		/* 19 - 24 */
2935204Sbostic 	"xfsz", "vtalrm", "prof", "winch", "29", "usr1",	/* 25 - 30 */
3035204Sbostic 	"usr2", NULL,						/* 31 - 32 */
3135204Sbostic 	};
321363Sbill 
331033Sbill main(argc, argv)
3435204Sbostic 	int argc;
3535204Sbostic 	char **argv;
361033Sbill {
3735204Sbostic 	register int numsig;
3835204Sbostic 	register char **p;
3935204Sbostic 	int errors;
401033Sbill 
4135204Sbostic 	if (argc < 2)
4235204Sbostic 		usage();
4335204Sbostic 
4435204Sbostic 	if (!strcmp(*++argv, "-l")) {
4535204Sbostic 		printsig();
4635204Sbostic 		exit(0);
471033Sbill 	}
4835204Sbostic 
4935204Sbostic 	numsig = SIGTERM;
5035204Sbostic 	if (**argv == '-') {
5135204Sbostic 		++*argv;
5235204Sbostic 		if (isalpha(**argv)) {
5335204Sbostic 			if (!strncasecmp(*argv, "sig", 3))
5435204Sbostic 				*argv += 3;
5535204Sbostic 			for (p = signals;; ++p) {
5635204Sbostic 				if (!*p)
5735204Sbostic 					goto error;
5835204Sbostic 				if (!strcasecmp(*p, *argv)) {
5935204Sbostic 					numsig = p - signals + 1;
6035204Sbostic 					break;
6135204Sbostic 				}
621363Sbill 			}
6335204Sbostic 		}
6435204Sbostic 		else if (isdigit(**argv)) {
6535204Sbostic 			numsig = atoi(*argv);
6635204Sbostic 			if (numsig <= 0 || numsig > NSIG)
6735204Sbostic 				goto error;
6835204Sbostic 		}
6935204Sbostic 		else {
7035204Sbostic error:			printf("kill: unknown signal %s; valid signals:\n", *argv);
7135204Sbostic 			printsig();
721363Sbill 			exit(1);
731363Sbill 		}
7435204Sbostic 		++argv;
7535204Sbostic 	}
7635204Sbostic 
7735204Sbostic 	if (!*argv)
7835204Sbostic 		usage();
7935204Sbostic 
8035204Sbostic 	for (errors = 0; *argv; ++argv) {
8135204Sbostic 		if (!isdigit(**argv))
8235204Sbostic 			usage();
8335204Sbostic 		if (kill(atoi(*argv), numsig) == -1) {
8435204Sbostic 			perror(*argv);
8535204Sbostic 			errors = 1;
861033Sbill 		}
871033Sbill 	}
8835204Sbostic 	exit(errors);
891033Sbill }
9035204Sbostic 
9135204Sbostic printsig()
9235204Sbostic {
9335204Sbostic 	register char **p;
9435204Sbostic 
9535204Sbostic 	for (p = signals; *p; ++p) {
9635204Sbostic 		printf("%s ", *p);
9735204Sbostic 		if ((p - signals) == NSIG / 2 - 1)
9835204Sbostic 			printf("\n");
9935204Sbostic 	}
10035204Sbostic 	printf("\n");
10135204Sbostic }
10235204Sbostic 
10335204Sbostic usage()
10435204Sbostic {
10535204Sbostic 	printf("usage: kill [-l] [-sig] pid ...\n");
10635204Sbostic 	exit(2);
10735204Sbostic }
108