From 5147183a217a9dc9325764fba176d9db52d6e474 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 13 Sep 2022 08:02:03 -0500 Subject: [PATCH] Fix sed -z and add test cases. --- tests/sed.test | 5 ++++- toys/posix/sed.c | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/sed.test b/tests/sed.test index 5035478b..91f6b6e3 100755 --- a/tests/sed.test +++ b/tests/sed.test @@ -175,7 +175,10 @@ testing "bonus backslashes" \ testing "end b with }" "sed -n '/START/{:a;n;/END/q;p;ba}'" "b\nc\n" \ "" "a\nSTART\nb\nc\nEND\nd" -testing '-z' 'sed -z "s/\n/-/g"' "a-b-c" "" "a\nb\nc" +testcmd '-z' '-z "s/\n/-/g"' "a-b-c" "" "a\nb\nc" +testcmd '-z N' '-z N' 'one\0two\0' '' 'one\0two\0' +testcmd 'p noeol' '-z p' 'one\0one' '' 'one' +testcmd '-z N noeol' '-z N' 'one\0two' '' 'one\0two' # toybox handling of empty capturing groups broke minjail. Check that we # correctly replace an empty capturing group with the empty string: diff --git a/toys/posix/sed.c b/toys/posix/sed.c index 3b3e4c31..471c6436 100644 --- a/toys/posix/sed.c +++ b/toys/posix/sed.c @@ -157,9 +157,9 @@ static int emit(char *line, long len, int eol) { int l, old = line[len]; - if (TT.noeol && !writeall(TT.fdout, "\n", 1)) return 1; + if (TT.noeol && !writeall(TT.fdout, &TT.delim, 1)) return 1; TT.noeol = !eol; - if (eol) line[len++] = '\n'; + if (eol) line[len++] = TT.delim; if (!len) return 0; l = writeall(TT.fdout, line, len); if (eol) line[len-1] = old; @@ -181,7 +181,7 @@ static char *extend_string(char **old, char *new, int oldlen, int newlen) if (newline) newlen = -newlen; s = *old = xrealloc(*old, oldlen+newlen+newline+1); - if (newline) s[oldlen++] = '\n'; + if (newline) s[oldlen++] = TT.delim; memcpy(s+oldlen, new, newlen); s[oldlen+newlen] = 0; @@ -227,7 +227,7 @@ static void sed_line(char **pline, long plen) } if (!line || !len) return; - if (line[len-1] == '\n') line[--len] = eol++; + if (line[len-1] == TT.delim) line[--len] = eol++; TT.count++; // The restart-1 is because we added one to make sure it wasn't NULL, -- 2.39.2