patch to improve behavior of zdiff when file is compressed without extension
[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 # Copyright (C) 1998, 2002, 2006, 2007, 2009 Free Software Foundation
5 # Copyright (C) 1993 Jean-loup Gailly
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 bindir=@bindir@
22 case $1 in
23 --__bindir) bindir=${2?}; shift; shift;;
24 esac
25 PATH=$bindir:$PATH
26
27 case $1 in
28   --__cmp) shift
29         prog=cmp;  cmp='${CMP-cmp}'  ;;
30   *)    prog=diff; cmp='${DIFF-diff}';;
31 esac
32
33 version="z$prog (gzip) @VERSION@
34 Copyright (C) 2007, 2009-2010 Free Software Foundation, Inc.
35 This is free software.  You may redistribute copies of it under the terms of
36 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
37 There is NO WARRANTY, to the extent permitted by law.
38
39 Written by Jean-loup Gailly."
40
41 usage="Usage: $0 [OPTION]... FILE1 [FILE2]
42 Compare FILE1 to FILE2, using their uncompressed contents if they are
43 compressed.  If FILE2 is omitted, compare FILE1 to the uncompressed
44 contents of FILE1.gz.  Do comparisons like '$prog' does.
45
46 OPTIONs are the same as for '$prog'.
47
48 Report bugs to <bug-gzip@gnu.org>."
49
50 # sed script to escape all ' for the shell, and then (to handle trailing
51 # newlines correctly) turn trailing X on last line into '.
52 escape='
53   s/'\''/'\''\\'\'''\''/g
54   $s/X$/'\''/
55 '
56
57 while :; do
58   case $1 in
59   --h*) printf '%s\n' "$usage" || exit 2; exit;;
60   --v*) echo "$version" || exit 2; exit;;
61   --) shift; break;;
62   -*\'*) cmp="$cmp '"`printf '%sX\n' "$1" | sed "$escape"`;;
63   -?*) cmp="$cmp '$1'";;
64   *) break;;
65   esac
66   shift
67 done
68 cmp="$cmp --"
69
70 for file
71 do
72   test "X$file" = X- || <"$file" || exit 2
73 done
74
75 gzip_status=0
76 exec 3>&1
77
78 if test $# -eq 1; then
79   type_file=$(file -b --mime-type "$1")
80
81   case $type_file in
82   application/x-gzip | application/x-compress)
83     FILE=`expr "X$1" : 'X\(.*\)[-.][zZtga]*$'`
84     if test "$FILE" == ""; then
85         FILE="$1"
86     fi;
87     
88     gzip_status=$(
89       exec 4>&1
90       (gzip -cd -- "$1" 4>&-; echo $? >&4) 3>&- | eval "$cmp" - '"$FILE"' >&3
91     );;
92   *)
93     echo >&2 "$0: $1: unknown compressed file extension"
94     exit 2;;
95   esac
96 elif test $# -eq 2; then
97         type_file1=$(file -b --mime-type "$1")
98         type_file2=$(file -b --mime-type "$2")
99         
100         case "$type_file1" in
101         application/x-gzip | application/x-compress)
102                 case "$type_file2" in
103                 application/x-gzip | application/x-compress)
104                     if test "$1$2" = --; then
105                         gzip_status=$(
106                           exec 4>&1
107                           (gzip -cdfq - 4>&-; echo $? >&4) 3>&- |
108                             eval "$cmp" - - >&3
109                         )
110                     elif
111                         # Reject Solaris 8's buggy /bin/bash 2.03.
112                         echo X |
113                          (echo X | eval "$cmp" /dev/fd/5 - >/dev/null 2>&1) \
114                                 5<&0
115                     then
116                         gzip_status=$(
117                           exec 4>&1
118                           (gzip -cdfq -- "$1" 4>&-; echo $? >&4) 3>&- |
119                             ( (gzip -cdfq -- "$2" 4>&-; echo $? >&4) 3>&- 5<&- </dev/null |
120                                eval "$cmp" /dev/fd/5 - >&3) 5<&0
121                         )
122                         cmp_status=$?
123                         case $gzip_status in
124                           *[1-9]*) gzip_status=1;;
125                           *) gzip_status=0;;
126                         esac
127                         (exit $cmp_status)
128                     else
129                         F=`expr "/$2" : '.*/\(.*\)[-.][zZtga]*$'` || F=$prog
130                         if test "$F" == ""; then
131                             F="$2"
132                         fi;
133                         tmp=
134                         trap '
135                           test -n "$tmp" && rm -f "$tmp"
136                           (exit 2); exit 2
137                         ' HUP INT PIPE TERM 0
138                         if type mktemp >/dev/null 2>&1; then
139                           tmp=`mktemp -t -- "$F.XXXXXX"` || exit 2
140                         else
141                           set -C
142                           tmp=${TMPDIR-/tmp}/$F.$$
143                         fi
144                         gzip -cdfq -- "$2" > "$tmp" || exit 2
145                         gzip_status=$(
146                           exec 4>&1
147                           (gzip -cdfq -- "$1" 4>&-; echo $? >&4) 3>&- |
148                             eval "$cmp" - '"$tmp"' >&3
149                         )
150                         cmp_status=$?
151                         rm -f "$tmp" || gzip_status=$?
152                         trap - HUP INT PIPE TERM 0
153                         (exit $cmp_status)
154                     fi;;
155                 *)
156                     gzip_status=$(
157                       exec 4>&1
158                       (gzip -cdfq -- "$1" 4>&-; echo $? >&4) 3>&- |
159                         eval "$cmp" - '"$2"' >&3
160                     );;
161                 esac;;
162         *)      case "$type_file2" in
163                 application/x-gzip | application/x-compress)
164                         gzip_status=$(
165                           exec 4>&1
166                           (gzip -cdfq -- "$2" 4>&-; echo $? >&4) 3>&- |
167                             eval "$cmp" '"$1"' - >&3
168                         );;
169                 *)      eval "$cmp" '"$1"' '"$2"';;
170                 esac;;
171         esac
172 else
173         echo >&2 "$0: invalid number of operands; try \`$0 --help' for help"
174         exit 2
175 fi
176
177 cmp_status=$?
178 test "$gzip_status" -eq 0 || exit 2
179 exit $cmp_status