4 # zgrep -- a wrapper around a grep program that decompresses files as needed
5 # Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>
7 # Copyright (C) 1998, 2001, 2002, 2006 Free Software Foundation
8 # Copyright (C) 1993 Jean-loup Gailly
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along
21 # with this program; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 PATH="BINDIR:$PATH"; export PATH
27 *egrep) grep=${EGREP-egrep} ;;
28 *fgrep) grep=${FGREP-fgrep} ;;
29 *) grep=${GREP-grep} ;;
32 version="z$grep (gzip) @VERSION@
33 Copyright (C) 2006 Free Software Foundation, Inc.
34 This is free software. You may redistribute copies of it under the terms of
35 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
36 There is NO WARRANTY, to the extent permitted by law.
38 Written by Jean-loup Gailly."
40 usage="Usage: z$grep [OPTION]... [-e] PATTERN [FILE]...
41 Look for instances of PATTERN in the input FILEs, using their
42 uncompressed contents if they are compressed.
44 OPTIONs are the same as for '$grep'.
46 Report bugs to <bug-gzip@gnu.org>."
51 files_without_matches=0
55 while test $# -ne 0; do
56 case "$after_dash_dash$1" in
57 --d* | --rec*) echo >&2 "$0: $1: option not supported"; exit 2;;
58 --h*) echo "$usage" || exit 2; exit;;
59 --files-with-*) files_with_matches=1;;
60 --files-witho*) files_without_matches=1;;
61 --no-f*) no_filename=1;;
62 --v*) echo "$version" || exit 2; exit;;
63 --wi*) with_filename=1;;
67 -*[dr]*) echo >&2 "$0: $1: option not supported"; exit 2;;
70 -*H*) with_filename=1;;
76 -*L*) files_without_matches=1;;
79 -*l*) files_with_matches=1;;
82 case "$after_dash_dash$1" in
83 -[ef]) opt="$opt $1"; shift; pat="$1"
84 if test "$grep" = grep; then # grep is buggy with -e on SVR4
87 -[ABCdm])opt="$opt $1 $2"; shift;;
88 --) opt="$opt $1"; after_dash_dash=1;;
90 *) if test -z "$pat"; then
99 if test -z "$pat"; then
104 if test $# -eq 0; then
105 gzip -cdfq | $grep $opt "$pat"
112 if test $files_with_matches -eq 1; then
113 $grep $opt "$pat" > /dev/null && printf '%s\n' "$i"
114 elif test $files_without_matches -eq 1; then
115 $grep $opt "$pat" > /dev/null || printf '%s\n' "$i"
116 elif test $with_filename -eq 0 && { test $# -eq 1 || test $no_filename -eq 1; }; then
119 if test $with_filename -eq 1; then
120 sed_script="s|^[^:]*:|${i}:|"
122 sed_script="s|^|${i}:|"
125 # Fail if either grep or sed fails.
126 # Bash has ${PIPESTATUS[0]}, but that's not portable.
130 ($grep $opt "$pat" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
135 test $res -lt $r && res=$r