* zdiff.in: Test /dev//fd/3, not /dev/fd/3, to work around a problem
[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
18 version="z$prog (gzip) @VERSION@
19 Copyright (C) 2006 Free Software Foundation, Inc.
20 This is free software.  You may redistribute copies of it under the terms of
21 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
22 There is NO WARRANTY, to the extent permitted by law.
23
24 Written by Jean-loup Gailly."
25
26 usage="Usage: z$prog [OPTION]... FILE1 [FILE2]
27 Compare FILE1 to FILE2, using their uncompressed contents if they are
28 compressed.  If FILE2 is omitted, compare FILE1 to the uncompressed
29 contents of FILE1.gz.  Do comparisons like '$prog' does.
30
31 OPTIONs are the same as for '$prog'.
32
33 Report bugs to <bug-gzip@gnu.org>."
34
35 FILES=
36 while :; do
37   case $1 in
38   --h*) echo "$usage" || exit 2; exit;;
39   --v*) echo "$version" || exit 2; exit;;
40   --) shift; break;;
41   -*\'*) echo >&2 "$prog: $1: option contains apostrophe"; exit 2;;
42   -?*) cmp="$cmp '$1'"; shift;;
43   *) break;;
44   esac
45 done
46 cmp="$cmp --"
47
48 for file; do
49   test "X$file" = X- || <"$file" || exit 2
50 done
51
52 if test $# -eq 1; then
53   case $1 in
54   *[-.]gz* | *[-.][zZ] | *.t[ga]z)
55     FILE=`expr "X$1" : 'X\(.*\)[-.][zZtga]*$'`
56     gzip -cd -- "$1" | eval "$cmp" - '"$FILE"';;
57   *)
58     echo >&2 "$prog: $1: unknown compressed file extension"
59     exit 2;;
60   esac
61 elif test $# -eq 2; then
62         case "$1" in
63         *[-.]gz* | *[-.][zZ] | *.t[ga]z | -)
64                 case "$2" in
65                 *[-.]gz* | *[-.][zZ] | *.t[ga]z | -)
66                     if test "$1$2" = --; then
67                         gzip -cdfq - | eval "$cmp" - -
68                     # The extra slash in /dev//fd works around a problem with
69                     # Tru64 4.0F ksh M-11/16/88f.
70                     elif test -r /dev//fd/3 3</dev/null; then
71                         gzip -cdfq -- "$1" |
72                           (gzip -cdfq -- "$2" |
73                            eval "$cmp" /dev/fd/3 -) 3<&0
74                     else
75                         F=`expr "/$2" : '.*/\(.*\)[-.][zZtga]*$'` || F=$prog
76                         tmp=
77                         trap '
78                           test -n "$tmp" && rm -f "$tmp"
79                           (exit 2); exit 2
80                         ' HUP INT PIPE TERM 0
81                         if type mktemp >/dev/null 2>&1; then
82                           tmp=`mktemp -t -- "$F.XXXXXX"` || exit
83                         else
84                           set -C
85                           tmp=${TMPDIR-/tmp}/$F.$$
86                         fi
87                         gzip -cdfq -- "$2" > "$tmp" || exit
88                         gzip -cdfq -- "$1" | eval "$cmp" - '"$tmp"'
89                         STAT="$?"
90                         rm -f "$tmp" || STAT=2
91                         trap - HUP INT PIPE TERM 0
92                         exit $STAT
93                     fi;;
94                 *)      gzip -cdfq -- "$1" | eval "$cmp" - '"$2"';;
95                 esac;;
96         *)      case "$2" in
97                 *[-.]gz* | *[-.][zZ] | *.t[ga]z | -)
98                         gzip -cdfq -- "$2" | eval "$cmp" '"$1"' -;;
99                 *)      eval "$cmp" '"$1"' '"$2"';;
100                 esac;;
101         esac
102 else
103         echo >&2 "$usage"
104         exit 2
105 fi