sbase/fold.c

114 lines
1.7 KiB
C
Raw Normal View History

2011-06-08 20:30:33 +00:00
/* See LICENSE file for copyright and license details. */
#include <ctype.h>
#include <stdint.h>
2011-06-08 20:30:33 +00:00
#include <stdio.h>
#include <stdlib.h>
2011-06-08 20:30:33 +00:00
#include "util.h"
static int bflag = 0;
static int sflag = 0;
static size_t width = 80;
2011-06-08 20:30:33 +00:00
static void
foldline(const char *str)
{
size_t i = 0, n = 0, col, j;
int space;
char c;
do {
space = 0;
for (j = i, col = 0; str[j] && col <= width; j++) {
c = str[j];
if (!UTF8_POINT(c) && !bflag)
continue;
if (sflag && isspace(c)) {
space = 1;
n = j + 1;
} else if (!space) {
n = j;
}
if (!bflag && iscntrl(c)) {
switch(c) {
case '\b':
col--;
break;
case '\r':
col = 0;
break;
case '\t':
col += (col + 1) % 8;
break;
}
} else {
col++;
}
}
if (fwrite(str + i, 1, n - i, stdout) != n - i)
eprintf("fwrite <stdout>:");
if (str[n])
putchar('\n');
} while (str[i = n] && str[i] != '\n');
}
static void
fold(FILE *fp, const char *fname)
{
char *buf = NULL;
size_t size = 0;
while (getline(&buf, &size, fp) >= 0)
foldline(buf);
if (ferror(fp))
eprintf("getline %s:", fname);
free(buf);
}
2013-06-14 18:20:47 +00:00
static void
usage(void)
{
2015-03-05 08:15:23 +00:00
eprintf("usage: %s [-bs] [-w width | -width] [FILE...]\n", argv0);
2013-06-14 18:20:47 +00:00
}
2011-06-08 20:30:33 +00:00
int
main(int argc, char *argv[])
{
FILE *fp;
int ret = 0;
2011-06-08 20:30:33 +00:00
2013-06-14 18:20:47 +00:00
ARGBEGIN {
case 'b':
bflag = 1;
2013-06-14 18:20:47 +00:00
break;
case 's':
sflag = 1;
2013-06-14 18:20:47 +00:00
break;
case 'w':
width = estrtonum(EARGF(usage()), 1, MIN(LLONG_MAX, SIZE_MAX));
2013-06-14 18:20:47 +00:00
break;
ARGNUM:
width = ARGNUMF();
break;
2013-06-14 18:20:47 +00:00
default:
usage();
} ARGEND;
if (!argc) {
fold(stdin, "<stdin>");
} else {
for (; *argv; argc--, argv++) {
if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv);
ret = 1;
2014-04-22 10:43:01 +00:00
continue;
}
fold(fp, *argv);
2014-04-22 10:43:01 +00:00
fclose(fp);
}
2011-06-08 20:30:33 +00:00
}
2013-06-14 18:20:47 +00:00
return ret;
2011-06-08 20:30:33 +00:00
}