xref: /csrg-svn/bin/kill/kill.c (revision 50752)
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*50752Sbostic static char sccsid[] = "@(#)kill.c	5.4 (Berkeley) 08/02/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 
25*50752Sbostic void nosig __P((char *));
26*50752Sbostic void printsig __P((FILE *));
27*50752Sbostic 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*50752Sbostic 			for (p = sys_signame;; ++p) {
5235204Sbostic 				if (!*p)
5350084Sbostic 					nosig(*argv);
5435204Sbostic 				if (!strcasecmp(*p, *argv)) {
55*50752Sbostic 					numsig = p - sys_signame;
5635204Sbostic 					break;
5735204Sbostic 				}
581363Sbill 			}
5950084Sbostic 		} else if (isdigit(**argv)) {
6050084Sbostic 			numsig = strtol(*argv, &ep, 10);
6150084Sbostic 			if (!*argv || *ep) {
6250084Sbostic 				(void)fprintf(stderr,
6350084Sbostic 				    "kill: illegal signal number %s\n", *argv);
6450084Sbostic 				exit(1);
6550084Sbostic 			}
6635204Sbostic 			if (numsig <= 0 || numsig > NSIG)
6750084Sbostic 				nosig(*argv);
6850084Sbostic 		} else
6950084Sbostic 			nosig(*argv);
7035204Sbostic 		++argv;
7135204Sbostic 	}
7235204Sbostic 
7335204Sbostic 	if (!*argv)
7435204Sbostic 		usage();
7535204Sbostic 
7635204Sbostic 	for (errors = 0; *argv; ++argv) {
7750084Sbostic 		pid = strtol(*argv, &ep, 10);
7850084Sbostic 		if (!*argv || *ep) {
7950084Sbostic 			(void)fprintf(stderr,
8050084Sbostic 			    "kill: illegal process id %s\n", *argv);
8150084Sbostic 			continue;
8250084Sbostic 		}
8350098Sbostic 		if (kill(pid, numsig) == -1) {
8450084Sbostic 			(void)fprintf(stderr,
8550084Sbostic 			    "kill: %s: %s\n", *argv, strerror(errno));
8635204Sbostic 			errors = 1;
871033Sbill 		}
881033Sbill 	}
8935204Sbostic 	exit(errors);
901033Sbill }
9135204Sbostic 
92*50752Sbostic void
9350084Sbostic nosig(name)
9450084Sbostic 	char *name;
9535204Sbostic {
9650084Sbostic 	(void)fprintf(stderr,
9750084Sbostic 	    "kill: unknown signal %s; valid signals:\n", name);
9850084Sbostic 	printsig(stderr);
9950084Sbostic 	exit(1);
10050084Sbostic }
10150084Sbostic 
102*50752Sbostic void
10350084Sbostic printsig(fp)
10450084Sbostic 	FILE *fp;
10550084Sbostic {
106*50752Sbostic 	register int cnt;
10735204Sbostic 	register char **p;
10835204Sbostic 
109*50752Sbostic 	for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
11050084Sbostic 		(void)fprintf(fp, "%s ", *p);
111*50752Sbostic 		if (cnt == NSIG / 2)
11250084Sbostic 			(void)fprintf(fp, "\n");
11335204Sbostic 	}
11450084Sbostic 	(void)fprintf(fp, "\n");
11535204Sbostic }
11635204Sbostic 
117*50752Sbostic void
11835204Sbostic usage()
11935204Sbostic {
12050084Sbostic 	(void)fprintf(stderr, "usage: kill [-l] [-sig] pid ...\n");
12150084Sbostic 	exit(1);
12235204Sbostic }
123