xref: /csrg-svn/bin/kill/kill.c (revision 35204)
11033Sbill /*
2*35204Sbostic  * Copyright (c) 1988 Regents of the University of California.
3*35204Sbostic  * All rights reserved.
4*35204Sbostic  *
5*35204Sbostic  * Redistribution and use in source and binary forms are permitted
6*35204Sbostic  * provided that the above copyright notice and this paragraph are
7*35204Sbostic  * duplicated in all such forms and that any documentation,
8*35204Sbostic  * advertising materials, and other materials related to such
9*35204Sbostic  * distribution and use acknowledge that the software was developed
10*35204Sbostic  * by the University of California, Berkeley.  The name of the
11*35204Sbostic  * University may not be used to endorse or promote products derived
12*35204Sbostic  * from this software without specific prior written permission.
13*35204Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*35204Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*35204Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
161033Sbill  */
171033Sbill 
18*35204Sbostic #ifndef lint
19*35204Sbostic char copyright[] =
20*35204Sbostic "@(#) Copyright (c) 1988 Regents of the University of California.\n\
21*35204Sbostic  All rights reserved.\n";
22*35204Sbostic #endif /* not lint */
23*35204Sbostic 
24*35204Sbostic #ifndef lint
25*35204Sbostic static char sccsid[] = "@(#)kill.c	4.5 (Berkeley) 07/21/88";
26*35204Sbostic #endif /* not lint */
27*35204Sbostic 
281033Sbill #include <signal.h>
29*35204Sbostic #include <stdio.h>
301363Sbill #include <ctype.h>
311033Sbill 
32*35204Sbostic static char *signals[] = {
33*35204Sbostic 	"hup", "int", "quit", "ill", "trap", "iot",		/*  1 - 6  */
34*35204Sbostic 	"emt", "fpe", "kill", "bus", "segv", "sys",		/*  7 - 12 */
35*35204Sbostic 	"pipe", "alrm",  "term", "urg", "stop", "tstp",		/* 13 - 18 */
36*35204Sbostic 	"cont", "chld", "ttin", "ttou", "io", "xcpu",		/* 19 - 24 */
37*35204Sbostic 	"xfsz", "vtalrm", "prof", "winch", "29", "usr1",	/* 25 - 30 */
38*35204Sbostic 	"usr2", NULL,						/* 31 - 32 */
39*35204Sbostic 	};
401363Sbill 
411033Sbill main(argc, argv)
42*35204Sbostic 	int argc;
43*35204Sbostic 	char **argv;
441033Sbill {
45*35204Sbostic 	register int numsig;
46*35204Sbostic 	register char **p;
47*35204Sbostic 	int errors;
481033Sbill 
49*35204Sbostic 	if (argc < 2)
50*35204Sbostic 		usage();
51*35204Sbostic 
52*35204Sbostic 	if (!strcmp(*++argv, "-l")) {
53*35204Sbostic 		printsig();
54*35204Sbostic 		exit(0);
551033Sbill 	}
56*35204Sbostic 
57*35204Sbostic 	numsig = SIGTERM;
58*35204Sbostic 	if (**argv == '-') {
59*35204Sbostic 		++*argv;
60*35204Sbostic 		if (isalpha(**argv)) {
61*35204Sbostic 			if (!strncasecmp(*argv, "sig", 3))
62*35204Sbostic 				*argv += 3;
63*35204Sbostic 			for (p = signals;; ++p) {
64*35204Sbostic 				if (!*p)
65*35204Sbostic 					goto error;
66*35204Sbostic 				if (!strcasecmp(*p, *argv)) {
67*35204Sbostic 					numsig = p - signals + 1;
68*35204Sbostic 					break;
69*35204Sbostic 				}
701363Sbill 			}
71*35204Sbostic 		}
72*35204Sbostic 		else if (isdigit(**argv)) {
73*35204Sbostic 			numsig = atoi(*argv);
74*35204Sbostic 			if (numsig <= 0 || numsig > NSIG)
75*35204Sbostic 				goto error;
76*35204Sbostic 		}
77*35204Sbostic 		else {
78*35204Sbostic error:			printf("kill: unknown signal %s; valid signals:\n", *argv);
79*35204Sbostic 			printsig();
801363Sbill 			exit(1);
811363Sbill 		}
82*35204Sbostic 		++argv;
83*35204Sbostic 	}
84*35204Sbostic 
85*35204Sbostic 	if (!*argv)
86*35204Sbostic 		usage();
87*35204Sbostic 
88*35204Sbostic 	for (errors = 0; *argv; ++argv) {
89*35204Sbostic 		if (!isdigit(**argv))
90*35204Sbostic 			usage();
91*35204Sbostic 		if (kill(atoi(*argv), numsig) == -1) {
92*35204Sbostic 			perror(*argv);
93*35204Sbostic 			errors = 1;
941033Sbill 		}
951033Sbill 	}
96*35204Sbostic 	exit(errors);
971033Sbill }
98*35204Sbostic 
99*35204Sbostic static
100*35204Sbostic printsig()
101*35204Sbostic {
102*35204Sbostic 	register char **p;
103*35204Sbostic 
104*35204Sbostic 	for (p = signals; *p; ++p) {
105*35204Sbostic 		printf("%s ", *p);
106*35204Sbostic 		if ((p - signals) == NSIG / 2 - 1)
107*35204Sbostic 			printf("\n");
108*35204Sbostic 	}
109*35204Sbostic 	printf("\n");
110*35204Sbostic }
111*35204Sbostic 
112*35204Sbostic static
113*35204Sbostic usage()
114*35204Sbostic {
115*35204Sbostic 	printf("usage: kill [-l] [-sig] pid ...\n");
116*35204Sbostic 	exit(2);
117*35204Sbostic }
118