Support NUL containing lines in fold(1) and reuse the line-buffer(!)

This commit is contained in:
FRIGN 2016-03-09 12:38:44 +01:00 committed by sin
parent dc5190eab1
commit d7741c7725
2 changed files with 23 additions and 20 deletions

2
README
View File

@ -37,7 +37,7 @@ The following tools are implemented:
0=*|o false . 0=*|o false .
0= find . 0= find .
0=* x flock . 0=* x flock .
#*|o fold . 0#*|o fold .
0=*|o getconf (-v) 0=*|o getconf (-v)
=*|o grep . =*|o grep .
0=*|o head . 0=*|o head .

41
fold.c
View File

@ -5,6 +5,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "text.h"
#include "util.h" #include "util.h"
static int bflag = 0; static int bflag = 0;
@ -12,26 +13,25 @@ static int sflag = 0;
static size_t width = 80; static size_t width = 80;
static void static void
foldline(const char *str) { foldline(struct line *l) {
const char *p, *spacesect = NULL; size_t i, col, last, spacesect, len;
size_t col, off;
for (p = str, col = 0; *p && *p != '\n'; p++) { for (i = 0, last = 0, col = 0, spacesect = 0; i < l->len; i++) {
if (!UTF8_POINT(*p) && !bflag) if (!UTF8_POINT(l->data[i]) && !bflag)
continue; continue;
if (col >= width) { if (col >= width) {
off = (sflag && spacesect) ? spacesect - str : p - str; len = ((sflag && spacesect) ? spacesect : i) - last;
if (fwrite(str, 1, off, stdout) != off) if (fwrite(l->data + last, 1, len, stdout) != len)
eprintf("fwrite <stdout>:"); eprintf("fwrite <stdout>:");
putchar('\n'); putchar('\n');
spacesect = NULL; last = (sflag && spacesect) ? spacesect : i;
col = 0; col = 0;
p = str += off; spacesect = 0;
} }
if (sflag && isspace(*p)) if (sflag && isspace(l->data[i]))
spacesect = p + 1; spacesect = i + 1;
if (!bflag && iscntrl(*p)) { if (!bflag && iscntrl(l->data[i])) {
switch(*p) { switch(l->data[i]) {
case '\b': case '\b':
col -= (col > 0); col -= (col > 0);
break; break;
@ -46,20 +46,23 @@ foldline(const char *str) {
col++; col++;
} }
} }
fputs(str, stdout); if (l->len - last)
fwrite(l->data + last, 1, l->len - last, stdout);
} }
static void static void
fold(FILE *fp, const char *fname) fold(FILE *fp, const char *fname)
{ {
char *buf = NULL; static struct line line;
size_t size = 0; static size_t size = 0;
ssize_t len;
while (getline(&buf, &size, fp) > 0) while ((len = getline(&line.data, &size, fp)) > 0) {
foldline(buf); line.len = len;
foldline(&line);
}
if (ferror(fp)) if (ferror(fp))
eprintf("getline %s:", fname); eprintf("getline %s:", fname);
free(buf);
} }
static void static void