As per discussion on IRC with FRIGN, ioctl is no standard, but it is
probably a defacto standard; how else can you determine the terminal
width portably?  However, this patch adds a fallback in case you can't
read the terminal width - this is how cols currently does it.

-- 
Tai Chi Minh Ralph Eastwood
tcmreastw...@gmail.com
From 6acec3c8e42a8fe81b8784cddd3c09af6485babc Mon Sep 17 00:00:00 2001
From: Tai Chi Minh Ralph Eastwood <tcmreastw...@gmail.com>
Date: Sun, 15 Feb 2015 14:38:01 +0000
Subject: [PATCH 4/4] ls: add fallback if ioctl does not work

---
 ls.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/ls.c b/ls.c
index ae5ec34..b23fbd8 100644
--- a/ls.c
+++ b/ls.c
@@ -51,7 +51,7 @@ static int qflag = 0;
 static int uflag = 0;
 static int many;
 static int first = 1;
-static struct winsize ws;
+static int cols = 65;
 
 static void
 usage(void)
@@ -67,6 +67,7 @@ main(int argc, char *argv[])
 	FILE *mfp;
 	char *buf;
 	size_t size;
+	static struct winsize ws;
 
 	ARGBEGIN {
 	case '1':
@@ -111,7 +112,8 @@ main(int argc, char *argv[])
 		break;
 	case 'C':
 		if ((Cflag = isatty(fileno(stdout))))
-			ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
+			if (!ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws))
+				cols = ws.ws_col;
 		break;
 	case 'R':
 		Rflag = 1;
@@ -446,10 +448,10 @@ static void columns(char *buf, size_t sz, int cnt)
 				t += ll[j] - colw[k];
 				colw[k] = ll[j];
 			}
-			if (t > ws.ws_col - (l - 1) * 2)
+			if (t > cols - (l - 1) * 2)
 				break;
 		}
-		if (t <= ws.ws_col - (l - 1) * 2)
+		if (t <= cols - (l - 1) * 2)
 			break; /* fits */
 	}
 	if (l == 0)
-- 
2.3.0

Reply via email to