From 3ebb829683aab54f5089a878120294ae9e5d2fcf Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 16 Mar 2016 11:36:45 +0000 Subject: sed: drop \r when reading input An upstream bug (https://bugs.busybox.net/show_bug.cgi?id=8791) reports: $ cat myfile a b c $ sed "s/\(.*\)/\1\1/" myfile a b c it should be: $ sed "s/\(.*\)/\1\1/" myfile aa bb cc This happened because busybox-w32 opens files in binary mode. Lines read by sed had trailing LFs removed but not trailing CRs. The CRs ended up in the matched strings and were output, thus giving the appearance that only one of the backreferences was printed. The same happens on Linux when a DOS file is processed by BusyBox sed or GNU sed. However, this behaviour is arguably incorrect on Windows. I've modified busybox-w32 to drop trailing CRs as well as LFs from input lines. --- editors/sed.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/editors/sed.c b/editors/sed.c index 4c7f75521..a0c713f58 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -990,6 +990,11 @@ static char *get_next_line(char *gets_char, char *last_puts_char, char last_gets char c = temp[len-1]; if (c == '\n' || c == '\0') { temp[len-1] = '\0'; +#if ENABLE_PLATFORM_MINGW32 + if (c == '\n' && len > 1 && temp[len-2] == '\r') { + temp[len-2] = '\0'; + } +#endif gc = c; if (c == '\0') { int ch = fgetc(fp); -- cgit v1.2.3-55-g6feb