2012-05-21 22:05:09 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-01-20 12:03:01 +00:00
|
|
|
#include <sys/ioctl.h>
|
2014-11-13 18:54:28 +00:00
|
|
|
#include <unistd.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2012-05-21 22:05:09 +00:00
|
|
|
#include "text.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static long chars = 65;
|
2014-01-20 12:03:01 +00:00
|
|
|
static int cflag;
|
2012-05-21 22:05:09 +00:00
|
|
|
static struct linebuf b = EMPTY_LINEBUF;
|
|
|
|
|
|
|
|
static long n_columns;
|
|
|
|
static long n_rows;
|
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-c chars] [FILE...]\n", argv0);
|
|
|
|
}
|
|
|
|
|
2012-05-21 22:05:09 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
long i, l, col;
|
2014-06-04 15:46:22 +00:00
|
|
|
size_t len;
|
|
|
|
int maxlen = 0;
|
2014-01-20 12:03:01 +00:00
|
|
|
struct winsize w;
|
2012-05-21 22:05:09 +00:00
|
|
|
FILE *fp;
|
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'c':
|
2014-01-20 12:03:01 +00:00
|
|
|
cflag = 1;
|
2013-06-14 18:20:47 +00:00
|
|
|
chars = estrtol(EARGF(usage()), 0);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (chars < 3)
|
2013-06-14 18:20:47 +00:00
|
|
|
eprintf("%d: too few character columns");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
2012-05-21 22:05:09 +00:00
|
|
|
|
2014-01-20 12:03:01 +00:00
|
|
|
if (cflag == 0) {
|
|
|
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
|
|
|
if (w.ws_col != 0)
|
|
|
|
chars = w.ws_col;
|
|
|
|
}
|
|
|
|
|
2012-05-21 22:05:09 +00:00
|
|
|
/* XXX librarify this chunk, too? only useful in sponges though */
|
2014-11-13 17:29:30 +00:00
|
|
|
if (argc == 0) {
|
2012-05-21 22:05:09 +00:00
|
|
|
getlines(stdin, &b);
|
2014-11-13 17:29:30 +00:00
|
|
|
} else for (; argc > 0; argc--, argv++) {
|
|
|
|
if (!(fp = fopen(argv[0], "r")))
|
2013-06-14 18:20:47 +00:00
|
|
|
eprintf("fopen %s:", argv[0]);
|
2012-05-21 22:05:09 +00:00
|
|
|
getlines(fp, &b);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (l = 0; l < b.nlines; ++l) {
|
2014-06-04 15:46:22 +00:00
|
|
|
len = strlen(b.lines[l]);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (len > 0 && b.lines[l][len-1] == '\n')
|
2012-05-21 22:05:09 +00:00
|
|
|
b.lines[l][--len] = '\0';
|
2014-11-13 17:29:30 +00:00
|
|
|
if (len > maxlen)
|
2012-05-21 22:05:09 +00:00
|
|
|
maxlen = len;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (maxlen > (chars - 1) / 2)
|
2012-05-21 22:05:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
n_columns = (chars + 1) / (maxlen + 1);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (n_columns <= 1) {
|
|
|
|
for (l = 0; l < b.nlines; ++l) {
|
2012-05-21 22:05:09 +00:00
|
|
|
fputs(b.lines[l], stdout);
|
|
|
|
}
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2012-05-21 22:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n_rows = (b.nlines + (n_columns - 1)) / n_columns;
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0; i < n_rows; ++i) {
|
|
|
|
for (l = i, col = 1; l < b.nlines; l += n_rows, ++col) {
|
2014-06-04 15:46:22 +00:00
|
|
|
len = strlen(b.lines[l]);
|
2012-05-21 22:05:09 +00:00
|
|
|
fputs(b.lines[l], stdout);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (col < n_columns)
|
2014-06-04 15:46:22 +00:00
|
|
|
printf("%*s", maxlen + 1 - (int)len, "");
|
2012-05-21 22:05:09 +00:00
|
|
|
}
|
|
|
|
fputs("\n", stdout);
|
|
|
|
}
|
|
|
|
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2012-05-21 22:05:09 +00:00
|
|
|
}
|