NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

bin/55531: [PATCH] script(1): Check if stdin is a terminal + other error handling



>Number:         55531
>Category:       bin
>Synopsis:       [PATCH] script(1): Check if stdin is a terminal + other error handling
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sun Aug 02 08:30:00 +0000 2020
>Originator:     Soumendra Ganguly
>Release:        9.0
>Organization:
Texas A&M University
>Environment:
NetBSD localhost 9.0 NetBSD 9.0 (GENERIC) #0: Fri Feb 14 00:06:28 UTC 2020  mkrepro%mkrepro.NetBSD.org@localhost:/usr/src/sys/arch/amd64/compile/GENERIC amd64
>Description:
If not using [-p], script(1) assumes that stdin is a terminal. However, that might not be the case. This patch fixes this. It also adds other error handling.

It also adds an extra call of termreset before printing "\nScript done on ..." in playback() [ see PR #55529 ].
>How-To-Repeat:

>Fix:
--- src/usr.bin/script/script.c	2020-08-02 02:06:56.406214345 -0500
+++ script.c	2020-08-02 03:06:46.020439568 -0500
@@ -155,17 +155,28 @@
 	if (pflg)
 		playback(fscript);
 
-	(void)tcgetattr(STDIN_FILENO, &tt);
-	(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
-	if (openpty(&master, &slave, NULL, &tt, &win) == -1)
-		err(1, "openpty");
+	isterm = isatty(STDIN_FILENO);
+	if (isterm) {
+		if (tcgetattr(STDIN_FILENO, &tt) == -1)
+			err(1, "tcgetattr");
+		if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
+			err(1, "ioctl");
+		if (openpty(&master, &slave, NULL, &tt, &win) == -1)
+			err(1, "openpty");
+	} else {
+		if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
+			err(1, "openpty");		
+	}
 
 	if (!quiet)
 		(void)printf("Script started, output file is %s\n", fname);
-	rtt = tt;
-	cfmakeraw(&rtt);
-	rtt.c_lflag &= ~ECHO;
-	(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
+
+	if (isterm) {
+		rtt = tt;
+		cfmakeraw(&rtt);
+		rtt.c_lflag &= ~ECHO;
+		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
+	}
 
 	(void)signal(SIGCHLD, finish);
 	child = fork();
@@ -301,7 +312,8 @@
 		(void)fclose(fscript);
 		(void)close(master);
 	} else {
-		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
+		if (isterm)
+			(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
 		if (!quiet)
 			(void)printf("Script done, output file is %s\n", fname);
 	}
@@ -365,13 +377,13 @@
 	if (!isterm)
 		return;
 
-	if (tcgetattr(STDOUT_FILENO, &tt) != 0)
+	if (tcgetattr(STDOUT_FILENO, &tt) == -1)
 		err(1, "tcgetattr");
 
 	traw = tt;
 	cfmakeraw(&traw);
 	traw.c_lflag |= ISIG;
-	if (tcsetattr(STDOUT_FILENO, TCSANOW, &traw) != 0)
+	if (tcsetattr(STDOUT_FILENO, TCSANOW, &traw) == -1)
 		err(1, "tcsetattr");
 }
 
@@ -431,6 +443,7 @@
 			atexit(termreset);
 			break;
 		case 'e':
+			termreset();
 			if (!quiet)
 				(void)printf("\nScript done on %s",
 				    ctime(&tclock));



Home | Main Index | Thread Index | Old Index