sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit eebba22577d649952ff4b4e456a0dc2e26811171
parent 691c54750f707a757630bab0a17b7c13ff9e2efa
Author: FRIGN <dev@frign.de>
Date:   Mon,  7 Mar 2016 01:42:01 +0100

Support NUL containing lines in comm(1)

Diffstat:
MREADME | 2+-
Mcomm.c | 32+++++++++++++++++++++-----------
2 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/README b/README @@ -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 @@ -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]);