[hackers] [sbase] Use the width of the output device by default in mc(1) || sin

From: <git_AT_suckless.org>
Date: Tue, 21 Jan 2014 12:23:26 +0100

commit cc2d762798aa710b7e0cc916d6523ef5b7c92f2a
Author: sin <sin_AT_2f30.org>
Date: Mon Jan 20 12:03:01 2014 +0000

    Use the width of the output device by default in mc(1)
    
    If that fails, fallback to 65 characters as before. If the -c
    option is specified then just use that.

diff --git a/mc.1 b/mc.1
index f1c9a0f..b7ce4e3 100644
--- a/mc.1
+++ b/mc.1
_AT_@ -19,9 +19,9 @@ If no file is given, mc reads from stdin.
 specifies the maximum number of character columns to use
 (unless the input contains lines longer than
 .I chars
-characters).
-.I chars
-defaults to 65.
+characters). By default mc tries to figure out the width
+of the output device, if that fails it defaults to 65
+chars.
 .SH BUGS
 This implementation of
 .B mc
_AT_@ -32,5 +32,4 @@ or TAB characters correctly.
 .B mc
 currently mangles files which contain embedded NULs.
 .B mc
-does not attempt to determine the width of its output device,
-nor does it allow the user to set a default width in its environment.
+does not allow the user to set a default width in its environment.
diff --git a/mc.c b/mc.c
index 381885d..411ce58 100644
--- a/mc.c
+++ b/mc.c
_AT_@ -5,10 +5,12 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/ioctl.h>
 #include "text.h"
 #include "util.h"
 
 static long chars = 65;
+static int cflag;
 static struct linebuf b = EMPTY_LINEBUF;
 
 static long n_columns;
_AT_@ -26,10 +28,12 @@ main(int argc, char *argv[])
         long i, l, col;
         size_t maxlen = 0;
         char *space;
+ struct winsize w;
         FILE *fp;
 
         ARGBEGIN {
         case 'c':
+ cflag = 1;
                 chars = estrtol(EARGF(usage()), 0);
                 if(chars < 3)
                         eprintf("%d: too few character columns");
_AT_@ -38,6 +42,12 @@ main(int argc, char *argv[])
                 usage();
         } ARGEND;
 
+ if (cflag == 0) {
+ ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
+ if (w.ws_col != 0)
+ chars = w.ws_col;
+ }
+
         /* XXX librarify this chunk, too? only useful in sponges though */
         if(argc == 0) {
                 getlines(stdin, &b);
_AT_@ -84,4 +94,3 @@ main(int argc, char *argv[])
 
         return EXIT_SUCCESS;
 }
-
Received on Tue Jan 21 2014 - 12:23:26 CET

This archive was generated by hypermail 2.3.0 : Tue Jan 21 2014 - 12:24:04 CET