xref: /original-bsd/bin/kill/kill.c (revision 1b11f3a3)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1988 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)kill.c	5.5 (Berkeley) 08/04/91";
16 #endif /* not lint */
17 
18 #include <signal.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <ctype.h>
24 
25 void nosig __P((char *));
26 void printsig __P((FILE *));
27 void usage __P((void));
28 
29 main(argc, argv)
30 	int argc;
31 	char **argv;
32 {
33 	register int errors, numsig, pid;
34 	register char **p;
35 	char *ep;
36 
37 	if (argc < 2)
38 		usage();
39 
40 	if (!strcmp(*++argv, "-l")) {
41 		printsig(stdout);
42 		exit(0);
43 	}
44 
45 	numsig = SIGTERM;
46 	if (**argv == '-') {
47 		++*argv;
48 		if (isalpha(**argv)) {
49 			if (!strncasecmp(*argv, "sig", 3))
50 				*argv += 3;
51 			for (numsig = NSIG, p = sys_signame + 1; --numsig; ++p)
52 				if (!strcasecmp(*p, *argv)) {
53 					numsig = p - sys_signame;
54 					break;
55 				}
56 			if (!numsig)
57 				nosig(*argv);
58 		} else if (isdigit(**argv)) {
59 			numsig = strtol(*argv, &ep, 10);
60 			if (!*argv || *ep) {
61 				(void)fprintf(stderr,
62 				    "kill: illegal signal number %s\n", *argv);
63 				exit(1);
64 			}
65 			if (numsig <= 0 || numsig > NSIG)
66 				nosig(*argv);
67 		} else
68 			nosig(*argv);
69 		++argv;
70 	}
71 
72 	if (!*argv)
73 		usage();
74 
75 	for (errors = 0; *argv; ++argv) {
76 		pid = strtol(*argv, &ep, 10);
77 		if (!*argv || *ep) {
78 			(void)fprintf(stderr,
79 			    "kill: illegal process id %s\n", *argv);
80 			continue;
81 		}
82 		if (kill(pid, numsig) == -1) {
83 			(void)fprintf(stderr,
84 			    "kill: %s: %s\n", *argv, strerror(errno));
85 			errors = 1;
86 		}
87 	}
88 	exit(errors);
89 }
90 
91 void
92 nosig(name)
93 	char *name;
94 {
95 	(void)fprintf(stderr,
96 	    "kill: unknown signal %s; valid signals:\n", name);
97 	printsig(stderr);
98 	exit(1);
99 }
100 
101 void
102 printsig(fp)
103 	FILE *fp;
104 {
105 	register int cnt;
106 	register char **p;
107 
108 	for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
109 		(void)fprintf(fp, "%s ", *p);
110 		if (cnt == NSIG / 2)
111 			(void)fprintf(fp, "\n");
112 	}
113 	(void)fprintf(fp, "\n");
114 }
115 
116 void
117 usage()
118 {
119 	(void)fprintf(stderr, "usage: kill [-l] [-sig] pid ...\n");
120 	exit(1);
121 }
122