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