From 5ed3c33417d35be2bfc2336c2b29bf1f1bc8509e Mon Sep 17 00:00:00 2001 From: Daniel Mentz Date: Mon, 7 Nov 2022 14:26:30 -0800 Subject: [PATCH] Fix grep to not truncate last character of file provided by -f argument The command line argument -f allows a user to specify a file that lists multiple regular expressions (one per line) to match. It turned out that, if the file ended with a new line character (like most text files do), then the character just prior to that newline character got truncated. For example, if the file was just 4 bytes long and contained "abc\n", then the character "c" would be removed. --- toys/posix/grep.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toys/posix/grep.c b/toys/posix/grep.c index 9b2e583c..5c6f5936 100644 --- a/toys/posix/grep.c +++ b/toys/posix/grep.c @@ -388,7 +388,7 @@ static void parse_regex(void) if (!*(s = xreadfile(al->arg, 0, 0))) { free(s); s = 0; - } else if (*(ss = s+strlen(s)-1)=='\n') *--ss = 0; + } else if (*(ss = s+strlen(s)-1)=='\n') *ss = 0; } else s = al->arg; // Advance, when we run out of -f switch to -e. -- 2.39.2