delete trailing whitespace from control files
[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-2018 Free Software Foundation, Inc.
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 case $1 in
22   --__cmp) shift
23         prog=cmp;  cmp='${CMP-cmp}'  ;;
24   *)    prog=diff; cmp='${DIFF-diff}';;
25 esac
26
27 version="z$prog (gzip) @VERSION@
28 Copyright (C) 2009, 2011-2018 Free Software Foundation, Inc.
29 This is free software.  You may redistribute copies of it under the terms of
30 the GNU General Public License <https://www.gnu.org/licenses/gpl.html>.
31 There is NO WARRANTY, to the extent permitted by law.
32
33 Written by Jean-loup Gailly."
34
35 usage="Usage: $0 [OPTION]... FILE1 [FILE2]
36 Compare FILE1 to FILE2, using their uncompressed contents if they are
37 compressed.  If FILE2 is omitted, compare FILE1 to the uncompressed
38 contents of FILE1.gz.  Do comparisons like '$prog' does.
39
40 OPTIONs are the same as for '$prog'.
41
42 Report bugs to <bug-gzip@gnu.org>."
43
44 # sed script to escape all ' for the shell, and then (to handle trailing
45 # newlines correctly) turn trailing X on last line into '.
46 escape='
47   s/'\''/'\''\\'\'''\''/g
48   $s/X$/'\''/
49 '
50
51 while :; do
52   case $1 in
53   --h*) printf '%s\n' "$usage"   || exit 2; exit;;
54   --v*) printf '%s\n' "$version" || exit 2; exit;;
55   --) shift; break;;
56   -*\'*) cmp="$cmp '"`printf '%sX\n' "$1" | sed "$escape"`;;
57   -?*) cmp="$cmp '$1'";;
58   *) break;;
59   esac
60   shift
61 done
62 cmp="$cmp --"
63
64 for file
65 do
66   test "X$file" = X- || <"$file" || exit 2
67 done
68
69 gzip_status=0
70 exec 3>&1
71
72 if test $# -eq 1; then
73   case $1 in
74   *[-.]gz* | *[-.][zZ] | *.t[ga]z)
75     FILE=`expr "X$1" : 'X\(.*\)[-.][zZtga]*$'`
76     gzip_status=$(
77       exec 4>&1
78       (gzip -cd -- "$1" 4>&-; echo $? >&4) 3>&- | eval "$cmp" - '"$FILE"' >&3
79     );;
80   *)
81     printf >&2 '%s\n' "$0: $1: unknown compressed file extension"
82     exit 2;;
83   esac
84 elif test $# -eq 2; then
85         case "$1" in
86         *[-.]gz* | *[-.][zZ] | *.t[ga]z | -)
87                 case "$2" in
88                 *[-.]gz* | *[-.][zZ] | *.t[ga]z | -)
89                     if test "$1$2" = --; then
90                         gzip_status=$(
91                           exec 4>&1
92                           (gzip -cdfq - 4>&-; echo $? >&4) 3>&- |
93                             eval "$cmp" - - >&3
94                         )
95                     elif
96                         # Reject Solaris 8's buggy /bin/bash 2.03.
97                         echo X |
98                          (echo X | eval "$cmp" /dev/fd/5 - >/dev/null 2>&1) \
99                                 5<&0
100                     then
101                         gzip_status=$(
102                           exec 4>&1
103                           (gzip -cdfq -- "$1" 4>&-; echo $? >&4) 3>&- |
104                             ( (gzip -cdfq -- "$2" 4>&-; echo $? >&4) 3>&- 5<&- </dev/null |
105                                eval "$cmp" /dev/fd/5 - >&3) 5<&0
106                         )
107                         cmp_status=$?
108                         case $gzip_status in
109                           *[1-9]*) gzip_status=1;;
110                           *) gzip_status=0;;
111                         esac
112                         (exit $cmp_status)
113                     else
114                         tmp=
115                         trap '
116                           test -n "$tmp" && rm -f "$tmp"
117                           (exit 2); exit 2
118                         ' HUP INT PIPE TERM 0
119                         case $TMPDIR in
120                           / | /*/) ;;
121                           /*) TMPDIR=$TMPDIR/;;
122                           *) TMPDIR=/tmp/;;
123                         esac
124                         if type mktemp >/dev/null 2>&1; then
125                           tmp=`mktemp "${TMPDIR}zdiffXXXXXXXXX"` ||
126                             exit 2
127                         else
128                           set -C
129                           tmp=${TMPDIR}zdiff$$
130                         fi
131                         gzip -cdfq -- "$2" > "$tmp" || exit 2
132                         gzip_status=$(
133                           exec 4>&1
134                           (gzip -cdfq -- "$1" 4>&-; echo $? >&4) 3>&- |
135                             eval "$cmp" - '"$tmp"' >&3
136                         )
137                         cmp_status=$?
138                         rm -f "$tmp" || gzip_status=$?
139                         trap - HUP INT PIPE TERM 0
140                         (exit $cmp_status)
141                     fi;;
142                 *)
143                     gzip_status=$(
144                       exec 4>&1
145                       (gzip -cdfq -- "$1" 4>&-; echo $? >&4) 3>&- |
146                         eval "$cmp" - '"$2"' >&3
147                     );;
148                 esac;;
149         *)      case "$2" in
150                 *[-.]gz* | *[-.][zZ] | *.t[ga]z | -)
151                         gzip_status=$(
152                           exec 4>&1
153                           (gzip -cdfq -- "$2" 4>&-; echo $? >&4) 3>&- |
154                             eval "$cmp" '"$1"' - >&3
155                         );;
156                 *)      eval "$cmp" '"$1"' '"$2"';;
157                 esac;;
158         esac
159 else
160         printf >&2 '%s\n' \
161           "$0: invalid number of operands; try \`$0 --help' for help"
162         exit 2
163 fi
164
165 cmp_status=$?
166 test "$gzip_status" -eq 0 || exit 2
167 exit $cmp_status