Support NUL containing lines in comm(1)

This commit is contained in:
FRIGN 2016-03-07 01:42:01 +01:00 committed by sin
parent 691c54750f
commit eebba22577
2 changed files with 22 additions and 12 deletions

2
README
View File

@ -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 .

32
comm.c
View File

@ -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;
@ -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
@ -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':
@ -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]);