about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--mscan.c5
1 files changed, 4 insertions, 1 deletions
diff --git a/mscan.c b/mscan.c
index 4260af8..a209c2a 100644
--- a/mscan.c
+++ b/mscan.c
@@ -520,7 +520,8 @@ main(int argc, char *argv[])
 		replacement = 0xfffd;
 
 	struct winsize w;
-	if (ioctl(1, TIOCGWINSZ, &w) == 0) {
+	int ttyfd = open("/dev/tty", O_RDONLY | O_NOCTTY);
+	if (ttyfd >= 0 && ioctl(ttyfd, TIOCGWINSZ, &w) == 0) {
 		cols = w.ws_col;
 
 		char *pg;
@@ -535,6 +536,8 @@ main(int argc, char *argv[])
 				    pg, strerror(errno));
 		}
 	}
+	if (ttyfd >= 0)
+		close(ttyfd);
 	if (getenv("COLUMNS"))
 		cols = atoi(getenv("COLUMNS"));
 	if (cols <= 40)