xref: /csrg-svn/bin/kill/kill.c (revision 50760)
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*50760Sbostic static char sccsid[] = "@(#)kill.c	5.5 (Berkeley) 08/04/91";
1635204Sbostic #endif /* not lint */
1735204Sbostic 
181033Sbill #include <signal.h>
1950084Sbostic #include <errno.h>
2035204Sbostic #include <stdio.h>
2146654Sbostic #include <stdlib.h>
2246654Sbostic #include <string.h>
231363Sbill #include <ctype.h>
241033Sbill 
2550752Sbostic void nosig __P((char *));
2650752Sbostic void printsig __P((FILE *));
2750752Sbostic void usage __P((void));
281363Sbill 
291033Sbill main(argc, argv)
3035204Sbostic 	int argc;
3135204Sbostic 	char **argv;
321033Sbill {
3350084Sbostic 	register int errors, numsig, pid;
3435204Sbostic 	register char **p;
3550084Sbostic 	char *ep;
361033Sbill 
3735204Sbostic 	if (argc < 2)
3835204Sbostic 		usage();
3935204Sbostic 
4035204Sbostic 	if (!strcmp(*++argv, "-l")) {
4150084Sbostic 		printsig(stdout);
4235204Sbostic 		exit(0);
431033Sbill 	}
4435204Sbostic 
4535204Sbostic 	numsig = SIGTERM;
4635204Sbostic 	if (**argv == '-') {
4735204Sbostic 		++*argv;
4835204Sbostic 		if (isalpha(**argv)) {
4935204Sbostic 			if (!strncasecmp(*argv, "sig", 3))
5035204Sbostic 				*argv += 3;
51*50760Sbostic 			for (numsig = NSIG, p = sys_signame + 1; --numsig; ++p)
5235204Sbostic 				if (!strcasecmp(*p, *argv)) {
5350752Sbostic 					numsig = p - sys_signame;
5435204Sbostic 					break;
5535204Sbostic 				}
56*50760Sbostic 			if (!numsig)
57*50760Sbostic 				nosig(*argv);
5850084Sbostic 		} else if (isdigit(**argv)) {
5950084Sbostic 			numsig = strtol(*argv, &ep, 10);
6050084Sbostic 			if (!*argv || *ep) {
6150084Sbostic 				(void)fprintf(stderr,
6250084Sbostic 				    "kill: illegal signal number %s\n", *argv);
6350084Sbostic 				exit(1);
6450084Sbostic 			}
6535204Sbostic 			if (numsig <= 0 || numsig > NSIG)
6650084Sbostic 				nosig(*argv);
6750084Sbostic 		} else
6850084Sbostic 			nosig(*argv);
6935204Sbostic 		++argv;
7035204Sbostic 	}
7135204Sbostic 
7235204Sbostic 	if (!*argv)
7335204Sbostic 		usage();
7435204Sbostic 
7535204Sbostic 	for (errors = 0; *argv; ++argv) {
7650084Sbostic 		pid = strtol(*argv, &ep, 10);
7750084Sbostic 		if (!*argv || *ep) {
7850084Sbostic 			(void)fprintf(stderr,
7950084Sbostic 			    "kill: illegal process id %s\n", *argv);
8050084Sbostic 			continue;
8150084Sbostic 		}
8250098Sbostic 		if (kill(pid, numsig) == -1) {
8350084Sbostic 			(void)fprintf(stderr,
8450084Sbostic 			    "kill: %s: %s\n", *argv, strerror(errno));
8535204Sbostic 			errors = 1;
861033Sbill 		}
871033Sbill 	}
8835204Sbostic 	exit(errors);
891033Sbill }
9035204Sbostic 
9150752Sbostic void
9250084Sbostic nosig(name)
9350084Sbostic 	char *name;
9435204Sbostic {
9550084Sbostic 	(void)fprintf(stderr,
9650084Sbostic 	    "kill: unknown signal %s; valid signals:\n", name);
9750084Sbostic 	printsig(stderr);
9850084Sbostic 	exit(1);
9950084Sbostic }
10050084Sbostic 
10150752Sbostic void
10250084Sbostic printsig(fp)
10350084Sbostic 	FILE *fp;
10450084Sbostic {
10550752Sbostic 	register int cnt;
10635204Sbostic 	register char **p;
10735204Sbostic 
10850752Sbostic 	for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
10950084Sbostic 		(void)fprintf(fp, "%s ", *p);
11050752Sbostic 		if (cnt == NSIG / 2)
11150084Sbostic 			(void)fprintf(fp, "\n");
11235204Sbostic 	}
11350084Sbostic 	(void)fprintf(fp, "\n");
11435204Sbostic }
11535204Sbostic 
11650752Sbostic void
11735204Sbostic usage()
11835204Sbostic {
11950084Sbostic 	(void)fprintf(stderr, "usage: kill [-l] [-sig] pid ...\n");
12050084Sbostic 	exit(1);
12135204Sbostic }
122