Resurrect z*grep script to support gzip'ped files.
[dragonfly.git] / usr.bin / gzip / zdiff
1 #!/bin/sh -
2 #
3 # $NetBSD: zdiff,v 1.2 2003/12/28 12:43:43 wiz Exp $
4 # $DragonFly: src/usr.bin/gzip/zdiff,v 1.1 2004/10/26 11:19:31 joerg Exp $
5 # $OpenBSD: zdiff,v 1.2 2003/07/29 07:42:44 otto Exp $
6 #
7 # Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
8 #
9 # Permission to use, copy, modify, and distribute this software for any
10 # purpose with or without fee is hereby granted, provided that the above
11 # copyright notice and this permission notice appear in all copies.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #
21 # Sponsored in part by the Defense Advanced Research Projects
22 # Agency (DARPA) and Air Force Research Laboratory, Air Force
23 # Materiel Command, USAF, under agreement number F39502-99-1-0512.
24 #
25
26 # Set $prog based on $0
27 case $0 in
28         *cmp)   prog=cmp
29                 ;;
30         *)      prog=diff
31                 ;;
32 esac
33 USAGE="usage: z$prog [options] file1 [file2]"
34
35 # Pull out any command line flags so we can pass them to diff/cmp
36 # XXX - assumes there is no optarg
37 flags=
38 while test $# -ne 0; do
39         case "$1" in
40                 --)
41                         shift
42                         break
43                         ;;
44                 -*)
45                         flags="$flags $1"
46                         shift
47                         ;;
48                 *)
49                         break
50                         ;;
51         esac
52 done
53
54 if [ $# -eq 1 ]; then
55         # One file given, compare compressed to uncompressed
56         files="$1"
57         case "$1" in
58                 *[._-][Zz])
59                         files="${1%??}"
60                         ;;
61                 *[._-]gz)
62                         files="${1%???}"
63                         ;;
64                 *.t[ag]z)
65                         files="${1%??}"ar
66                         ;;
67                 *)      echo "z$prog: unknown suffix" 1>&2
68                         exit 1
69         esac
70         gzip -cdfq "$1" | $prog $flags - "$files"
71         status=$?
72 elif [ $# -eq 2 ]; then
73         # Two files given, compare the two uncompressing as needed
74         case "$1" in
75                 *[._-][Zz]|*[._-]gz|*.t[ag]z)
76                         files=-
77                         filt="gzip -cdfq $1"
78                         ;;
79                 *)
80                         files="$1"
81                         ;;
82         esac
83         case "$2" in
84                 *[._-][Zz]|*[._-]gz|*.t[ag]z)
85                         if [ "$files" = "-" ]; then
86                                 tmp=`mktemp -t z$prog.XXXXXXXXXX` || exit 1
87                                 trap "rm -f $tmp" 0 1 2 3 13 15
88                                 gzip -cdfq "$2" > $tmp
89                                 files="$files $tmp"
90                         else
91                                 files="$files -"
92                                 filt="gzip -cdfq $2"
93                         fi
94                         ;;
95                 *)
96                         files="$files $2"
97                         ;;
98         esac
99         if [ -n "$filt" ]; then
100                 $filt | $prog $flags $files
101         else
102                 $prog $flags $files
103         fi
104         status=$?
105 else
106         echo "$USAGE" 1>&2
107         exit 1
108 fi
109
110 exit $status