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