[hackers] [sbase] Add COLUMNS support to cols(1) || sin

From: <git_AT_suckless.org>
Date: Thu, 19 Feb 2015 12:28:03 +0100 (CET)

commit 8bcae43ec18861b62a860a6ee96ea2abe064a99f
Author: sin <sin_AT_2f30.org>
Date: Thu Feb 19 11:24:28 2015 +0000

    Add COLUMNS support to cols(1)
    
    First attempt to see if COLUMNS is set, if that fails an ioctl()
    is issued to find the width of the output device. If that fails
    as well, we default to 65 chars per line.

diff --git a/cols.1 b/cols.1
index c027118..179ff0a 100644
--- a/cols.1
+++ b/cols.1
_AT_@ -1,4 +1,4 @@
-.Dd February 1, 2015
+.Dd February 19, 2015
 .Dt COLS 1
 .Os sbase
 .Sh NAME
_AT_@ -20,16 +20,24 @@ character columns. If no
 is given,
 .Nm
 reads from stdin.
+.Pp
+By default
+.Nm cols
+tries to figure out the width of the output
+device. If that fails, it defaults to 65 chars.
 .Sh OPTIONS
 .Bl -tag -width Ds
 .It Fl c Ar chars
 Set the maximum number of character columns to use
 (unless the input contains lines longer than
 .Ar num
-characters). By default
-.Nm cols
-tries to figure out the width of the output
-device. If that fails, it defaults to 65 chars.
+characters).
+.El
+.Sh ENVIRONMENT
+.Bl -tag -width COLUMNS
+.It COLUMNS
+If this variable is set, the value is used as the
+width of the output device.
 .El
 .Sh HISTORY
 .Nm
diff --git a/cols.c b/cols.c
index 572dbe0..a48c576 100644
--- a/cols.c
+++ b/cols.c
_AT_@ -31,6 +31,7 @@ main(int argc, char *argv[])
         size_t i, l, col, len, bytes, maxlen = 0;
         struct winsize w;
         FILE *fp;
+ char *p;
 
         ARGBEGIN {
         case 'c':
_AT_@ -42,8 +43,9 @@ main(int argc, char *argv[])
         } ARGEND;
 
         if (cflag == 0) {
- ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
- if (w.ws_col != 0)
+ if ((p = getenv("COLUMNS")))
+ chars = estrtonum(p, 1, MIN(LLONG_MAX, SIZE_MAX));
+ else if (!ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) && w.ws_col > 0)
                         chars = w.ws_col;
         }
 
Received on Thu Feb 19 2015 - 12:28:03 CET

This archive was generated by hypermail 2.3.0 : Thu Feb 19 2015 - 12:36:09 CET