* zdiff.in (cmp0): New var.
[debian/gzip] / zdiff.in
1 #!/bin/sh
2 # sh is buggy on RS/6000 AIX 3.2. Replace above line with #!/bin/ksh
3
4 # Zcmp and zdiff are used to invoke the cmp or the  diff  pro-
5 # gram  on compressed files.  All options specified are passed
6 # directly to cmp or diff.  If only 1 file is specified,  then
7 # the  files  compared  are file1 and an uncompressed file1.gz.
8 # If two files are specified, then they are  uncompressed  (if
9 # necessary) and fed to cmp or diff.  The exit status from cmp
10 # or diff is preserved.
11
12 PATH="BINDIR:$PATH"; export PATH
13 case "$0" in
14   *cmp) prog=cmp ; cmp='${CMP-cmp}'  ;;
15   *)    prog=diff; cmp='${DIFF-diff}';;
16 esac
17 cmp0=$cmp
18
19 version="z$prog (gzip) @VERSION@
20 Copyright (C) 2006 Free Software Foundation, Inc.
21 This is free software.  You may redistribute copies of it under the terms of
22 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
23 There is NO WARRANTY, to the extent permitted by law.
24
25 Written by Jean-loup Gailly."
26
27 usage="Usage: z$prog [OPTION]... FILE1 [FILE2]
28 Compare FILE1 to FILE2, using their uncompressed contents if they are
29 compressed.  If FILE2 is omitted, compare FILE1 to the uncompressed
30 contents of FILE1.gz.  Do comparisons like '$prog' does.
31
32 OPTIONs are the same as for '$prog'.
33
34 Report bugs to <bug-gzip@gnu.org>."
35
36 FILES=
37 while :; do
38   case $1 in
39   --h*) echo "$usage" || exit 2; exit;;
40   --v*) echo "$version" || exit 2; exit;;
41   --) shift; break;;
42   -*\'*) echo >&2 "$prog: $1: option contains apostrophe"; exit 2;;
43   -?*) cmp="$cmp '$1'"; shift;;
44   *) break;;
45   esac
46 done
47 cmp="$cmp --"
48
49 for file
50 do
51   test "X$file" = X- || <"$file" || exit 2
52 done
53
54 if test $# -eq 1; then
55   case $1 in
56   *[-.]gz* | *[-.][zZ] | *.t[ga]z)
57     FILE=`expr "X$1" : 'X\(.*\)[-.][zZtga]*$'`
58     gzip -cd -- "$1" | eval "$cmp" - '"$FILE"';;
59   *)
60     echo >&2 "$prog: $1: unknown compressed file extension"
61     exit 2;;
62   esac
63 elif test $# -eq 2; then
64         case "$1" in
65         *[-.]gz* | *[-.][zZ] | *.t[ga]z | -)
66                 case "$2" in
67                 *[-.]gz* | *[-.][zZ] | *.t[ga]z | -)
68                     if test "$1$2" = --; then
69                         gzip -cdfq - | eval "$cmp" - -
70                     elif
71                         # Reject Solaris 8's buggy /bin/bash 2.03.
72                         echo X |
73                          (echo X | eval "$cmp0" /dev/fd/3 - >/dev/null 2>&1) \
74                                 3<&0
75                     then
76                         gzip -cdfq -- "$1" |
77                           (gzip -cdfq -- "$2" |
78                            eval "$cmp" /dev/fd/3 -) 3<&0
79                     else
80                         F=`expr "/$2" : '.*/\(.*\)[-.][zZtga]*$'` || F=$prog
81                         tmp=
82                         trap '
83                           test -n "$tmp" && rm -f "$tmp"
84                           (exit 2); exit 2
85                         ' HUP INT PIPE TERM 0
86                         if type mktemp >/dev/null 2>&1; then
87                           tmp=`mktemp -t -- "$F.XXXXXX"` || exit
88                         else
89                           set -C
90                           tmp=${TMPDIR-/tmp}/$F.$$
91                         fi
92                         gzip -cdfq -- "$2" > "$tmp" || exit
93                         gzip -cdfq -- "$1" | eval "$cmp" - '"$tmp"'
94                         STAT="$?"
95                         rm -f "$tmp" || STAT=2
96                         trap - HUP INT PIPE TERM 0
97                         exit $STAT
98                     fi;;
99                 *)      gzip -cdfq -- "$1" | eval "$cmp" - '"$2"';;
100                 esac;;
101         *)      case "$2" in
102                 *[-.]gz* | *[-.][zZ] | *.t[ga]z | -)
103                         gzip -cdfq -- "$2" | eval "$cmp" '"$1"' -;;
104                 *)      eval "$cmp" '"$1"' '"$2"';;
105                 esac;;
106         esac
107 else
108         echo >&2 "$usage"
109         exit 2
110 fi