#include <ctype.h>
#include <err.h>
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#define DEFLINEWIDTH 80
static void fold(unsigned int);
static int isu8cont(unsigned char);
static __dead void usage(void);
int count_bytes = 0;
int split_words = 0;
int
main(int argc, char *argv[])
{
int ch, lastch, newarg, prevoptind;
unsigned int width;
const char *errstr;
setlocale(LC_CTYPE, "");
if (pledge("stdio rpath", NULL) == -1)
err(1, "pledge");
width = 0;
lastch = '\0';
prevoptind = 1;
newarg = 1;
while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1) {
switch (ch) {
case 'b':
count_bytes = 1;
break;
case 's':
split_words = 1;
break;
case 'w':
width = strtonum(optarg, 1, UINT_MAX, &errstr);
if (errstr != NULL)
errx(1, "illegal width value, %s: %s", errstr,
optarg);
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (newarg)
width = 0;
else if (!isdigit(lastch))
usage();
if (width > UINT_MAX / 10 - 1)
errx(1, "illegal width value, too large");
width = (width * 10) + (ch - '0');
if (width < 1)
errx(1, "illegal width value, too small");
break;
default:
usage();
}
lastch = ch;
newarg = optind != prevoptind;
prevoptind = optind;
}
argv += optind;
argc -= optind;
if (width == 0)
width = DEFLINEWIDTH;
if (!*argv) {
if (pledge("stdio", NULL) == -1)
err(1, "pledge");
fold(width);
} else {
for (; *argv; ++argv) {
if (!freopen(*argv, "r", stdin))
err(1, "%s", *argv);
else
fold(width);
}
}
return 0;
}
static void
fold(unsigned int max_width)
{
static char *buf = NULL;
static size_t bufsz = 2048;
char *cp;
char *np;
char *sp;
char *nbuf;
wchar_t wc;
int ch;
int len;
unsigned int col;
int width;
if (buf == NULL && (buf = malloc(bufsz)) == NULL)
err(1, NULL);
np = cp = buf;
ch = 0;
col = 0;
while (ch != EOF) {
while ((ch = getchar()) != EOF) {
if (np + 1 == buf + bufsz) {
nbuf = reallocarray(buf, 2, bufsz);
if (nbuf == NULL)
err(1, NULL);
bufsz *= 2;
cp = nbuf + (cp - buf);
np = nbuf + (np - buf);
buf = nbuf;
}
*np++ = ch;
if (np - cp > 4 || (np - cp > 1 && !isu8cont(ch)))
break;
}
while (cp < np) {
if (*cp == '\n' || (*cp == '\r' && !count_bytes)) {
fwrite(buf, 1, ++cp - buf, stdout);
memmove(buf, cp, np - cp);
np = buf + (np - cp);
cp = buf;
col = 0;
continue;
}
if (*cp == '\b' && !count_bytes) {
if (col)
col--;
cp++;
continue;
}
if (np - cp > (ch != EOF)) {
len = 1;
width = 1;
if (*cp == '\t') {
if (count_bytes == 0)
width = 8 - (col & 7);
} else if ((len = mbtowc(&wc, cp,
np - cp)) < 1)
len = 1;
else if (count_bytes)
width = len;
else if ((width = wcwidth(wc)) < 0)
width = 1;
col += width;
if (col <= max_width || cp == buf) {
cp += len;
continue;
}
}
if (col > max_width) {
if (split_words) {
for (sp = cp; sp > buf; sp--) {
if (sp[-1] == ' ') {
cp = sp;
break;
}
}
}
fwrite(buf, 1, cp - buf, stdout);
putchar('\n');
memmove(buf, cp, np - cp);
np = buf + (np - cp);
cp = buf;
col = 0;
continue;
}
break;
}
}
fwrite(buf, 1, np - buf, stdout);
if (ferror(stdin))
err(1, NULL);
}
static int
isu8cont(unsigned char c)
{
return MB_CUR_MAX > 1 && (c & (0x80 | 0x40)) == 0x80;
}
static __dead void
usage(void)
{
(void)fprintf(stderr, "usage: fold [-bs] [-w width] [file ...]\n");
exit(1);
}