2011-06-08 16:30:33 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <ctype.h>
|
2015-01-31 19:24:03 -05:00
|
|
|
#include <stdint.h>
|
2011-06-08 16:30:33 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-03-16 14:26:42 -04:00
|
|
|
#include <string.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2011-06-08 16:30:33 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-03-13 18:47:41 -04:00
|
|
|
static int bflag = 0;
|
|
|
|
static int sflag = 0;
|
|
|
|
static size_t width = 80;
|
2011-06-08 16:30:33 -04:00
|
|
|
|
2015-01-25 15:22:17 -05:00
|
|
|
static void
|
2015-03-16 14:26:42 -04:00
|
|
|
foldline(const char *str) {
|
|
|
|
const char *p, *spacesect = NULL;
|
|
|
|
size_t col, off;
|
2015-01-25 15:22:17 -05:00
|
|
|
|
2015-03-16 14:26:42 -04:00
|
|
|
for (p = str, col = 0; *p && *p != '\n'; p++) {
|
|
|
|
if (!UTF8_POINT(*p) && !bflag)
|
|
|
|
continue;
|
|
|
|
if (col >= width) {
|
|
|
|
off = (sflag && spacesect) ? spacesect - str : p - str;
|
|
|
|
if (fwrite(str, 1, off, stdout) != off)
|
|
|
|
eprintf("fwrite <stdout>:");
|
|
|
|
putchar('\n');
|
|
|
|
spacesect = NULL;
|
|
|
|
col = 0;
|
|
|
|
p = str += off;
|
|
|
|
}
|
|
|
|
if (sflag && isspace(*p))
|
|
|
|
spacesect = p + 1;
|
|
|
|
if (!bflag && iscntrl(*p)) {
|
|
|
|
switch(*p) {
|
|
|
|
case '\b':
|
|
|
|
col -= (col > 0);
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
col = 0;
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
col += (col + 1) % 8;
|
|
|
|
break;
|
2015-03-13 18:47:41 -04:00
|
|
|
}
|
2015-03-16 14:26:42 -04:00
|
|
|
} else {
|
|
|
|
col++;
|
2015-01-25 15:22:17 -05:00
|
|
|
}
|
2015-03-16 14:26:42 -04:00
|
|
|
}
|
|
|
|
fputs(str, stdout);
|
2015-01-25 15:22:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-03-13 18:47:41 -04:00
|
|
|
fold(FILE *fp, const char *fname)
|
2015-01-25 15:22:17 -05:00
|
|
|
{
|
|
|
|
char *buf = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
|
2015-03-27 09:49:48 -04:00
|
|
|
while (getline(&buf, &size, fp) > 0)
|
2015-03-13 18:47:41 -04:00
|
|
|
foldline(buf);
|
|
|
|
if (ferror(fp))
|
|
|
|
eprintf("getline %s:", fname);
|
2015-01-25 15:22:17 -05:00
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-16 14:26:42 -04:00
|
|
|
eprintf("usage: %s [-bs] [-w num | -num] [FILE ...]\n", argv0);
|
2013-06-14 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2011-06-08 16:30:33 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2015-03-13 18:47:41 -04:00
|
|
|
int ret = 0;
|
2011-06-08 16:30:33 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'b':
|
2014-11-13 15:24:47 -05:00
|
|
|
bflag = 1;
|
2013-06-14 14:20:47 -04:00
|
|
|
break;
|
|
|
|
case 's':
|
2014-11-13 15:24:47 -05:00
|
|
|
sflag = 1;
|
2013-06-14 14:20:47 -04:00
|
|
|
break;
|
|
|
|
case 'w':
|
2015-01-31 19:24:03 -05:00
|
|
|
width = estrtonum(EARGF(usage()), 1, MIN(LLONG_MAX, SIZE_MAX));
|
2013-06-14 14:20:47 -04:00
|
|
|
break;
|
2013-11-11 14:53:01 -05:00
|
|
|
ARGNUM:
|
2015-01-30 11:45:44 -05:00
|
|
|
width = ARGNUMF();
|
2013-11-11 14:53:01 -05:00
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2015-03-13 18:47:41 -04:00
|
|
|
if (!argc) {
|
|
|
|
fold(stdin, "<stdin>");
|
2015-01-25 15:26:30 -05:00
|
|
|
} else {
|
2015-03-13 18:47:41 -04:00
|
|
|
for (; *argv; argc--, argv++) {
|
|
|
|
if (!(fp = fopen(*argv, "r"))) {
|
|
|
|
weprintf("fopen %s:", *argv);
|
|
|
|
ret = 1;
|
2015-03-16 14:26:42 -04:00
|
|
|
} else {
|
|
|
|
fold(fp, *argv);
|
|
|
|
fclose(fp);
|
2014-04-22 06:43:01 -04:00
|
|
|
}
|
2013-11-12 05:44:37 -05:00
|
|
|
}
|
2011-06-08 16:30:33 -04:00
|
|
|
}
|
2013-06-14 14:20:47 -04:00
|
|
|
|
2015-03-13 18:47:41 -04:00
|
|
|
return ret;
|
2011-06-08 16:30:33 -04:00
|
|
|
}
|