aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2022-07-18 21:52:31 +0300
committerLasse Collin <lasse.collin@tukaani.org>2022-07-24 11:38:19 +0300
commit2c1ff2ed6b9f17b3913866a15d776ebbbeb824d2 (patch)
treeb6241dd847e7d7f217613066418bcc48bf4ae17d /src
parentxzgrep: Use -e to specify the pattern to grep. (diff)
downloadxz-2c1ff2ed6b9f17b3913866a15d776ebbbeb824d2.tar.xz
xzgrep: Use grep -H --label when available (GNU, *BSDs).
It avoids the use of sed for prefixing filenames to output lines. Using sed for that is slower and prone to security bugs so now the sed method is only used as a fallback. This also fixes an actual bug: When grepping a binary file, GNU grep nowadays prints its diagnostics to stderr instead of stdout and thus the sed-method for prefixing the filename doesn't work. So with this commit grepping binary files gives reasonable output with GNU grep now. This was inspired by zgrep but the implementation is different.
Diffstat (limited to 'src')
-rw-r--r--src/scripts/xzgrep.in21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/scripts/xzgrep.in b/src/scripts/xzgrep.in
index 559dbbc5..dd0643d6 100644
--- a/src/scripts/xzgrep.in
+++ b/src/scripts/xzgrep.in
@@ -57,6 +57,13 @@ files_without_matches=0
no_filename=0
with_filename=0
+# See if -H and --label options are supported (GNU and *BSDs).
+if test f:x = "$(eval "echo x | $grep -H --label=f x 2> /dev/null")"; then
+ grep_supports_label=1
+else
+ grep_supports_label=0
+fi
+
while test $# -ne 0; do
option=$1
shift
@@ -192,6 +199,20 @@ for i; do
elif test $with_filename -eq 0 &&
{ test $# -eq 1 || test $no_filename -eq 1; }; then
eval "$grep"
+ elif test $grep_supports_label -eq 1; then
+ # The grep implementation in use allows us to specify the filename
+ # that grep will prefix to the output lines. This is faster and
+ # less prone to security bugs than the fallback method that uses sed.
+ # This also avoids confusing output with GNU grep >= 3.5 (2020-09-27)
+ # which prints "binary file matches" to stderr instead of stdout.
+ #
+ # If reading from stdin, let grep use whatever name it prefers for
+ # stdin. With GNU grep it's a locale-specific translated string.
+ if test "x$i" = "x-"; then
+ eval "$grep -H"
+ else
+ eval "$grep -H --label \"\$i\""
+ fi
else
# Append a colon so that the last character will never be a newline
# which would otherwise get lost in shell command substitution.