xref: /csrg-svn/bin/kill/kill.c (revision 59496)
11033Sbill /*
235204Sbostic  * Copyright (c) 1988 Regents of the University of California.
335204Sbostic  * All rights reserved.
435204Sbostic  *
542534Sbostic  * %sccs.include.redist.c%
61033Sbill  */
71033Sbill 
835204Sbostic #ifndef lint
935204Sbostic char copyright[] =
1035204Sbostic "@(#) Copyright (c) 1988 Regents of the University of California.\n\
1135204Sbostic  All rights reserved.\n";
1235204Sbostic #endif /* not lint */
1335204Sbostic 
1435204Sbostic #ifndef lint
15*59496Sbostic static char sccsid[] = "@(#)kill.c	5.7 (Berkeley) 04/29/93";
1635204Sbostic #endif /* not lint */
1735204Sbostic 
18*59496Sbostic #include <ctype.h>
19*59496Sbostic #include <err.h>
20*59496Sbostic #include <errno.h>
211033Sbill #include <signal.h>
2235204Sbostic #include <stdio.h>
2346654Sbostic #include <stdlib.h>
2446654Sbostic #include <string.h>
251033Sbill 
2650752Sbostic void nosig __P((char *));
2750752Sbostic void printsig __P((FILE *));
2850752Sbostic void usage __P((void));
291363Sbill 
30*59496Sbostic int
311033Sbill main(argc, argv)
3235204Sbostic 	int argc;
33*59496Sbostic 	char *argv[];
341033Sbill {
3550084Sbostic 	register int errors, numsig, pid;
3658677Storek 	register char *const *p;
3750084Sbostic 	char *ep;
381033Sbill 
3935204Sbostic 	if (argc < 2)
4035204Sbostic 		usage();
4135204Sbostic 
4235204Sbostic 	if (!strcmp(*++argv, "-l")) {
4350084Sbostic 		printsig(stdout);
4435204Sbostic 		exit(0);
451033Sbill 	}
4635204Sbostic 
4735204Sbostic 	numsig = SIGTERM;
4835204Sbostic 	if (**argv == '-') {
4935204Sbostic 		++*argv;
5035204Sbostic 		if (isalpha(**argv)) {
5135204Sbostic 			if (!strncasecmp(*argv, "sig", 3))
5235204Sbostic 				*argv += 3;
5350760Sbostic 			for (numsig = NSIG, p = sys_signame + 1; --numsig; ++p)
5435204Sbostic 				if (!strcasecmp(*p, *argv)) {
5550752Sbostic 					numsig = p - sys_signame;
5635204Sbostic 					break;
5735204Sbostic 				}
5850760Sbostic 			if (!numsig)
5950760Sbostic 				nosig(*argv);
6050084Sbostic 		} else if (isdigit(**argv)) {
6150084Sbostic 			numsig = strtol(*argv, &ep, 10);
62*59496Sbostic 			if (!*argv || *ep)
63*59496Sbostic 				errx(1, "illegal signal number: %s", *argv);
6435204Sbostic 			if (numsig <= 0 || numsig > NSIG)
6550084Sbostic 				nosig(*argv);
6650084Sbostic 		} else
6750084Sbostic 			nosig(*argv);
6835204Sbostic 		++argv;
6935204Sbostic 	}
7035204Sbostic 
7135204Sbostic 	if (!*argv)
7235204Sbostic 		usage();
7335204Sbostic 
7435204Sbostic 	for (errors = 0; *argv; ++argv) {
7550084Sbostic 		pid = strtol(*argv, &ep, 10);
7650084Sbostic 		if (!*argv || *ep) {
77*59496Sbostic 			warnx("illegal process id: %s", *argv);
7835204Sbostic 			errors = 1;
79*59496Sbostic 		} else if (kill(pid, numsig) == -1) {
80*59496Sbostic 			warn("%s", *argv);
81*59496Sbostic 			errors = 1;
821033Sbill 		}
831033Sbill 	}
8435204Sbostic 	exit(errors);
851033Sbill }
8635204Sbostic 
8750752Sbostic void
8850084Sbostic nosig(name)
8950084Sbostic 	char *name;
9035204Sbostic {
9150084Sbostic 	(void)fprintf(stderr,
9250084Sbostic 	    "kill: unknown signal %s; valid signals:\n", name);
9350084Sbostic 	printsig(stderr);
9450084Sbostic 	exit(1);
9550084Sbostic }
9650084Sbostic 
9750752Sbostic void
9850084Sbostic printsig(fp)
9950084Sbostic 	FILE *fp;
10050084Sbostic {
10150752Sbostic 	register int cnt;
10258677Storek 	register char *const *p;
10335204Sbostic 
10450752Sbostic 	for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
10550084Sbostic 		(void)fprintf(fp, "%s ", *p);
10650752Sbostic 		if (cnt == NSIG / 2)
10750084Sbostic 			(void)fprintf(fp, "\n");
10835204Sbostic 	}
10950084Sbostic 	(void)fprintf(fp, "\n");
11035204Sbostic }
11135204Sbostic 
11250752Sbostic void
11335204Sbostic usage()
11435204Sbostic {
11550084Sbostic 	(void)fprintf(stderr, "usage: kill [-l] [-sig] pid ...\n");
11650084Sbostic 	exit(1);
11735204Sbostic }
118