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 Free Software Foundation
7 # Copyright (C) 1993 Jean-loup Gailly
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 PATH="BINDIR:$PATH"; export PATH
25 prog=`echo "$0" | sed 's|.*/||'`
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
126 *'&'*) char='&' repl='\&';;
127 *'\'*) char='\\' repl='\\';;
128 *'|'*) char='|' repl='\|';;
131 up_to_first_char="\\([^$char]*\\)"
132 after_first_char="[^$char]*$char\\(.*\\)"
133 escaped=$escaped`expr "X$i" : "X$up_to_first_char"`$repl
134 i=`expr "X$i" : "$after_first_char"`
136 if test $with_filename -eq 1; then
137 sed_script="s|[^:]*|$escaped$i|"
139 sed_script="s|^|$escaped$i:|"
142 # Fail if either grep or sed fails.
143 # Bash has ${PIPESTATUS[0]}, but that's not portable.
147 ($grep $opt "$pat" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
152 test $res -lt $r && res=$r