VirtualBox

source: kBuild/trunk/src/ash/bltin/kill.c@ 632

Last change on this file since 632 was 632, checked in by bird, 18 years ago

MSC hacking.

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1/* $NetBSD: kill.c,v 1.23 2003/08/07 09:05:13 agc Exp $ */
2
3/*
4 * Copyright (c) 1988, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#if !defined(lint) && !defined(SHELL)
34__COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\n\
35 The Regents of the University of California. All rights reserved.\n");
36#endif /* not lint */
37
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)kill.c 8.4 (Berkeley) 4/28/95";
41#else
42__RCSID("$NetBSD: kill.c,v 1.23 2003/08/07 09:05:13 agc Exp $");
43#endif
44#endif /* not lint */
45
46#include <ctype.h>
47#include <err.h>
48#include <errno.h>
49#include <signal.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <termios.h>
54#include <unistd.h>
55#include <locale.h>
56#include <sys/ioctl.h>
57
58#ifndef HAVE_SYS_SIGNAME
59extern void init_sys_signame(void);
60extern char sys_signame[NSIG][16];
61#endif
62
63#ifdef SHELL /* sh (aka ash) builtin */
64#define main killcmd
65#include "bltin/bltin.h"
66#endif /* SHELL */
67
68static void nosig(char *);
69static void printsignals(FILE *);
70static int signame_to_signum(char *);
71static void usage(void);
72int main(int, char *[]);
73
74int
75main(int argc, char *argv[])
76{
77 int errors, numsig, pid;
78 char *ep;
79
80 setprogname(argv[0]);
81 setlocale(LC_ALL, "");
82 if (argc < 2)
83 usage();
84
85 numsig = SIGTERM;
86
87 argc--, argv++;
88 if (strcmp(*argv, "-l") == 0) {
89 argc--, argv++;
90 if (argc > 1)
91 usage();
92 if (argc == 1) {
93 if (isdigit((unsigned char)**argv) == 0)
94 usage();
95 numsig = strtol(*argv, &ep, 10);
96 if (*ep != '\0') {
97 errx(EXIT_FAILURE, "illegal signal number: %s",
98 *argv);
99 /* NOTREACHED */
100 }
101 if (numsig >= 128)
102 numsig -= 128;
103 if (numsig <= 0 || numsig >= NSIG)
104 nosig(*argv);
105#ifndef HAVE_SYS_SIGNAME
106 init_sys_signame();
107#endif
108 printf("%s\n", sys_signame[numsig]);
109 exit(0);
110 }
111 printsignals(stdout);
112 exit(0);
113 }
114
115 if (!strcmp(*argv, "-s")) {
116 argc--, argv++;
117 if (argc < 1) {
118 warnx("option requires an argument -- s");
119 usage();
120 }
121 if (strcmp(*argv, "0")) {
122 if ((numsig = signame_to_signum(*argv)) < 0)
123 nosig(*argv);
124 } else
125 numsig = 0;
126 argc--, argv++;
127 } else if (**argv == '-') {
128 ++*argv;
129 if (isalpha((unsigned char)**argv)) {
130 if ((numsig = signame_to_signum(*argv)) < 0)
131 nosig(*argv);
132 } else if (isdigit((unsigned char)**argv)) {
133 numsig = strtol(*argv, &ep, 10);
134 if (!*argv || *ep) {
135 errx(EXIT_FAILURE, "illegal signal number: %s",
136 *argv);
137 /* NOTREACHED */
138 }
139 if (numsig < 0 || numsig >= NSIG)
140 nosig(*argv);
141 } else
142 nosig(*argv);
143 argc--, argv++;
144 }
145
146 if (argc == 0)
147 usage();
148
149 for (errors = 0; argc; argc--, argv++) {
150#ifdef SHELL
151 extern int getjobpgrp(const char *);
152 if (*argv[0] == '%') {
153 pid = getjobpgrp(*argv);
154 if (pid == 0) {
155 warnx("illegal job id: %s", *argv);
156 errors = 1;
157 continue;
158 }
159 } else
160#endif
161 {
162 pid = strtol(*argv, &ep, 10);
163 if (!**argv || *ep) {
164 warnx("illegal process id: %s", *argv);
165 errors = 1;
166 continue;
167 }
168 }
169 if (kill(pid, numsig) == -1) {
170 warn("%s", *argv);
171 errors = 1;
172 }
173#ifdef SHELL
174 /* Wakeup the process if it was suspended, so it can
175 exit without an explicit 'fg'. */
176 if (numsig == SIGTERM || numsig == SIGHUP)
177 kill(pid, SIGCONT);
178#endif
179 }
180
181 exit(errors);
182 /* NOTREACHED */
183}
184
185static int
186signame_to_signum(char *sig)
187{
188 int n;
189#ifndef HAVE_SYS_SIGNAME
190 init_sys_signame();
191#endif
192 if (strncasecmp(sig, "sig", 3) == 0)
193 sig += 3;
194 for (n = 1; n < NSIG; n++) {
195 if (!strcasecmp(sys_signame[n], sig))
196 return (n);
197 }
198 return (-1);
199}
200
201static void
202nosig(char *name)
203{
204
205 warnx("unknown signal %s; valid signals:", name);
206 printsignals(stderr);
207 exit(1);
208 /* NOTREACHED */
209}
210
211static void
212printsignals(FILE *fp)
213{
214 int sig;
215 int len, nl;
216 const char *name;
217 int termwidth = 80;
218
219#ifdef TIOCGWINSZ
220 if (isatty(fileno(fp))) {
221 struct winsize win;
222 if (ioctl(fileno(fp), TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
223 termwidth = win.ws_col;
224 }
225#else
226#ifndef _MSC_VER
227#warning TIOCGWINSZ is not present.
228#endif
229#endif
230#ifndef HAVE_SYS_SIGNAME
231 init_sys_signame();
232#endif
233
234 for (len = 0, sig = 1; sig < NSIG; sig++) {
235 name = sys_signame[sig];
236 nl = 1 + strlen(name);
237
238 if (len + nl >= termwidth) {
239 fprintf(fp, "\n");
240 len = 0;
241 } else
242 if (len != 0)
243 fprintf(fp, " ");
244 len += nl;
245 fprintf(fp, "%s", name);
246 }
247 if (len != 0)
248 fprintf(fp, "\n");
249}
250
251static void
252usage(void)
253{
254
255 fprintf(stderr, "usage: %s [-s signal_name] pid ...\n"
256 " %s -l [exit_status]\n"
257 " %s -signal_name pid ...\n"
258 " %s -signal_number pid ...\n",
259 getprogname(), getprogname(), getprogname(), getprogname());
260 exit(1);
261 /* NOTREACHED */
262}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette