sbase

suckless unix tools
git clone git://git.2f30.org/sbase.git
Log | Files | Refs | README | LICENSE

commit bc0de5e7e541a6fc45774ac3a03248c2a533f9cf
parent f34e1ae1d0b2bb95290425a3e0292937b47e4de5
Author: sin <sin@2f39.org>
Date:   Wed Mar 12 16:43:45 +0200

Rename mc(1) to cols(1)

There is an obvious name collision with the popular file manager
midnight commander.

Diffstat:
Makefile | 2+-
cols.1 | 35+++++++++++++++++++++++++++++++++++
cols.c | 96+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
mc.1 | 35-----------------------------------
mc.c | 96-------------------------------------------------------------------------------
5 files changed, 132 insertions(+), 132 deletions(-)
diff --git a/Makefile b/Makefile @@ -36,6 +36,7 @@ SRC = \ chroot.c \ cksum.c \ cmp.c \ + cols.c \ comm.c \ cp.c \ cut.c \ @@ -53,7 +54,6 @@ SRC = \ kill.c \ ln.c \ ls.c \ - mc.c \ md5sum.c \ mkdir.c \ mkfifo.c \ diff --git a/cols.1 b/cols.1 @@ -0,0 +1,35 @@ +.TH COLS 1 sbase\-VERSION +.SH NAME +cols \- columnize output +.SH SYNOPSIS +.B cols +.RB [ \-c +.IR chars ] +.RI [ file ...] +.SH DESCRIPTION +.B cols +reads each file in sequence and writes them to stdout, +in as many vertical columns as will fit in +.I chars +character columns. +If no file is given, cols reads from stdin. +.SH OPTIONS +.TP +.BI \-c " chars" +specifies the maximum number of character columns to use +(unless the input contains lines longer than +.I chars +characters). By default cols tries to figure out the width +of the output device, if that fails it defaults to 65 +chars. +.SH BUGS +This implementation of +.B cols +assumes that every byte is a character +which takes up one column on the screen. +It does not handle non-ASCII UTF-8 runes +or TAB characters correctly. +.B cols +currently mangles files which contain embedded NULs. +.B cols +does not allow the user to set a default width in its environment. diff --git a/cols.c b/cols.c @@ -0,0 +1,96 @@ +/* See LICENSE file for copyright and license details. */ +#include <assert.h> +#include <stdbool.h> +#include <stdio.h> +#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; +static long n_rows; + +static void +usage(void) +{ + eprintf("usage: %s [-c chars] [FILE...]\n", argv0); +} + +int +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"); + break; + default: + 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); + } else for(; argc > 0; argc--, argv++) { + if(!(fp = fopen(argv[0], "r"))) + eprintf("fopen %s:", argv[0]); + getlines(fp, &b); + fclose(fp); + } + + for(l = 0; l < b.nlines; ++l) { + size_t len = strlen(b.lines[l]); + if(len > 0 && b.lines[l][len-1] == '\n') + b.lines[l][--len] = '\0'; + if(len > maxlen) + maxlen = len; + if(maxlen > (chars - 1) / 2) + break; + } + + n_columns = (chars + 1) / (maxlen + 1); + if(n_columns <= 1) { + for(l = 0; l < b.nlines; ++l) { + fputs(b.lines[l], stdout); + } + return EXIT_SUCCESS; + } + + if(!(space = malloc(maxlen + 2))) + eprintf("malloc:"); + memset(space, ' ', maxlen + 1); + space[maxlen + 1] = '\0'; + + n_rows = (b.nlines + (n_columns - 1)) / n_columns; + for(i = 0; i < n_rows; ++i) { + for(l = i, col = 1; l < b.nlines; l += n_rows, ++col) { + fputs(b.lines[l], stdout); + if(col < n_columns) + fputs(space + strlen(b.lines[l]), stdout); + } + fputs("\n", stdout); + } + free(space); + + return EXIT_SUCCESS; +} diff --git a/mc.1 b/mc.1 @@ -1,35 +0,0 @@ -.TH MC 1 sbase\-VERSION -.SH NAME -mc \- multi-column -.SH SYNOPSIS -.B mc -.RB [ \-c -.IR chars ] -.RI [ file ...] -.SH DESCRIPTION -.B mc -reads each file in sequence and writes them to stdout, -in as many vertical columns as will fit in -.I chars -character columns. -If no file is given, mc reads from stdin. -.SH OPTIONS -.TP -.BI \-c " chars" -specifies the maximum number of character columns to use -(unless the input contains lines longer than -.I chars -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 -assumes that every byte is a character -which takes up one column on the screen. -It does not handle non-ASCII UTF-8 runes -or TAB characters correctly. -.B mc -currently mangles files which contain embedded NULs. -.B mc -does not allow the user to set a default width in its environment. diff --git a/mc.c b/mc.c @@ -1,96 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <assert.h> -#include <stdbool.h> -#include <stdio.h> -#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; -static long n_rows; - -static void -usage(void) -{ - eprintf("usage: %s [-c chars] [FILE...]\n", argv0); -} - -int -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"); - break; - default: - 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); - } else for(; argc > 0; argc--, argv++) { - if(!(fp = fopen(argv[0], "r"))) - eprintf("fopen %s:", argv[0]); - getlines(fp, &b); - fclose(fp); - } - - for(l = 0; l < b.nlines; ++l) { - size_t len = strlen(b.lines[l]); - if(len > 0 && b.lines[l][len-1] == '\n') - b.lines[l][--len] = '\0'; - if(len > maxlen) - maxlen = len; - if(maxlen > (chars - 1) / 2) - break; - } - - n_columns = (chars + 1) / (maxlen + 1); - if(n_columns <= 1) { - for(l = 0; l < b.nlines; ++l) { - fputs(b.lines[l], stdout); - } - return EXIT_SUCCESS; - } - - if(!(space = malloc(maxlen + 2))) - eprintf("malloc:"); - memset(space, ' ', maxlen + 1); - space[maxlen + 1] = '\0'; - - n_rows = (b.nlines + (n_columns - 1)) / n_columns; - for(i = 0; i < n_rows; ++i) { - for(l = i, col = 1; l < b.nlines; l += n_rows, ++col) { - fputs(b.lines[l], stdout); - if(col < n_columns) - fputs(space + strlen(b.lines[l]), stdout); - } - fputs("\n", stdout); - } - free(space); - - return EXIT_SUCCESS; -}