]> git.saurik.com Git - apple/shell_cmds.git/blame - kill/kill.c
shell_cmds-118.tar.gz
[apple/shell_cmds.git] / kill / kill.c
CommitLineData
44bd5ea7
A
1/*
2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
44bd5ea7 34#ifndef lint
c0fcf4e1
A
35static char const copyright[] =
36"@(#) Copyright (c) 1988, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
44bd5ea7
A
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)kill.c 8.4 (Berkeley) 4/28/95";
44bd5ea7 43#endif
c0fcf4e1
A
44static const char rcsid[] =
45 "$FreeBSD: src/bin/kill/kill.c,v 1.11.2.1 2001/08/01 02:42:56 obrien Exp $";
44bd5ea7
A
46#endif /* not lint */
47
48#include <ctype.h>
49#include <err.h>
50#include <errno.h>
51#include <signal.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55
c0fcf4e1 56int main __P((int, char *[]));
44bd5ea7
A
57void nosig __P((char *));
58void printsignals __P((FILE *));
59int signame_to_signum __P((char *));
60void usage __P((void));
44bd5ea7
A
61
62int
63main(argc, argv)
64 int argc;
65 char *argv[];
66{
67 int errors, numsig, pid;
68 char *ep;
69
70 if (argc < 2)
71 usage();
72
73 numsig = SIGTERM;
74
75 argc--, argv++;
76 if (!strcmp(*argv, "-l")) {
77 argc--, argv++;
78 if (argc > 1)
79 usage();
80 if (argc == 1) {
81 if (!isdigit(**argv))
82 usage();
83 numsig = strtol(*argv, &ep, 10);
c0fcf4e1 84 if (!**argv || *ep)
44bd5ea7
A
85 errx(1, "illegal signal number: %s", *argv);
86 if (numsig >= 128)
87 numsig -= 128;
88 if (numsig <= 0 || numsig >= NSIG)
89 nosig(*argv);
c0fcf4e1 90 printf("%s\n", sys_signame[numsig]);
44bd5ea7
A
91 exit(0);
92 }
93 printsignals(stdout);
94 exit(0);
95 }
96
97 if (!strcmp(*argv, "-s")) {
98 argc--, argv++;
99 if (argc < 1) {
100 warnx("option requires an argument -- s");
101 usage();
102 }
103 if (strcmp(*argv, "0")) {
104 if ((numsig = signame_to_signum(*argv)) < 0)
105 nosig(*argv);
106 } else
107 numsig = 0;
108 argc--, argv++;
109 } else if (**argv == '-') {
110 ++*argv;
111 if (isalpha(**argv)) {
112 if ((numsig = signame_to_signum(*argv)) < 0)
113 nosig(*argv);
114 } else if (isdigit(**argv)) {
115 numsig = strtol(*argv, &ep, 10);
c0fcf4e1 116 if (!**argv || *ep)
44bd5ea7
A
117 errx(1, "illegal signal number: %s", *argv);
118 if (numsig < 0 || numsig >= NSIG)
119 nosig(*argv);
120 } else
121 nosig(*argv);
122 argc--, argv++;
123 }
124
125 if (argc == 0)
126 usage();
127
128 for (errors = 0; argc; argc--, argv++) {
129 pid = strtol(*argv, &ep, 10);
130 if (!**argv || *ep) {
131 warnx("illegal process id: %s", *argv);
132 errors = 1;
133 } else if (kill(pid, numsig) == -1) {
134 warn("%s", *argv);
135 errors = 1;
136 }
137 }
138
139 exit(errors);
44bd5ea7
A
140}
141
142int
143signame_to_signum(sig)
144 char *sig;
145{
146 int n;
147
c0fcf4e1 148 if (!strncasecmp(sig, "sig", (size_t)3))
44bd5ea7
A
149 sig += 3;
150 for (n = 1; n < NSIG; n++) {
151 if (!strcasecmp(sys_signame[n], sig))
152 return (n);
153 }
154 return (-1);
155}
156
157void
158nosig(name)
159 char *name;
160{
161
162 warnx("unknown signal %s; valid signals:", name);
163 printsignals(stderr);
164 exit(1);
44bd5ea7
A
165}
166
167void
168printsignals(fp)
169 FILE *fp;
170{
171 int n;
172
173 for (n = 1; n < NSIG; n++) {
174 (void)fprintf(fp, "%s", sys_signame[n]);
175 if (n == (NSIG / 2) || n == (NSIG - 1))
176 (void)fprintf(fp, "\n");
177 else
178 (void)fprintf(fp, " ");
179 }
180}
181
182void
183usage()
184{
185
c0fcf4e1
A
186 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
187 "usage: kill [-s signal_name] pid ...",
188 " kill -l [exit_status]",
189 " kill -signal_name pid ...",
190 " kill -signal_number pid ...");
44bd5ea7 191 exit(1);
44bd5ea7 192}