From 7928b0d28b7e8fe8b7cb7eeb5ddde4d4840db9e4 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 16 Nov 2014 14:21:42 -0600 Subject: Fix sed 'b' with no label and 'N' in general. --- toys/pending/sed.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/toys/pending/sed.c b/toys/pending/sed.c index d7987366..7a9500c4 100644 --- a/toys/pending/sed.c +++ b/toys/pending/sed.c @@ -3,6 +3,9 @@ * Copyright 2014 Rob Landley * * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html + * + * TODO: lines > 2G could signed int wrap length counters. Not just getline() + * but N and s/// USE_SED(NEWTOY(sed, "(version)e*f*inr", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LOCALE)) @@ -335,10 +338,8 @@ static void walk_pattern(char **pline, long plen) if (c != 'b') tea = 0; if (c=='b' || t^(c=='T')) { + if (!logrus->arg1) break; str = logrus->arg1+(char *)logrus; - - - if (!*str) break; for (logrus = (void *)TT.pattern; logrus; logrus = logrus->next) if (logrus->c == ':' && !strcmp(logrus->arg1+(char *)logrus, str)) break; @@ -388,14 +389,18 @@ static void walk_pattern(char **pline, long plen) break; } else if (c=='N') { + // Can't just grab next line because we could have multiple N and + // we need to actually read ahead to get N;$p EOF detection right. if (pline) { TT.restart = logrus->next; - extend_string(&line, TT.nextline, plen, -TT.nextlen); + extend_string(&line, TT.nextline, len, -TT.nextlen); free(TT.nextline); TT.nextline = line; + TT.nextlen += len + 1; line = 0; } + // Pending append goes out right after N goto done; } else if (c=='p') { if (emit(line, len, eol)) break; -- cgit v1.2.3