Subject: bin/6699: [PATCH] Wump pager handling
To: None <gnats-bugs@gnats.netbsd.org>
From: Joseph Myers <jsm28@cam.ac.uk>
List: netbsd-bugs
Date: 01/01/1999 14:28:44
>Number:         6699
>Category:       bin
>Synopsis:       [PATCH] Wump pager handling
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Fri Jan  1 09:50:02 1999
>Last-Modified:
>Originator:     Joseph S. Myers
>Organization:
Trinity College, University of Cambridge, UK
>Release:        NetBSD-current of 1998-12-07
>Environment:
[
System: Linux decomino 2.0.36 #1 Mon Nov 16 14:25:34 UTC 1998 i686 unknown
Architecture: i686
]
>Description:

POSIX.2 specifies the handling of the PAGER environment variable for
those commands it specifies that can invoke a pager.  It is desirable
for other commands to behave in the same way; the appended patch gives
wump(6) this behaviour.  (A pager is only invoked when standard output
is a terminal; the paged data is passed to the pager on standard
input.  PAGER may specify any command that is a valid argument to `sh
-c', and is used if set and not null.)

>How-To-Repeat:

>Fix:

diff -ruN wump/wump.c wump+/wump.c
--- wump/wump.c	Mon Sep 14 11:05:06 1998
+++ wump+/wump.c	Fri Jan  1 14:16:30 1999
@@ -57,8 +57,10 @@
  * would care to remember.
  */
 
+#include <err.h>
 #include <sys/types.h>
 #include <sys/file.h>
+#include <sys/wait.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -724,7 +726,10 @@
 void
 instructions()
 {
-	char buf[120], *p;
+	const char *pager;
+	pid_t pid;
+	int status;
+	int fd;
 
 	/*
 	 * read the instructions file, if needed, and show the user how to
@@ -740,12 +745,26 @@
 		return;
 	}
 
-	if (!(p = getenv("PAGER")) ||
-	    strlen(p) > sizeof(buf) + strlen(_PATH_WUMPINFO) + 5)
-		p = _PATH_PAGER;
-
-	(void)sprintf(buf, "%s %s", p, _PATH_WUMPINFO);
-	(void)system(buf);
+	if (!isatty(1))
+		pager = "cat";
+	else {
+		if (!(pager = getenv("PAGER")) || (*pager == 0))
+			pager = _PATH_PAGER;
+	}
+	switch (pid = fork()) {
+	case 0: /* child */
+		if ((fd = open(_PATH_WUMPINFO, O_RDONLY)) == -1)
+			err(1, "open %s", _PATH_WUMPINFO);
+		if (dup2(fd, 0) == -1)
+			err(1, "dup2");
+		(void)execl("/bin/sh", "sh", "-c", pager, NULL);
+		err(1, "exec sh -c %s", pager);
+	case -1:
+		err(1, "fork");
+	default:
+		(void)waitpid(pid, &status, 0);
+		break;
+	}
 }
 
 void
>Audit-Trail:
>Unformatted: