3 # zgrep -- a wrapper around a grep program that decompresses files as needed
4 # Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>
6 # Copyright (C) 1998, 2001-2002, 2006-2007, 2009-2022 Free Software Foundation,
9 # Copyright (C) 1993 Jean-loup Gailly
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License along
22 # with this program; if not, write to the Free Software Foundation, Inc.,
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 grep='${GREP-'\''@GREP@'\''}'
27 version
='zgrep (gzip) @VERSION@
28 Copyright (C) 2010-2018 Free Software Foundation, Inc.
29 This is free software. You may redistribute copies of it under the terms of
30 the GNU General Public License <https://www.gnu.org/licenses/gpl.html>.
31 There is NO WARRANTY, to the extent permitted by law.
33 Written by Jean-loup Gailly.'
35 usage
="Usage: $0 [OPTION]... [-e] PATTERN [FILE]...
36 Look for instances of PATTERN in the input FILEs, using their
37 uncompressed contents if they are compressed.
39 OPTIONs are the same as for 'grep', except that the following 'grep'
40 options are not supported: --dereference-recursive (-R), --directories (-d),
41 --exclude, --exclude-from, --exclude-dir, --include, --null (-Z),
42 --null-data (-z), and --recursive (-r).
44 Report bugs to <bug-gzip@gnu.org>."
46 # sed script to escape all ' for the shell, and then (to handle trailing
47 # newlines correctly) append ' to the last line.
49 s/'\''/'\''\\'\'''\''/g
55 files_without_matches
=0
60 while test $# -ne 0; do
66 (-[0123456789EFGHIKLPRTUVZabchilnoqrsuvwxyz
]*[!0123456789]*)
67 arg2
=-\'$
(expr "X$option" : 'X-.[0-9]*\(.*\)' |
sed "$escape")
68 eval "set -- $arg2 "'${1+"$@"}'
69 option
=$
(expr "X$option" : 'X\(-.[0-9]*\)');;
70 (--binary-*=* |
--[lm
]a
*=* |
--reg*=*)
72 (-[ABCDXdefm
] |
--binary-* |
--file |
--[lm
]a
* |
--reg*)
73 case ${1?"$option option requires an argument"} in
75 optarg
=" '"$
(printf '%s\n' "$1" |
sed "$escape");;
81 optarg
=" '"$
(expr "X$option" : 'X-f\(.*\)' |
sed "$escape")
84 optarg
=" '"$
(expr "X$option" : 'X-f\(.*\)')\'
87 optarg
=" '"$
(expr "X$option" : 'X--file=\(.*\)' |
sed "$escape")
90 optarg
=" '"$
(expr "X$option" : 'X--file=\(.*\)')\'
99 operands
="$operands '"$
(printf '%s\n' "$option" |
sed "$escape");;
101 operands
="$operands '$option'";;
103 ${POSIXLY_CORRECT+break}
108 (-[drRzZ
] |
--di* |
--exc* |
--inc* |
--rec* |
--nu*)
109 printf >&2 '%s: %s: option not supported\n' "$0" "$option"
114 # The pattern is coming from a file rather than the command-line.
115 # If the file is actually stdin then we need to do a little
116 # magic, since we use stdin to pass the gzip output to grep.
117 # Similarly if it is not a regular file, since it might be read repeatedly.
118 # In either of these two cases, copy the pattern into a temporary file,
119 # and use that file instead. The pattern might contain null bytes,
120 # so we cannot simply switch to -e here.
122 (" '-'" |
" '/dev/stdin'" |
" '/dev/fd/0'")
125 eval "test ! -f$optarg";;
128 if test -n "$pattmp"; then
129 eval "cat --$optarg" >>"$pattmp" ||
exit 2
133 test -n "$pattmp" && rm -f "$pattmp"
135 ' HUP INT PIPE TERM
0
138 /*) TMPDIR
=$TMPDIR/;;
141 if type mktemp
>/dev
/null
2>&1; then
142 pattmp
=$
(mktemp
"${TMPDIR}zgrepXXXXXXXXX") ||
exit 2
145 pattmp
=${TMPDIR}zgrep$$
147 eval "cat --$optarg" >"$pattmp" ||
exit 2
151 (--h |
--he |
--hel |
--help)
152 printf '%s\n' "$usage" ||
exit 2
154 (-H |
--wi |
--wit |
--with |
--with- |
--with-f |
--with-fi \
155 |
--with-fil |
--with-file |
--with-filen |
--with-filena |
--with-filenam \
159 (-l |
--files-with-*)
160 files_with_matches
=1;;
161 (-L |
--files-witho*)
162 files_without_matches
=1;;
165 (-V |
--v |
--ve |
--ver |
--vers |
--versi |
--versio |
--version)
166 printf '%s\n' "$version" ||
exit 2
172 option
=\'$
(printf '%s\n' "$option" |
sed "$escape");;
177 grep="$grep $option$optarg"
180 eval "set -- $operands "'${1+"$@"}'
182 if test $have_pat -eq 0; then
183 case ${1?"missing pattern; try \`$0 --help' for help"} in
185 grep="$grep -- '"$
(printf '%s\n' "$1" |
sed "$escape");;
187 grep="$grep -- '$1'";;
192 if test $# -eq 0; then
201 # Fail if gzip or grep (or sed) fails.
204 ('gzip' -cdfq -- "$i" 5>&-; echo $?
>&5) 3>&- |
205 if test $files_with_matches -eq 1; then
206 eval "$grep" >/dev
/null
&& { printf '%s\n' "$i" ||
exit 2; }
207 elif test $files_without_matches -eq 1; then
208 eval "$grep" >/dev
/null ||
{
210 if test $r -eq 1; then
211 printf '%s\n' "$i" || r
=2
213 test 256 -le $r && r
=$
(expr 128 + $r % 128)
216 elif test $with_filename -eq 0 &&
217 { test $# -eq 1 ||
test $no_filename -eq 1; }; then
222 '* |
*'&'* |
*'\'* | *'|
'*)
223 icolon=$(printf '%s
\n' "$i:" |
224 sed -e 's
/[&\|
]/\\&/g
' -e '$
!s
/$
/\\/');;
227 sed_script="s|^|$icolon|"
229 # Fail if grep or sed fails.
232 (eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
233 ) || { r=$?; test $r -lt 2 && r=2; }
234 test 256 -le $r && r=$(expr 128 + $r % 128)
240 # Ignore gzip status 2, as it is just a warning.
241 # gzip status 1 is an error, like grep status 2.
242 test $gzip_status -eq 2 && gzip_status=0
243 test $gzip_status -eq 1 && gzip_status=2
245 # Use the more serious of the grep and gzip statuses.
246 test $r -lt $gzip_status && r=$gzip_status
248 # Accumulate the greatest status, except consider 0 to be greater than 1.
249 if test $r -le 1 && test $res -le 1; then
255 # Exit immediately on a serious error.
256 test 126 -le $res && break
259 if test -n "$pattmp"; then
263 test $res -lt $r && res=$r
265 trap - HUP INT PIPE TERM 0
268 test 128 -le $res && kill -$(expr $res % 128) $$