tests: reference: don't rely on od's -w option
[gzip.git] / zdiff.in
bloba76e91b819acc916b9103a9b1ebb8c5387f6b05a
1 #!/bin/sh
2 # sh is buggy on RS/6000 AIX 3.2. Replace above line with #!/bin/ksh
4 # Copyright (C) 1998, 2002, 2006-2007, 2009-2024 Free Software Foundation, Inc.
5 # Copyright (C) 1993 Jean-loup Gailly
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.
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.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 case $1 in
21 --__cmp) shift
22 prog=cmp; cmp='${CMP-cmp}' ;;
23 *) prog=diff; cmp='${DIFF-diff}';;
24 esac
26 version="z$prog (gzip) @VERSION@
27 Copyright (C) 2024 Free Software Foundation, Inc.
28 This is free software. You may redistribute copies of it under the terms of
29 the GNU General Public License <https://www.gnu.org/licenses/gpl.html>.
30 There is NO WARRANTY, to the extent permitted by law.
32 Written by Jean-loup Gailly."
34 usage="Usage: $0 [OPTION]... FILE1 [FILE2]
35 Compare FILE1 to FILE2, using their uncompressed contents if they are
36 compressed. If FILE2 is omitted, compare FILE1 to the uncompressed
37 contents of FILE1.gz. Do comparisons like '$prog' does.
39 OPTIONs are the same as for '$prog'.
41 Report bugs to <bug-gzip@gnu.org>."
43 # sed script to escape all ' for the shell, and then (to handle trailing
44 # newlines correctly) turn trailing X on last line into '.
45 escape='
46 s/'\''/'\''\\'\'''\''/g
47 $s/X$/'\''/
50 filesonly=
51 file1=
52 file2=
53 needop=
55 for arg
57 case $filesonly$needop$arg in
58 --h*) printf '%s\n' "$usage" || exit 2; exit;;
59 --v*) printf '%s\n' "$version" || exit 2; exit;;
60 --) filesonly=t;;
61 -*\'*) cmp="$cmp '"`printf '%sX\n' "$arg" | LC_ALL=C sed "$escape"`;;
62 -[CDFISUWXx]) needop="'$arg'";;
63 -?*) cmp="$cmp '$arg'";;
64 *) case $needop in
65 '') case $arg in
66 '') printf >&2 '%s\n' "$0: empty file name"; exit 2;;
67 esac
68 case $file1 in
69 '') file1=$arg;;
70 *) case $file2 in
71 '') file2=$arg;;
72 *) printf >&2 '%s\n' "$0: extra operand '$arg'"; exit 2;;
73 esac;;
74 esac;;
75 *) cmp="$cmp $needop '$arg'"
76 needop=;;
77 esac;;
78 esac
79 done
80 case $needop in
81 '') ;;
82 *) printf >&2 '%s\n' "$0: $prevarg: option requires an argument -- $needop"
83 exit 2;;
84 esac
86 cmp="$cmp --"
88 case $file1 in
89 '') printf >&2 '%s\n' "$0: missing operand"; exit 2;;
90 -) ;;
91 *) <"$file1" || exit 2;;
92 esac
93 case $file2 in
94 ''|-) ;;
95 *) <"$file2" || exit 2;;
96 esac
98 gzip_status=0
99 exec 3>&1
101 case $file2 in
103 case $file1 in
104 *[-.]gz* | *[-.][zZ] | *.t[ga]z)
105 FILE=`LC_ALL=C expr "X$file1" : 'X\(.*\)[-.][zZtga]*$'`
106 gzip_status=$(
107 exec 4>&1
108 ('gzip' -cd -- "$file1" 4>&-; echo $? >&4) 3>&- |
109 eval "$cmp" - '"$FILE"' >&3
112 printf >&2 '%s\n' "$0: $file1: unknown compressed file extension"
113 exit 2;;
114 esac;;
116 case $file1,$file2 in
117 -,-)
118 gzip_status=$(
119 exec 4>&1
120 ('gzip' -cdfq - 4>&-; echo $? >&4) 3>&- |
121 eval "$cmp" - - >&3
124 case $file1 in
125 *[-.]gz* | *[-.][zZ] | *.t[ga]z | -)
126 case $file2 in
127 *[-.]gz* | *[-.][zZ] | *.t[ga]z | -)
129 # Reject Solaris 8's buggy /bin/bash 2.03.
130 echo X |
131 (echo X | eval "$cmp" /dev/fd/5 - >/dev/null 2>&1) \
132 5<&0
133 then
134 gzip_status=$(
135 exec 4>&1
136 ('gzip' -cdfq -- "$file1" 4>&-; echo $? >&4) 3>&- |
137 (('gzip' -cdfq -- "$file2" 4>&-
138 echo $? >&4) 3>&- 5<&- </dev/null |
139 eval "$cmp" /dev/fd/5 - >&3) 5<&0
141 cmp_status=$?
142 case $gzip_status in
143 *[1-9]*) gzip_status=1;;
144 *) gzip_status=0;;
145 esac
146 (exit $cmp_status)
147 else
148 tmp=
149 trap '
150 test -n "$tmp" && rm -f "$tmp"
151 (exit 2); exit 2
152 ' HUP INT PIPE TERM 0
153 case $TMPDIR in
154 / | /*/) ;;
155 /*) TMPDIR=$TMPDIR/;;
156 *) TMPDIR=/tmp/;;
157 esac
158 if type mktemp >/dev/null 2>&1; then
159 tmp=`mktemp "${TMPDIR}zdiffXXXXXXXXX"` ||
160 exit 2
161 else
162 set -C
163 tmp=${TMPDIR}zdiff$$
165 'gzip' -cdfq -- "$file2" > "$tmp" || exit 2
166 gzip_status=$(
167 exec 4>&1
168 ('gzip' -cdfq -- "$file1" 4>&-; echo $? >&4) 3>&- |
169 eval "$cmp" - '"$tmp"' >&3
171 cmp_status=$?
172 rm -f "$tmp" || gzip_status=$?
173 trap - HUP INT PIPE TERM 0
174 (exit $cmp_status)
175 fi;;
177 gzip_status=$(
178 exec 4>&1
179 ('gzip' -cdfq -- "$file1" 4>&-; echo $? >&4) 3>&- |
180 eval "$cmp" - '"$file2"' >&3
182 esac;;
183 *) case $file2 in
184 *[-.]gz* | *[-.][zZ] | *.t[ga]z | -)
185 gzip_status=$(
186 exec 4>&1
187 ('gzip' -cdfq -- "$file2" 4>&-; echo $? >&4) 3>&- |
188 eval "$cmp" '"$file1"' - >&3
190 *) eval "$cmp" '"$file1"' '"$file2"';;
191 esac;;
192 esac;;
193 esac;;
194 esac
196 cmp_status=$?
197 test "$gzip_status" -eq 0 || exit 2
198 exit $cmp_status