2011-06-08 16:30:33 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2011-06-08 16:30:33 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2014-11-13 15:24:47 -05:00
|
|
|
static int bflag = 0;
|
|
|
|
static int sflag = 0;
|
2011-06-08 16:30:33 -04:00
|
|
|
|
2015-01-25 15:22:17 -05:00
|
|
|
static void
|
|
|
|
foldline(const char *str, size_t width)
|
|
|
|
{
|
|
|
|
int space;
|
|
|
|
size_t i = 0, n = 0, col, j;
|
|
|
|
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("<stdout>: write error:");
|
|
|
|
if (str[n])
|
|
|
|
putchar('\n');
|
|
|
|
} while (str[i = n] && str[i] != '\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fold(FILE *fp, long width)
|
|
|
|
{
|
|
|
|
char *buf = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
|
|
|
|
while (getline(&buf, &size, fp) != -1)
|
|
|
|
foldline(buf, width);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-01-25 15:22:17 -05:00
|
|
|
eprintf("usage: %s [-bs] [-w width] [-N] [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[])
|
|
|
|
{
|
2015-01-25 15:22:17 -05:00
|
|
|
size_t width = 80;
|
2011-06-08 16:30:33 -04:00
|
|
|
FILE *fp;
|
|
|
|
|
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':
|
|
|
|
width = estrtol(EARGF(usage()), 0);
|
|
|
|
break;
|
2013-11-11 14:53:01 -05:00
|
|
|
ARGNUM:
|
|
|
|
width = ARGNUMF(0);
|
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2015-01-25 15:22:17 -05:00
|
|
|
if (argc == 0)
|
2011-06-21 00:05:37 -04:00
|
|
|
fold(stdin, width);
|
2015-01-25 15:22:17 -05:00
|
|
|
else {
|
2014-11-13 12:29:30 -05:00
|
|
|
for (; argc > 0; argc--, argv++) {
|
|
|
|
if (!(fp = fopen(argv[0], "r"))) {
|
2014-04-22 06:43:01 -04:00
|
|
|
weprintf("fopen %s:", argv[0]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fold(fp, width);
|
|
|
|
fclose(fp);
|
2013-11-12 05:44:37 -05:00
|
|
|
}
|
2011-06-08 16:30:33 -04:00
|
|
|
}
|
2013-06-14 14:20:47 -04:00
|
|
|
|
2014-10-02 18:46:04 -04:00
|
|
|
return 0;
|
2011-06-08 16:30:33 -04:00
|
|
|
}
|