From kensmith@cse.Buffalo.EDU  Fri Nov  7 05:17:18 2003
Return-Path: <kensmith@cse.Buffalo.EDU>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 916C416A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Fri,  7 Nov 2003 05:17:18 -0800 (PST)
Received: from opus.cse.buffalo.edu (opus.cse.Buffalo.EDU [128.205.32.55])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 8E5A443FDF
	for <FreeBSD-gnats-submit@freebsd.org>; Fri,  7 Nov 2003 05:17:17 -0800 (PST)
	(envelope-from kensmith@cse.Buffalo.EDU)
Received: from opus.cse.buffalo.edu (localhost.cse.buffalo.edu [127.0.0.1])
	by opus.cse.buffalo.edu (8.12.9p1/8.12.4) with ESMTP id hA7DMcD8097812
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 7 Nov 2003 08:22:38 -0500 (EST)
Received: (from kensmith@localhost)
	by opus.cse.buffalo.edu (8.12.9p1/8.12.9/Submit) id hA7DMcmD097811;
	Fri, 7 Nov 2003 08:22:38 -0500 (EST)
Message-Id: <200311071322.hA7DMcmD097811@opus.cse.buffalo.edu>
Date: Fri, 7 Nov 2003 08:22:38 -0500 (EST)
From: Ken Smith <kensmith@cse.Buffalo.EDU>
Reply-To: Ken Smith <kensmith@cse.Buffalo.EDU>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: Possible patch for PR's bin/56166 and bin/57414
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         59036
>Category:       bin
>Synopsis:       Possible patch for PR's bin/56166 and bin/57414
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    cperciva
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Nov 07 05:20:00 PST 2003
>Closed-Date:    Sat Mar 13 01:26:11 PST 2004
>Last-Modified:  Sat Mar 13 01:26:11 PST 2004
>Originator:     Ken Smith
>Release:        FreeBSD 5.1-RELEASE-p10 i386
>Organization:
U. Buffalo CSE
>Environment:
System: FreeBSD opus.cse.buffalo.edu 5.1-RELEASE-p10 FreeBSD 5.1-RELEASE-p10 #1: Sat Oct 4 22:57:49 EDT 2003 root@opus.cse.buffalo.edu:/usr/obj/usr/src/sys/OPUS i386
>Description:
	script(1) behaves badly if stdin is not a terminal.
>How-To-Repeat:
	See PR's bin/56166 and bin/57414
>Fix:

	This patch seems to fix at least 56166, has not been tested to see
	if it fixes 57414 but problems seem related.  The patch changes
	script(1) so it does not try to do terminal-specific things to
	stdin if it is not a terminal, and it does not immediately exit
	if end-of-file is read from stdin when stdin is not a terminal.


--- script.c_orig	Wed Sep  4 19:29:06 2002
+++ script.c	Thu Oct 16 22:18:48 2003
@@ -67,9 +67,9 @@
 int	master, slave;
 int	child;
 const char *fname;
-int	qflg;
+int	qflg, ttyflg;
 
-struct	termios tt;
+struct	termios tt, *ttptr;
 
 void	done(int) __dead2;
 void	dooutput(void);
@@ -83,7 +83,7 @@
 {
 	int cc;
 	struct termios rtt, stt;
-	struct winsize win;
+	struct winsize win, *winptr;
 	int aflg, kflg, ch, n;
 	struct timeval tv, *tvp;
 	time_t tvec, start;
@@ -93,6 +93,7 @@
 	int flushtime = 30;
 
 	aflg = kflg = 0;
+	winptr = NULL;
 	while ((ch = getopt(argc, argv, "aqkt:")) != -1)
 		switch(ch) {
 		case 'a':
@@ -126,9 +127,19 @@
 	if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
 		err(1, "%s", fname);
 
-	(void)tcgetattr(STDIN_FILENO, &tt);
-	(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
-	if (openpty(&master, &slave, NULL, &tt, &win) == -1)
+	if (ttyflg = isatty(STDIN_FILENO)) {
+		if (tcgetattr(STDIN_FILENO, &tt) == -1) {
+			err(1, "tcgetattr");
+		} else {
+			ttptr = &tt;
+		}
+		if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) {
+			err(1, "ioctl");
+		} else {
+			winptr = &win;
+		}
+	}
+	if (openpty(&master, &slave, NULL, ttptr, winptr) == -1)
 		err(1, "openpty");
 
 	if (!qflg) {
@@ -137,10 +148,12 @@
 		(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
 		fflush(fscript);
 	}
-	rtt = tt;
-	cfmakeraw(&rtt);
-	rtt.c_lflag &= ~ECHO;
-	(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
+	if (ttyflg) {
+		rtt = tt;
+		cfmakeraw(&rtt);
+		rtt.c_lflag &= ~ECHO;
+		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
+	}
 
 	child = fork();
 	if (child < 0) {
@@ -169,7 +182,9 @@
 			break;
 		if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
 			cc = read(STDIN_FILENO, ibuf, BUFSIZ);
-			if (cc <= 0)
+			if (cc < 0)
+				break;
+			if (cc == 0 && ttyflg)
 				break;
 			if (cc > 0) {
 				(void)write(master, ibuf, cc);
@@ -260,7 +275,8 @@
 {
 	time_t tvec;
 
-	(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
+	if (ttyflg)
+		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, ttptr);
 	tvec = time(NULL);
 	if (!qflg) {
 		(void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->patched 
State-Changed-By: cperciva 
State-Changed-When: Thu Jan 22 13:05:23 PST 2004 
State-Changed-Why:  
Fixed in -current, will MFC in 7 days 



Responsible-Changed-From-To: freebsd-bugs->cperciva 
Responsible-Changed-By: cperciva 
Responsible-Changed-When: Thu Jan 22 13:05:23 PST 2004 
Responsible-Changed-Why:  
Claim ownership in order to remind myself to close this later. 


http://www.freebsd.org/cgi/query-pr.cgi?pr=59036 
State-Changed-From-To: patched->closed 
State-Changed-By: cperciva 
State-Changed-When: Sat Mar 13 01:25:53 PST 2004 
State-Changed-Why:  
MFC to RELENG_4 is done. 


http://www.freebsd.org/cgi/query-pr.cgi?pr=59036 
>Unformatted:
