changeset 1097:54b2776de2f4 draft

Refactor terminal querying.
author Rob Landley <rob@landley.net>
date Sun, 27 Oct 2013 00:02:56 -0500
parents 580a0300b68f
children 46d83f3c7546
files lib/lib.c
diffstat 1 files changed, 16 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/lib/lib.c	Sun Oct 13 15:33:32 2013 +0200
+++ b/lib/lib.c	Sun Oct 27 00:02:56 2013 -0500
@@ -485,29 +485,28 @@
 // set *x=0 and *y=0 before calling to detect failure to set either, or
 // x=80 y=25 to provide defaults
 
-void terminal_size(unsigned *x, unsigned *y)
+void terminal_size(unsigned *xx, unsigned *yy)
 {
   struct winsize ws;
-  int i;
+  unsigned i, x = xx ? *xx : 0, y = yy ? *yy : 0;
+  char *s;
 
-  //memset(&ws, 0, sizeof(ws));
   for (i=0; i<3; i++) {
-    if (ioctl(i, TIOCGWINSZ, &ws)) continue;
-    if (x) *x = ws.ws_col;
-    if (y) *y = ws.ws_row;
+    memset(&ws, 0, sizeof(ws));
+    if (!ioctl(i, TIOCGWINSZ, &ws)) {
+      if (ws.ws_col) x = ws.ws_col;
+      if (ws.ws_row) y = ws.ws_row;
+
+      break;
+    }
   }
-  if (x) {
-    char *s = getenv("COLUMNS");
+  s = getenv("COLUMNS");
+  if (s) sscanf(s, "%u", &x);
+  s = getenv("ROWS");
+  if (s) sscanf(s, "%u", &y);
 
-    i = s ? atoi(s) : 0;
-    if (i>0) *x = i;
-  }
-  if (y) {
-    char *s = getenv("ROWS");
-
-    i = s ? atoi(s) : 0;
-    if (i>0) *y = i;
-  }
+  if (xx) *xx = x;
+  if (yy) *yy = y;
 }
 
 int yesno(char *prompt, int def)