Module Name:    src
Committed By:   christos
Date:           Fri Aug  7 13:36:28 UTC 2020

Modified Files:
        src/usr.bin/script: script.c

Log Message:
PR/55548: Soumendra Ganguly: Since isatty(3) is implemented using
tcgetattr(3), call it directly to avoid calling it twice. This
makes error handling more precise. Also don't call err(3) when
tcsetattr(3) fails.


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/usr.bin/script/script.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/script/script.c
diff -u src/usr.bin/script/script.c:1.24 src/usr.bin/script/script.c:1.25
--- src/usr.bin/script/script.c:1.24	Sun Aug  2 23:34:43 2020
+++ src/usr.bin/script/script.c	Fri Aug  7 09:36:28 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: script.c,v 1.24 2020/08/03 03:34:43 christos Exp $	*/
+/*	$NetBSD: script.c,v 1.25 2020/08/07 13:36:28 christos Exp $	*/
 
 /*
  * Copyright (c) 1980, 1992, 1993
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 19
 #if 0
 static char sccsid[] = "@(#)script.c	8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: script.c,v 1.24 2020/08/03 03:34:43 christos Exp $");
+__RCSID("$NetBSD: script.c,v 1.25 2020/08/07 13:36:28 christos Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -156,17 +156,22 @@ main(int argc, char *argv[])
 	if (pflg)
 		playback(fscript);
 
-	isterm = isatty(STDIN_FILENO);
-	if (isterm) {
-		if (tcgetattr(STDIN_FILENO, &tt) == -1)
-			err(EXIT_FAILURE, "tcgetattr");
-		if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
+	if (tcgetattr(STDIN_FILENO, &tt) == -1 ||
+	    ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) {
+		switch (errno) {
+		case ENOTTY:
+			if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
+				err(EXIT_FAILURE, "openpty");
+			break;
+		case EBADF:
+			err(EXIT_FAILURE, "%d not valid fd", STDIN_FILENO);
+		default: /* errno == EFAULT or EINVAL for ioctl. Not reached in practice. */
 			err(EXIT_FAILURE, "ioctl");
-		if (openpty(&master, &slave, NULL, &tt, &win) == -1)
-			err(EXIT_FAILURE, "openpty");
+		}
 	} else {
-		if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
+		if (openpty(&master, &slave, NULL, &tt, &win) == -1)
 			err(EXIT_FAILURE, "openpty");
+		isterm = 1;
 	}
 
 	if (!quiet)
@@ -377,18 +382,17 @@ termset(void)
 {
 	struct termios traw;
 
-	isterm = isatty(STDOUT_FILENO);
-	if (!isterm)
+	if (tcgetattr(STDOUT_FILENO, &tt) == -1) {
+		if (errno == EBADF)
+			err(EXIT_FAILURE, "%d not valid fd", STDOUT_FILENO);
+		/* errno == ENOTTY */
 		return;
-
-	if (tcgetattr(STDOUT_FILENO, &tt) == -1)
-		err(EXIT_FAILURE, "tcgetattr");
-
+	}
+	isterm = 1;
 	traw = tt;
 	cfmakeraw(&traw);
 	traw.c_lflag |= ISIG;
-	if (tcsetattr(STDOUT_FILENO, TCSANOW, &traw) == -1)
-		err(EXIT_FAILURE, "tcsetattr");
+        (void)tcsetattr(STDOUT_FILENO, TCSANOW, &traw);
 }
 
 static void

Reply via email to