11033Sbill /*
266636Spendry * Copyright (c) 1988, 1993, 1994
360670Sbostic * The Regents of the University of California. All rights reserved.
435204Sbostic *
542534Sbostic * %sccs.include.redist.c%
61033Sbill */
71033Sbill
835204Sbostic #ifndef lint
960670Sbostic static char copyright[] =
1066636Spendry "@(#) Copyright (c) 1988, 1993, 1994\n\
1160670Sbostic The Regents of the University of California. All rights reserved.\n";
1235204Sbostic #endif /* not lint */
1335204Sbostic
1435204Sbostic #ifndef lint
15*68983Sbostic static char sccsid[] = "@(#)kill.c 8.4 (Berkeley) 04/28/95";
1635204Sbostic #endif /* not lint */
1735204Sbostic
1859496Sbostic #include <ctype.h>
1959496Sbostic #include <err.h>
2059496Sbostic #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 *));
27*68983Sbostic void printsignals __P((FILE *));
28*68983Sbostic int signame_to_signum __P((char *));
2950752Sbostic void usage __P((void));
301363Sbill
3159496Sbostic int
main(argc,argv)321033Sbill main(argc, argv)
3335204Sbostic int argc;
3459496Sbostic char *argv[];
351033Sbill {
3666556Spendry const char *const *p;
3766556Spendry int errors, numsig, pid;
3850084Sbostic char *ep;
391033Sbill
4035204Sbostic if (argc < 2)
4135204Sbostic usage();
4235204Sbostic
43*68983Sbostic numsig = SIGTERM;
44*68983Sbostic
45*68983Sbostic argc--, argv++;
46*68983Sbostic if (!strcmp(*argv, "-l")) {
47*68983Sbostic argc--, argv++;
48*68983Sbostic if (argc > 1)
49*68983Sbostic usage();
50*68983Sbostic if (argc == 1) {
51*68983Sbostic if (!isdigit(**argv))
52*68983Sbostic usage();
53*68983Sbostic numsig = strtol(*argv, &ep, 10);
54*68983Sbostic if (!*argv || *ep)
55*68983Sbostic errx(1, "illegal signal number: %s", *argv);
56*68983Sbostic if (numsig >= 128)
57*68983Sbostic numsig -= 128;
58*68983Sbostic if (numsig <= 0 || numsig >= NSIG)
59*68983Sbostic nosig(*argv);
60*68983Sbostic printf("%s\n", sys_signame[numsig]);
61*68983Sbostic exit(0);
62*68983Sbostic }
63*68983Sbostic printsignals(stdout);
6435204Sbostic exit(0);
651033Sbill }
6635204Sbostic
67*68983Sbostic if (!strcmp(*argv, "-s")) {
68*68983Sbostic argc--, argv++;
69*68983Sbostic if (argc < 1) {
70*68983Sbostic warnx("option requires an argument -- s");
71*68983Sbostic usage();
72*68983Sbostic }
73*68983Sbostic if (strcmp(*argv, "0")) {
74*68983Sbostic if ((numsig = signame_to_signum(*argv)) < 0)
75*68983Sbostic nosig(*argv);
76*68983Sbostic } else
77*68983Sbostic numsig = 0;
78*68983Sbostic argc--, argv++;
79*68983Sbostic } else if (**argv == '-') {
8035204Sbostic ++*argv;
8135204Sbostic if (isalpha(**argv)) {
82*68983Sbostic if ((numsig = signame_to_signum(*argv)) < 0)
8350760Sbostic nosig(*argv);
8450084Sbostic } else if (isdigit(**argv)) {
8550084Sbostic numsig = strtol(*argv, &ep, 10);
8659496Sbostic if (!*argv || *ep)
8759496Sbostic errx(1, "illegal signal number: %s", *argv);
88*68983Sbostic if (numsig <= 0 || numsig >= NSIG)
8950084Sbostic nosig(*argv);
9050084Sbostic } else
9150084Sbostic nosig(*argv);
92*68983Sbostic argc--, argv++;
9335204Sbostic }
9435204Sbostic
95*68983Sbostic if (argc == 0)
9635204Sbostic usage();
9735204Sbostic
98*68983Sbostic for (errors = 0; argc; argc--, argv++) {
9950084Sbostic pid = strtol(*argv, &ep, 10);
10050084Sbostic if (!*argv || *ep) {
10159496Sbostic warnx("illegal process id: %s", *argv);
10235204Sbostic errors = 1;
10359496Sbostic } else if (kill(pid, numsig) == -1) {
10459496Sbostic warn("%s", *argv);
10559496Sbostic errors = 1;
1061033Sbill }
1071033Sbill }
108*68983Sbostic
10935204Sbostic exit(errors);
1101033Sbill }
11135204Sbostic
112*68983Sbostic int
signame_to_signum(sig)113*68983Sbostic signame_to_signum(sig)
114*68983Sbostic char *sig;
115*68983Sbostic {
116*68983Sbostic int n;
117*68983Sbostic
118*68983Sbostic if (!strncasecmp(sig, "sig", 3))
119*68983Sbostic sig += 3;
120*68983Sbostic for (n = 1; n < NSIG; n++) {
121*68983Sbostic if (!strcasecmp(sys_signame[n], sig))
122*68983Sbostic return (n);
123*68983Sbostic }
124*68983Sbostic return (-1);
125*68983Sbostic }
126*68983Sbostic
12750752Sbostic void
nosig(name)12850084Sbostic nosig(name)
12950084Sbostic char *name;
13035204Sbostic {
13166556Spendry
13266556Spendry warnx("unknown signal %s; valid signals:", name);
133*68983Sbostic printsignals(stderr);
13450084Sbostic exit(1);
13550084Sbostic }
13650084Sbostic
13750752Sbostic void
printsignals(fp)138*68983Sbostic printsignals(fp)
13950084Sbostic FILE *fp;
14050084Sbostic {
141*68983Sbostic int n;
14235204Sbostic
143*68983Sbostic for (n = 1; n < NSIG; n++) {
144*68983Sbostic (void)fprintf(fp, "%s", sys_signame[n]);
145*68983Sbostic if (n == (NSIG / 2) || n == (NSIG - 1))
14650084Sbostic (void)fprintf(fp, "\n");
147*68983Sbostic else
148*68983Sbostic (void)fprintf(fp, " ");
14935204Sbostic }
15035204Sbostic }
15135204Sbostic
15250752Sbostic void
usage()15335204Sbostic usage()
15435204Sbostic {
15566556Spendry
156*68983Sbostic (void)fprintf(stderr, "usage: kill [-s signal_name] pid ...\n");
157*68983Sbostic (void)fprintf(stderr, " kill -l [exit_status]\n");
158*68983Sbostic (void)fprintf(stderr, " kill -signal_name pid ...\n");
159*68983Sbostic (void)fprintf(stderr, " kill -signal_number pid ...\n");
16050084Sbostic exit(1);
16135204Sbostic }
162