1 /* $NetBSD: kill.c,v 1.25 2008/07/20 00:52:40 lukem Exp $ */
4 * Copyright (c) 1988, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 #if !defined(lint) && !defined(SHELL)
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)kill.c 8.4 (Berkeley) 4/28/95";
42 __RCSID("$NetBSD: kill.c,v 1.25 2008/07/20 00:52:40 lukem Exp $");
58 #include <sys/ioctl.h>
60 #ifdef SHELL /* sh (aka ash) builtin */
62 #include "../../bin/sh/bltin/bltin.h"
65 static void nosig(char *);
66 static void printsignals(FILE *);
67 static int signame_to_signum(char *);
68 static void usage(void);
69 int main(int, char *[]);
72 main(int argc
, char *argv
[])
79 setlocale(LC_ALL
, "");
86 if (strcmp(*argv
, "-l") == 0) {
91 if (isdigit((unsigned char)**argv
) == 0)
93 numsig
= strtoimax(*argv
, &ep
, 10);
94 /* check for correctly parsed number */
95 if (*ep
!= '\0' || numsig
== INTMAX_MIN
|| numsig
== INTMAX_MAX
) {
96 errx(EXIT_FAILURE
, "illegal signal number: %s",
102 /* and whether it fits into signals range */
103 if (numsig
<= 0 || numsig
>= NSIG
)
105 printf("%s\n", sys_signame
[(int) numsig
]);
108 printsignals(stdout
);
112 if (!strcmp(*argv
, "-s")) {
115 warnx("option requires an argument -- s");
118 if (strcmp(*argv
, "0")) {
119 if ((numsig
= signame_to_signum(*argv
)) < 0)
124 } else if (**argv
== '-') {
125 char *sn
= *argv
+ 1;
126 if (isalpha((unsigned char)*sn
)) {
127 if ((numsig
= signame_to_signum(sn
)) < 0)
129 } else if (isdigit((unsigned char)*sn
)) {
130 numsig
= strtoimax(sn
, &ep
, 10);
131 /* check for correctly parsed number */
132 if (*ep
|| numsig
== INTMAX_MIN
|| numsig
== INTMAX_MAX
) {
133 errx(EXIT_FAILURE
, "illegal signal number: %s",
137 /* and whether it fits into signals range */
138 if (numsig
< 0 || numsig
>= NSIG
)
148 for (errors
= 0; argc
; argc
--, argv
++) {
150 extern int getjobpgrp(const char *);
151 if (*argv
[0] == '%') {
152 pid
= getjobpgrp(*argv
);
154 warnx("illegal job id: %s", *argv
);
161 pid
= strtoimax(*argv
, &ep
, 10);
162 /* make sure the pid is a number and fits into pid_t */
163 if (!**argv
|| *ep
|| pid
== INTMAX_MIN
||
164 pid
== INTMAX_MAX
|| pid
!= (pid_t
) pid
) {
166 warnx("illegal process id: %s", *argv
);
171 if (kill((pid_t
) pid
, (int) numsig
) == -1) {
176 /* Wakeup the process if it was suspended, so it can
177 exit without an explicit 'fg'. */
178 if (numsig
== SIGTERM
|| numsig
== SIGHUP
)
179 kill((pid_t
) pid
, SIGCONT
);
188 signame_to_signum(char *sig
)
192 if (strncasecmp(sig
, "sig", 3) == 0)
194 for (n
= 1; n
< NSIG
; n
++) {
195 if (!strcasecmp(sys_signame
[n
], sig
))
205 warnx("unknown signal %s; valid signals:", name
);
206 printsignals(stderr
);
212 printsignals(FILE *fp
)
219 if (isatty(fileno(fp
))) {
221 if (ioctl(fileno(fp
), TIOCGWINSZ
, &win
) == 0 && win
.ws_col
> 0)
222 termwidth
= win
.ws_col
;
225 for (len
= 0, sig
= 1; sig
< NSIG
; sig
++) {
226 name
= sys_signame
[sig
];
227 nl
= 1 + strlen(name
);
229 if (len
+ nl
>= termwidth
) {
236 fprintf(fp
, "%s", name
);
246 fprintf(stderr
, "usage: %s [-s signal_name] pid ...\n"
247 " %s -l [exit_status]\n"
248 " %s -signal_name pid ...\n"
249 " %s -signal_number pid ...\n",
250 getprogname(), getprogname(), getprogname(), getprogname());