[hackers] [sbase] Support NUL containing lines in comm(1) || FRIGN
commit eebba22577d649952ff4b4e456a0dc2e26811171
Author: FRIGN <dev_AT_frign.de>
AuthorDate: Mon Mar 7 01:42:01 2016 +0100
Commit: sin <sin_AT_2f30.org>
CommitDate: Thu Mar 10 08:48:09 2016 +0000
Support NUL containing lines in comm(1)
diff --git a/README b/README
index 979e80e..8f553a6 100644
--- a/README
+++ b/README
_AT_@ -22,7 +22,7 @@ The following tools are implemented:
0=*|o cksum .
0=*|o cmp .
#*|x cols .
- =*|o comm .
+0=*|o comm .
0=*|o cp (-i)
0=*|x cron .
#*|o cut .
diff --git a/comm.c b/comm.c
index 4648d7d..b68f02d 100644
--- a/comm.c
+++ b/comm.c
_AT_@ -3,12 +3,13 @@
#include <stdlib.h>
#include <string.h>
+#include "text.h"
#include "util.h"
static int show = 0x07;
static void
-printline(int pos, char *line)
+printline(int pos, struct linebufline *line)
{
int i;
_AT_@ -19,7 +20,7 @@ printline(int pos, char *line)
if (show & (0x1 << i))
putchar('\t');
}
- fputs(line, stdout);
+ fwrite(line->data, 1, line->len, stdout);
}
static void
_AT_@ -32,9 +33,10 @@ int
main(int argc, char *argv[])
{
FILE *fp[2];
- size_t linelen[2] = { 0, 0 };
+ static struct linebufline line[2];
+ size_t linecap[2] = { 0, 0 };
+ ssize_t len;
int ret = 0, i, diff = 0, seenline = 0;
- char *line[2] = { NULL, NULL };
ARGBEGIN {
case '1':
_AT_@ -62,24 +64,32 @@ main(int argc, char *argv[])
for (i = 0; i < 2; i++) {
if (diff && i == (diff < 0))
continue;
- if (getline(&line[i], &linelen[i], fp[i]) > 0) {
+ if ((len = getline(&(line[i].data), &linecap[i],
+ fp[i])) > 0) {
+ line[i].len = len;
seenline = 1;
continue;
}
if (ferror(fp[i]))
eprintf("getline %s:", argv[i]);
- if ((diff || seenline) && line[!i][0])
- printline(!i, line[!i]);
- while (getline(&line[!i], &linelen[!i], fp[!i]) > 0)
- printline(!i, line[!i]);
+ if ((diff || seenline) && line[!i].data[0])
+ printline(!i, &line[!i]);
+ while ((len = getline(&(line[!i].data), &linecap[!i],
+ fp[!i])) > 0) {
+ line[!i].len = len;
+ printline(!i, &line[!i]);
+ }
if (ferror(fp[!i]))
eprintf("getline %s:", argv[!i]);
goto end;
}
- diff = strcmp(line[0], line[1]);
+ if (!(diff = memcmp(line[0].data, line[1].data,
+ MIN(line[0].len, line[1].len)))) {
+ diff = (line[0].len > line[1].len);
+ }
LIMIT(diff, -1, 1);
seenline = 0;
- printline((2 - diff) % 3, line[MAX(0, diff)]);
+ printline((2 - diff) % 3, &line[MAX(0, diff)]);
}
end:
ret |= fshut(fp[0], argv[0]);
Received on Thu Mar 10 2016 - 09:48:17 CET
This archive was generated by hypermail 2.3.0
: Thu Mar 10 2016 - 10:00:30 CET