From 2eb2fdea88b848d07bb8aa7ca22cb624127e440d Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 9 Dec 2018 19:02:32 -0600 Subject: Fix first grep.test failure (-B + -b not producing middle field). When necessary, realloc() the line to add 4 aligned bytes of storage at the end, stick the unsigned offset in there, and then fish it back out for display (and add 1 because offset is 0 based and display is 1 based). --- toys/posix/grep.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'toys/posix/grep.c') diff --git a/toys/posix/grep.c b/toys/posix/grep.c index b2394c7b..49c69dd0 100644 --- a/toys/posix/grep.c +++ b/toys/posix/grep.c @@ -234,7 +234,8 @@ static void do_grep(int fd, char *name) while (dlb) { struct double_list *dl = dlist_pop(&dlb); - outline(dl->data, '-', name, lcount-before, 0, -1); + outline(dl->data, '-', name, lcount-before, + *(unsigned *)(dl->data+((strlen(dl->data)+1)|3)+1)+1, -1); free(dl->data); free(dl); before--; @@ -260,6 +261,10 @@ static void do_grep(int fd, char *name) discard = 0; } if (discard && TT.B) { + if (FLAG(b)) { + line = xrealloc(line, (ulen|3)+4); + *(unsigned *)(line+(ulen|3)+1) = offset-len; + } dlist_add(&dlb, line); line = 0; if (++before>TT.B) { -- cgit v1.2.3