BSD 4_3_Net_2 release
[unix-history] / usr / src / bin / kill / kill.c
index 059caea..9aa50d7 100644 (file)
@@ -2,19 +2,33 @@
  * Copyright (c) 1988 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1988 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that: (1) source distributions retain this entire copyright
- * notice and comment, and (2) distributions including binaries display
- * the following acknowledgement:  ``This product includes software
- * developed by the University of California, Berkeley and its contributors''
- * in the documentation or other materials provided with the distribution
- * and in all advertising materials mentioning features or use of this
- * software. Neither the name of the University nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -24,11 +38,14 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)kill.c     4.6 (Berkeley) 5/31/90";
+static char sccsid[] = "@(#)kill.c     5.3 (Berkeley) 7/1/91";
 #endif /* not lint */
 
 #include <signal.h>
 #endif /* not lint */
 
 #include <signal.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <ctype.h>
 
 static char *signals[] = {
 #include <ctype.h>
 
 static char *signals[] = {
@@ -36,23 +53,23 @@ static char *signals[] = {
        "emt", "fpe", "kill", "bus", "segv", "sys",             /*  7 - 12 */
        "pipe", "alrm",  "term", "urg", "stop", "tstp",         /* 13 - 18 */
        "cont", "chld", "ttin", "ttou", "io", "xcpu",           /* 19 - 24 */
        "emt", "fpe", "kill", "bus", "segv", "sys",             /*  7 - 12 */
        "pipe", "alrm",  "term", "urg", "stop", "tstp",         /* 13 - 18 */
        "cont", "chld", "ttin", "ttou", "io", "xcpu",           /* 19 - 24 */
-       "xfsz", "vtalrm", "prof", "winch", "29", "usr1",        /* 25 - 30 */
+       "xfsz", "vtalrm", "prof", "winch", "info", "usr1",      /* 25 - 30 */
        "usr2", NULL,                                           /* 31 - 32 */
        "usr2", NULL,                                           /* 31 - 32 */
-       };
+};
 
 main(argc, argv)
        int argc;
        char **argv;
 {
 
 main(argc, argv)
        int argc;
        char **argv;
 {
-       register int numsig;
+       register int errors, numsig, pid;
        register char **p;
        register char **p;
-       int errors;
+       char *ep;
 
        if (argc < 2)
                usage();
 
        if (!strcmp(*++argv, "-l")) {
 
        if (argc < 2)
                usage();
 
        if (!strcmp(*++argv, "-l")) {
-               printsig();
+               printsig(stdout);
                exit(0);
        }
 
                exit(0);
        }
 
@@ -64,23 +81,23 @@ main(argc, argv)
                                *argv += 3;
                        for (p = signals;; ++p) {
                                if (!*p)
                                *argv += 3;
                        for (p = signals;; ++p) {
                                if (!*p)
-                                       goto error;
+                                       nosig(*argv);
                                if (!strcasecmp(*p, *argv)) {
                                        numsig = p - signals + 1;
                                        break;
                                }
                        }
                                if (!strcasecmp(*p, *argv)) {
                                        numsig = p - signals + 1;
                                        break;
                                }
                        }
-               }
-               else if (isdigit(**argv)) {
-                       numsig = atoi(*argv);
+               } else if (isdigit(**argv)) {
+                       numsig = strtol(*argv, &ep, 10);
+                       if (!*argv || *ep) {
+                               (void)fprintf(stderr,
+                                   "kill: illegal signal number %s\n", *argv);
+                               exit(1);
+                       }
                        if (numsig <= 0 || numsig > NSIG)
                        if (numsig <= 0 || numsig > NSIG)
-                               goto error;
-               }
-               else {
-error:                 printf("kill: unknown signal %s; valid signals:\n", *argv);
-                       printsig();
-                       exit(1);
-               }
+                               nosig(*argv);
+               } else
+                       nosig(*argv);
                ++argv;
        }
 
                ++argv;
        }
 
@@ -88,32 +105,45 @@ error:                     printf("kill: unknown signal %s; valid signals:\n", *argv);
                usage();
 
        for (errors = 0; *argv; ++argv) {
                usage();
 
        for (errors = 0; *argv; ++argv) {
-               if (!isdigit(**argv))
-                       usage();
-               if (kill(atoi(*argv), numsig) == -1) {
-                       perror(*argv);
+               pid = strtol(*argv, &ep, 10);
+               if (!*argv || *ep) {
+                       (void)fprintf(stderr,
+                           "kill: illegal process id %s\n", *argv);
+                       continue;
+               }
+               if (kill(pid, numsig) == -1) {
+                       (void)fprintf(stderr,
+                           "kill: %s: %s\n", *argv, strerror(errno));
                        errors = 1;
                }
        }
        exit(errors);
 }
 
                        errors = 1;
                }
        }
        exit(errors);
 }
 
-static
-printsig()
+nosig(name)
+       char *name;
+{
+       (void)fprintf(stderr,
+           "kill: unknown signal %s; valid signals:\n", name);
+       printsig(stderr);
+       exit(1);
+}
+
+printsig(fp)
+       FILE *fp;
 {
        register char **p;
 
        for (p = signals; *p; ++p) {
 {
        register char **p;
 
        for (p = signals; *p; ++p) {
-               printf("%s ", *p);
+               (void)fprintf(fp, "%s ", *p);
                if ((p - signals) == NSIG / 2 - 1)
                if ((p - signals) == NSIG / 2 - 1)
-                       printf("\n");
+                       (void)fprintf(fp, "\n");
        }
        }
-       printf("\n");
+       (void)fprintf(fp, "\n");
 }
 
 }
 
-static
 usage()
 {
 usage()
 {
-       printf("usage: kill [-l] [-sig] pid ...\n");
-       exit(2);
+       (void)fprintf(stderr, "usage: kill [-l] [-sig] pid ...\n");
+       exit(1);
 }
 }