[hackers] [sbase] Support NUL containing lines in fold(1) and reuse the line-buffer(!) || FRIGN
commit d7741c77255baf28ddebcd5c5f947efa629c4d75
Author: FRIGN <dev_AT_frign.de>
AuthorDate: Wed Mar 9 12:38:44 2016 +0100
Commit: sin <sin_AT_2f30.org>
CommitDate: Thu Mar 10 08:48:09 2016 +0000
Support NUL containing lines in fold(1) and reuse the line-buffer(!)
diff --git a/README b/README
index 4fde8b1..55b4918 100644
--- a/README
+++ b/README
_AT_@ -37,7 +37,7 @@ The following tools are implemented:
0=*|o false .
0= find .
0=* x flock .
- #*|o fold .
+0#*|o fold .
0=*|o getconf (-v)
=*|o grep .
0=*|o head .
diff --git a/fold.c b/fold.c
index f220767..a5a987d 100644
--- a/fold.c
+++ b/fold.c
_AT_@ -5,6 +5,7 @@
#include <stdlib.h>
#include <string.h>
+#include "text.h"
#include "util.h"
static int bflag = 0;
_AT_@ -12,26 +13,25 @@ static int sflag = 0;
static size_t width = 80;
static void
-foldline(const char *str) {
- const char *p, *spacesect = NULL;
- size_t col, off;
+foldline(struct line *l) {
+ size_t i, col, last, spacesect, len;
- for (p = str, col = 0; *p && *p != '\n'; p++) {
- if (!UTF8_POINT(*p) && !bflag)
+ for (i = 0, last = 0, col = 0, spacesect = 0; i < l->len; i++) {
+ if (!UTF8_POINT(l->data[i]) && !bflag)
continue;
if (col >= width) {
- off = (sflag && spacesect) ? spacesect - str : p - str;
- if (fwrite(str, 1, off, stdout) != off)
+ len = ((sflag && spacesect) ? spacesect : i) - last;
+ if (fwrite(l->data + last, 1, len, stdout) != len)
eprintf("fwrite <stdout>:");
putchar('\n');
- spacesect = NULL;
+ last = (sflag && spacesect) ? spacesect : i;
col = 0;
- p = str += off;
+ spacesect = 0;
}
- if (sflag && isspace(*p))
- spacesect = p + 1;
- if (!bflag && iscntrl(*p)) {
- switch(*p) {
+ if (sflag && isspace(l->data[i]))
+ spacesect = i + 1;
+ if (!bflag && iscntrl(l->data[i])) {
+ switch(l->data[i]) {
case '\b':
col -= (col > 0);
break;
_AT_@ -46,20 +46,23 @@ foldline(const char *str) {
col++;
}
}
- fputs(str, stdout);
+ if (l->len - last)
+ fwrite(l->data + last, 1, l->len - last, stdout);
}
static void
fold(FILE *fp, const char *fname)
{
- char *buf = NULL;
- size_t size = 0;
+ static struct line line;
+ static size_t size = 0;
+ ssize_t len;
- while (getline(&buf, &size, fp) > 0)
- foldline(buf);
+ while ((len = getline(&line.data, &size, fp)) > 0) {
+ line.len = len;
+ foldline(&line);
+ }
if (ferror(fp))
eprintf("getline %s:", fname);
- free(buf);
}
static void
Received on Thu Mar 10 2016 - 09:48:19 CET
This archive was generated by hypermail 2.3.0
: Thu Mar 10 2016 - 10:00:58 CET