sbin/mount_hammer: Use calloc(3) and cleanups
[dragonfly.git] / usr.bin / gzip / zgrep
blob157d6821b2aa3826c81dae9a6bca50d13b0a8f01
1 #!/bin/sh
3 # $NetBSD: zgrep,v 1.7 2008/05/08 15:35:23 wiz Exp $
4 # $DragonFly: src/usr.bin/gzip/zgrep,v 1.1 2004/10/26 11:19:31 joerg Exp $
6 # Copyright (c) 2003 Thomas Klausner.
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
17 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 grep=/usr/bin/grep
29 zcat=/usr/bin/zcat
31 endofopts=0
32 pattern_found=0
33 grep_args=""
34 hyphen=0
35 silent=0
37 prg=$0
39 # handle being called 'zegrep' or 'zfgrep'
40 case ${prg} in
41 *zegrep)
42 grep_args="-E";;
43 *zfgrep)
44 grep_args="-F";;
45 esac
47 # skip all options and pass them on to grep taking care of options
48 # with arguments, and if -e was supplied
50 while [ $# -gt 0 -a ${endofopts} -eq 0 ]
52 case $1 in
53 # from GNU grep-2.5.1 -- keep in sync!
54 -[ABCDXdefm])
55 if [ $# -lt 2 ]
56 then
57 echo "${prg}: missing argument for $1 flag" >&2
58 exit 1
60 case $1 in
61 -e)
62 pattern="$2"
63 pattern_found=1
64 shift 2
65 break
69 esac
70 grep_args="${grep_args} $1 $2"
71 shift 2
73 --)
74 shift
75 endofopts=1
78 hyphen=1
79 shift
81 -h)
82 silent=1
83 shift
85 -*)
86 grep_args="${grep_args} $1"
87 shift
90 # pattern to grep for
91 endofopts=1
93 esac
94 done
96 # if no -e option was found, take next argument as grep-pattern
97 if [ ${pattern_found} -lt 1 ]
98 then
99 if [ $# -ge 1 ]; then
100 pattern="$1"
101 shift
102 elif [ ${hyphen} -gt 0 ]; then
103 pattern="-"
104 else
105 echo "${prg}: missing pattern" >&2
106 exit 1
110 # call grep ...
111 if [ $# -lt 1 ]
112 then
113 # ... on stdin
114 ${zcat} -fq - | ${grep} ${grep_args} -- "${pattern}" -
115 else
116 # ... on all files given on the command line
117 if [ ${silent} -lt 1 ]; then
118 grep_args="-H ${grep_args}"
120 while [ $# -gt 0 ]
122 ${zcat} -fq -- "$1" | ${grep} --label="${1}" ${grep_args} -- "${pattern}" -
123 shift
124 done
127 exit 0