xref: /csrg-svn/bin/kill/kill.c (revision 50084)
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*50084Sbostic static char sccsid[] = "@(#)kill.c	5.1 (Berkeley) 06/17/91";
1635204Sbostic #endif /* not lint */
1735204Sbostic 
181033Sbill #include <signal.h>
19*50084Sbostic #include <errno.h>
2035204Sbostic #include <stdio.h>
2146654Sbostic #include <stdlib.h>
2246654Sbostic #include <string.h>
231363Sbill #include <ctype.h>
241033Sbill 
2535204Sbostic static char *signals[] = {
2635204Sbostic 	"hup", "int", "quit", "ill", "trap", "iot",		/*  1 - 6  */
2735204Sbostic 	"emt", "fpe", "kill", "bus", "segv", "sys",		/*  7 - 12 */
2835204Sbostic 	"pipe", "alrm",  "term", "urg", "stop", "tstp",		/* 13 - 18 */
2935204Sbostic 	"cont", "chld", "ttin", "ttou", "io", "xcpu",		/* 19 - 24 */
3035204Sbostic 	"xfsz", "vtalrm", "prof", "winch", "29", "usr1",	/* 25 - 30 */
3135204Sbostic 	"usr2", NULL,						/* 31 - 32 */
32*50084Sbostic };
331363Sbill 
341033Sbill main(argc, argv)
3535204Sbostic 	int argc;
3635204Sbostic 	char **argv;
371033Sbill {
38*50084Sbostic 	register int errors, numsig, pid;
3935204Sbostic 	register char **p;
40*50084Sbostic 	char *ep;
411033Sbill 
4235204Sbostic 	if (argc < 2)
4335204Sbostic 		usage();
4435204Sbostic 
4535204Sbostic 	if (!strcmp(*++argv, "-l")) {
46*50084Sbostic 		printsig(stdout);
4735204Sbostic 		exit(0);
481033Sbill 	}
4935204Sbostic 
5035204Sbostic 	numsig = SIGTERM;
5135204Sbostic 	if (**argv == '-') {
5235204Sbostic 		++*argv;
5335204Sbostic 		if (isalpha(**argv)) {
5435204Sbostic 			if (!strncasecmp(*argv, "sig", 3))
5535204Sbostic 				*argv += 3;
5635204Sbostic 			for (p = signals;; ++p) {
5735204Sbostic 				if (!*p)
58*50084Sbostic 					nosig(*argv);
5935204Sbostic 				if (!strcasecmp(*p, *argv)) {
6035204Sbostic 					numsig = p - signals + 1;
6135204Sbostic 					break;
6235204Sbostic 				}
631363Sbill 			}
64*50084Sbostic 		} else if (isdigit(**argv)) {
65*50084Sbostic 			numsig = strtol(*argv, &ep, 10);
66*50084Sbostic 			if (!*argv || *ep) {
67*50084Sbostic 				(void)fprintf(stderr,
68*50084Sbostic 				    "kill: illegal signal number %s\n", *argv);
69*50084Sbostic 				exit(1);
70*50084Sbostic 			}
7135204Sbostic 			if (numsig <= 0 || numsig > NSIG)
72*50084Sbostic 				nosig(*argv);
73*50084Sbostic 		} else
74*50084Sbostic 			nosig(*argv);
7535204Sbostic 		++argv;
7635204Sbostic 	}
7735204Sbostic 
7835204Sbostic 	if (!*argv)
7935204Sbostic 		usage();
8035204Sbostic 
8135204Sbostic 	for (errors = 0; *argv; ++argv) {
82*50084Sbostic 		pid = strtol(*argv, &ep, 10);
83*50084Sbostic 		if (!*argv || *ep) {
84*50084Sbostic 			(void)fprintf(stderr,
85*50084Sbostic 			    "kill: illegal process id %s\n", *argv);
86*50084Sbostic 			continue;
87*50084Sbostic 		}
8835204Sbostic 		if (kill(atoi(*argv), numsig) == -1) {
89*50084Sbostic 			(void)fprintf(stderr,
90*50084Sbostic 			    "kill: %s: %s\n", *argv, strerror(errno));
9135204Sbostic 			errors = 1;
921033Sbill 		}
931033Sbill 	}
9435204Sbostic 	exit(errors);
951033Sbill }
9635204Sbostic 
97*50084Sbostic nosig(name)
98*50084Sbostic 	char *name;
9935204Sbostic {
100*50084Sbostic 	(void)fprintf(stderr,
101*50084Sbostic 	    "kill: unknown signal %s; valid signals:\n", name);
102*50084Sbostic 	printsig(stderr);
103*50084Sbostic 	exit(1);
104*50084Sbostic }
105*50084Sbostic 
106*50084Sbostic printsig(fp)
107*50084Sbostic 	FILE *fp;
108*50084Sbostic {
10935204Sbostic 	register char **p;
11035204Sbostic 
11135204Sbostic 	for (p = signals; *p; ++p) {
112*50084Sbostic 		(void)fprintf(fp, "%s ", *p);
11335204Sbostic 		if ((p - signals) == NSIG / 2 - 1)
114*50084Sbostic 			(void)fprintf(fp, "\n");
11535204Sbostic 	}
116*50084Sbostic 	(void)fprintf(fp, "\n");
11735204Sbostic }
11835204Sbostic 
11935204Sbostic usage()
12035204Sbostic {
121*50084Sbostic 	(void)fprintf(stderr, "usage: kill [-l] [-sig] pid ...\n");
122*50084Sbostic 	exit(1);
12335204Sbostic }
124