Subject: bin/19145: kill %jobid isn't supported by sh
To: None <gnats-bugs@gnats.netbsd.org>
From: None <dsl@l8s.co.uk>
List: netbsd-bugs
Date: 11/23/2002 12:20:42
>Number:         19145
>Category:       bin
>Synopsis:       kill %jobid isn't supported by sh
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Nov 23 04:19:00 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     David Laight
>Release:        NetBSD 1.6K
>Organization:
dis
>Environment:
System: NetBSD snowdrop 1.6K NetBSD 1.6K (GENERIC) #190: Mon Nov 18 14:36:48 GMT 2002
dsl@snowdrop:/oldroot/usr/bsd-current/src/sys/arch/i386/compile/GENERIC i386
Architecture: i386
Machine: i386
>Description:
	posix requires that the shell support 'kill %jobid', the netbsd
	sh doesn't have kill as a builtin, nor does kill have the
	code in it.
>How-To-Repeat:
	rtfm
	http://www.opengroup.org/onlinepubs/007904975/utilities/kill.html
>Fix:
	The following patch adds the code to kill.
	The required change to bin/sh will be supplied later
	(because it fixes a lot of other shell woes).

	This also makes the output of 'kill -l' fit the terminal width.

Index: kill.c
===================================================================
RCS file: /cvsroot/basesrc/bin/kill/kill.c,v
retrieving revision 1.19
diff -u -r1.19 kill.c
--- kill.c	2001/09/16 13:55:09	1.19
+++ kill.c	2002/11/23 12:10:18
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-#ifndef lint
+#if !defined(lint) && !defined(SHELL)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\n\
 	The Regents of the University of California.  All rights reserved.\n");
 #endif /* not lint */
@@ -54,11 +54,20 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-
-void nosig(char *);
-void printsignals(FILE *);
-int signame_to_signum(char *);
-void usage(void);
+#include <termios.h>
+#include <unistd.h>
+#include <locale.h>
+#include <sys/ioctl.h>
+
+#ifdef SHELL            /* sh (aka ash) builtin */
+#define main killcmd
+#include "../../bin/sh/bltin/bltin.h"
+#endif /* SHELL */ 
+
+static void nosig(char *);
+static void printsignals(FILE *);
+static int signame_to_signum(char *);
+static void usage(void);
 int main(int, char *[]);
 
 int
@@ -68,6 +77,7 @@
 	char *ep;
 
 	setprogname(argv[0]);
+	setlocale(LC_ALL, "");
 	if (argc < 2)
 		usage();
 
@@ -88,7 +98,7 @@
 				numsig -= 128;
 			if (numsig <= 0 || numsig >= NSIG)
 				nosig(*argv);
-			(void)printf("%s\n", sys_signame[numsig]);
+			printf("%s\n", sys_signame[numsig]);
 			exit(0);
 		}
 		printsignals(stdout);
@@ -127,11 +137,26 @@
 		usage();
 
 	for (errors = 0; argc; argc--, argv++) {
-		pid = strtol(*argv, &ep, 10);
-		if (!**argv || *ep) {
-			warnx("illegal process id: %s", *argv);
-			errors = 1;
-		} else if (kill(pid, numsig) == -1) {
+#ifdef SHELL
+		extern int getjobpgrp(const char *);
+		if (*argv[0] == '%') {
+			pid = getjobpgrp(*argv);
+			if (pid == 0) {
+				warnx("illegal job id: %s", *argv);
+				errors = 1;
+				continue;
+			}
+		} else 
+#endif
+		{
+			pid = strtol(*argv, &ep, 10);
+			if (!**argv || *ep) {
+				warnx("illegal process id: %s", *argv);
+				errors = 1;
+				continue;
+			}
+		}
+		if (kill(pid, numsig) == -1) {
 			warn("%s", *argv);
 			errors = 1;
 		}
@@ -141,7 +166,7 @@
 	/* NOTREACHED */
 }
 
-int
+static int
 signame_to_signum(char *sig)
 {
 	int n;
@@ -155,7 +180,7 @@
 	return (-1);
 }
 
-void
+static void
 nosig(char *name)
 {
 
@@ -165,25 +190,42 @@
 	/* NOTREACHED */
 }
 
-void
+static void
 printsignals(FILE *fp)
 {
-	int n;
+	int sig;
+	int len, nl;
+	const char *name;
+	int termwidth = 80;
+
+	if (isatty(fileno(fp))) {
+		struct winsize win;
+		if (ioctl(fileno(fp), TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
+			termwidth = win.ws_col;
+	}
 
-	for (n = 1; n < NSIG; n++) {
-		(void)fprintf(fp, "%s", sys_signame[n]);
-		if (n == (NSIG / 2) || n == (NSIG - 1))
-			(void)fprintf(fp, "\n");
-		else
-			(void)fprintf(fp, " ");
+	for (len = 0, sig = 1; sig < NSIG; sig++) {
+		name = sys_signame[sig];
+		nl = 1 + strlen(name);
+
+		if (len + nl >= termwidth) {
+			fprintf(fp, "\n");
+			len = 0;
+		} else
+			if (len != 0)
+				fprintf(fp, " ");
+		len += nl;
+		fprintf(fp, "%s", name);
 	}
+	if (len != 0)
+		fprintf(fp, "\n");
 }
 
-void
+static void
 usage(void)
 {
 
-	(void)fprintf(stderr, "usage: %s [-s signal_name] pid ...\n"
+	fprintf(stderr, "usage: %s [-s signal_name] pid ...\n"
 	    "       %s -l [exit_status]\n"
 	    "       %s -signal_name pid ...\n"
 	    "       %s -signal_number pid ...\n",
>Release-Note:
>Audit-Trail:
>Unformatted: