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.
This commit is contained in:
sin 2014-01-20 12:03:01 +00:00
parent fe6144793f
commit cc2d762798
2 changed files with 14 additions and 6 deletions

9
mc.1
View File

@ -19,9 +19,9 @@ If no file is given, mc reads from stdin.
specifies the maximum number of character columns to use specifies the maximum number of character columns to use
(unless the input contains lines longer than (unless the input contains lines longer than
.I chars .I chars
characters). characters). By default mc tries to figure out the width
.I chars of the output device, if that fails it defaults to 65
defaults to 65. chars.
.SH BUGS .SH BUGS
This implementation of This implementation of
.B mc .B mc
@ -32,5 +32,4 @@ or TAB characters correctly.
.B mc .B mc
currently mangles files which contain embedded NULs. currently mangles files which contain embedded NULs.
.B mc .B mc
does not attempt to determine the width of its output device, does not allow the user to set a default width in its environment.
nor does it allow the user to set a default width in its environment.

11
mc.c
View File

@ -5,10 +5,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/ioctl.h>
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
static long chars = 65; static long chars = 65;
static int cflag;
static struct linebuf b = EMPTY_LINEBUF; static struct linebuf b = EMPTY_LINEBUF;
static long n_columns; static long n_columns;
@ -26,10 +28,12 @@ main(int argc, char *argv[])
long i, l, col; long i, l, col;
size_t maxlen = 0; size_t maxlen = 0;
char *space; char *space;
struct winsize w;
FILE *fp; FILE *fp;
ARGBEGIN { ARGBEGIN {
case 'c': case 'c':
cflag = 1;
chars = estrtol(EARGF(usage()), 0); chars = estrtol(EARGF(usage()), 0);
if(chars < 3) if(chars < 3)
eprintf("%d: too few character columns"); eprintf("%d: too few character columns");
@ -38,6 +42,12 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } 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 */ /* XXX librarify this chunk, too? only useful in sponges though */
if(argc == 0) { if(argc == 0) {
getlines(stdin, &b); getlines(stdin, &b);
@ -84,4 +94,3 @@ main(int argc, char *argv[])
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }