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