>From 449374f43006a7bf373db37659345ca1cb8ad3a3 Mon Sep 17 00:00:00 2001
From: Kamil Rytarowski <n54@gmx.com>
Date: Thu, 31 Jul 2014 03:05:02 +0200
Subject: [PATCH 2/2] Adapt timeout(1) for NetBSD

---
 Makefile          |  4 ++--
 timeout/Makefile  |  1 +
 timeout/timeout.1 | 11 +++++++++++
 timeout/timeout.c | 43 +++++++++++++++++++++++++++++++++----------
 4 files changed, 47 insertions(+), 12 deletions(-)

diff --git a/Makefile b/Makefile
index ba4a534..e589e78 100644
--- a/Makefile
+++ b/Makefile
@@ -26,8 +26,8 @@ SUBDIR= apply asa at audio audiocfg \
 	rup ruptime rusers rwall rwho \
 	script sdiff sdpquery sed seq shar shlock \
 	showmount shuffle sockstat sort spell split stat su systat \
-	tabs tail talk tcopy tee telnet tftp tic time tip touch tpfmt tput \
-	tr true tset tsort tty ul uname unexpand unifdef \
+	tabs tail talk tcopy tee telnet tftp tic time timeout tip touch tpfmt \
+	tput tr true tset tsort tty ul uname unexpand unifdef \
 	uniq units unvis unzip usbhidaction usbhidctl users utoppya \
 	uudecode uuencode uuidgen vacation vgrind videoctl vis \
 	vmstat vndcompress w \
diff --git a/timeout/Makefile b/timeout/Makefile
index 9efc91d..f96189f 100644
--- a/timeout/Makefile
+++ b/timeout/Makefile
@@ -1,3 +1,4 @@
+# $NetBSD: $
 # $FreeBSD: head/usr.bin/timeout/Makefile 268745 2014-07-16 09:55:36Z bapt $
 
 PROG=	timeout
diff --git a/timeout/timeout.1 b/timeout/timeout.1
index 14dfdb3..a2da5b1 100644
--- a/timeout/timeout.1
+++ b/timeout/timeout.1
@@ -1,3 +1,5 @@
+.\"	$NetBSD: $
+.\"
 .\" Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org>
 .\" All rights reserved.
 .\"
@@ -112,3 +114,12 @@ exits after receiving a signal, the exit status returned is the signal number pl
 .Sh SEE ALSO
 .Xr kill 1 ,
 .Xr signal 3
+.Sh HISTORY
+A
+.Nm
+utility appeared in a development branch of FreeBSD 11 and was imported into NetBSD 7.0.
+FreeBSD work is compatible with GNU
+.Xr timeout 1
+by Padraig Brady, from GNU Coreutils 8.21. The
+.Xr timeout 1
+utility first appeared in GNU Coreutils 7.0.
diff --git a/timeout/timeout.c b/timeout/timeout.c
index 784bce3..7a580b5 100644
--- a/timeout/timeout.c
+++ b/timeout/timeout.c
@@ -1,3 +1,5 @@
+/* $NetBSD: $ */
+
 /*-
  * Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org>
  * Copyright (c) 2014 Vsevolod Stakhov <vsevolod@FreeBSD.org>
@@ -26,7 +28,13 @@
  */
 
 #include <sys/cdefs.h>
+#if !defined(lint)
+#if 0
 __FBSDID("$FreeBSD: head/usr.bin/timeout/timeout.c 268763 2014-07-16 13:52:05Z bapt $");
+#else
+__RCSID("$NetBSD: $");
+#endif
+#endif /* not lint */
 
 #include <sys/time.h>
 #include <sys/wait.h>
@@ -34,6 +42,7 @@ __FBSDID("$FreeBSD: head/usr.bin/timeout/timeout.c 268763 2014-07-16 13:52:05Z b
 #include <err.h>
 #include <errno.h>
 #include <getopt.h>
+#include <limits.h>
 #include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
@@ -49,7 +58,7 @@ static sig_atomic_t sig_term = 0;
 static sig_atomic_t sig_alrm = 0;
 static sig_atomic_t sig_ign = 0;
 
-static void
+static void __dead
 usage(void)
 {
 
@@ -102,20 +111,32 @@ static int
 parse_signal(const char *str)
 {
 	int sig, i;
-	const char *errstr;
-
-	sig = strtonum(str, 0, sys_nsig, &errstr);
+	char *ep;
 
-	if (errstr == NULL)
-		return (sig);
-	if (strncasecmp(str, "SIG", 3) == 0)
+	if (strncasecmp(str, "SIG", 3) == 0) {
 		str += 3;
 
-	for (i = 1; i < sys_nsig; i++) {
-		if (strcasecmp(str, sys_signame[i]) == 0)
-			return (i);
+		for (i = 1; i < sys_nsig; i++) {
+			if (strcasecmp(str, sys_signame[i]) == 0)
+				return (i);
+		}
+
+		goto err;
 	}
 
+	errno = 0;
+	sig = strtol(str, &ep, 10);
+
+	if (str[0] == '\0' || *ep != '\0')
+		goto err;
+	if (errno == ERANGE && (sig == INT_MAX || sig == INT_MIN))
+		goto err;
+	if (sig >= sys_nsig || sig < 0)
+		goto err;
+
+	return (sig);
+
+err:
 	errx(EX_USAGE, "invalid signal");
 }
 
@@ -182,6 +203,8 @@ main(int argc, char **argv)
 		SIGQUIT,
 	};
 
+	setprogname(argv[0]);
+
 	foreground = preserve = 0;
 	second_kill = 0;
 	cpid = -1;
-- 
1.9.4

