maint: update bootstrap and m4/.gitignore to latest Gnulib
[gzip.git] / zdiff.in
blob423fcb4a370ed4ec688badd8db3144bb4cbda4ea
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 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.
21 case $1 in
22 --__cmp) shift
23 prog=cmp; cmp='${CMP-cmp}' ;;
24 *) prog=diff; cmp='${DIFF-diff}';;
25 esac
27 version="z$prog (gzip) @VERSION@
28 Copyright (C) 2024 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.
33 Written by Jean-loup Gailly."
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.
40 OPTIONs are the same as for '$prog'.
42 Report bugs to <bug-gzip@gnu.org>."
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$/'\''/
51 filesonly=
52 file1=
53 file2=
54 needop=
56 for arg
58 case $filesonly$needop$arg in
59 --h*) printf '%s\n' "$usage" || exit 2; exit;;
60 --v*) printf '%s\n' "$version" || exit 2; exit;;
61 --) filesonly=t;;
62 -*\'*) cmp="$cmp '"`printf '%sX\n' "$arg" | LC_ALL=C sed "$escape"`;;
63 -[CDFISUWXx]) needop="'$arg'";;
64 -?*) cmp="$cmp '$arg'";;
65 *) case $needop in
66 '') case $arg in
67 '') printf >&2 '%s\n' "$0: empty file name"; exit 2;;
68 esac
69 case $file1 in
70 '') file1=$arg;;
71 *) case $file2 in
72 '') file2=$arg;;
73 *) printf >&2 '%s\n' "$0: extra operand '$arg'"; exit 2;;
74 esac;;
75 esac;;
76 *) cmp="$cmp $needop '$arg'"
77 needop=;;
78 esac;;
79 esac
80 done
81 case $needop in
82 '') ;;
83 *) printf >&2 '%s\n' "$0: $prevarg: option requires an argument -- $needop"
84 exit 2;;
85 esac
87 cmp="$cmp --"
89 case $file1 in
90 '') printf >&2 '%s\n' "$0: missing operand"; exit 2;;
91 -) ;;
92 *) <"$file1" || exit 2;;
93 esac
94 case $file2 in
95 ''|-) ;;
96 *) <"$file2" || exit 2;;
97 esac
99 gzip_status=0
100 exec 3>&1
102 case $file2 in
104 case $file1 in
105 *[-.]gz* | *[-.][zZ] | *.t[ga]z)
106 FILE=`LC_ALL=C expr "X$file1" : 'X\(.*\)[-.][zZtga]*$'`
107 gzip_status=$(
108 exec 4>&1
109 ('gzip' -cd -- "$file1" 4>&-; echo $? >&4) 3>&- |
110 eval "$cmp" - '"$FILE"' >&3
113 printf >&2 '%s\n' "$0: $file1: unknown compressed file extension"
114 exit 2;;
115 esac;;
117 case $file1,$file2 in
118 -,-)
119 gzip_status=$(
120 exec 4>&1
121 ('gzip' -cdfq - 4>&-; echo $? >&4) 3>&- |
122 eval "$cmp" - - >&3
125 case $file1 in
126 *[-.]gz* | *[-.][zZ] | *.t[ga]z | -)
127 case $file2 in
128 *[-.]gz* | *[-.][zZ] | *.t[ga]z | -)
130 # Reject Solaris 8's buggy /bin/bash 2.03.
131 echo X |
132 (echo X | eval "$cmp" /dev/fd/5 - >/dev/null 2>&1) \
133 5<&0
134 then
135 gzip_status=$(
136 exec 4>&1
137 ('gzip' -cdfq -- "$file1" 4>&-; echo $? >&4) 3>&- |
138 (('gzip' -cdfq -- "$file2" 4>&-
139 echo $? >&4) 3>&- 5<&- </dev/null |
140 eval "$cmp" /dev/fd/5 - >&3) 5<&0
142 cmp_status=$?
143 case $gzip_status in
144 *[1-9]*) gzip_status=1;;
145 *) gzip_status=0;;
146 esac
147 (exit $cmp_status)
148 else
149 tmp=
150 trap '
151 test -n "$tmp" && rm -f "$tmp"
152 (exit 2); exit 2
153 ' HUP INT PIPE TERM 0
154 case $TMPDIR in
155 / | /*/) ;;
156 /*) TMPDIR=$TMPDIR/;;
157 *) TMPDIR=/tmp/;;
158 esac
159 if type mktemp >/dev/null 2>&1; then
160 tmp=`mktemp "${TMPDIR}zdiffXXXXXXXXX"` ||
161 exit 2
162 else
163 set -C
164 tmp=${TMPDIR}zdiff$$
166 'gzip' -cdfq -- "$file2" > "$tmp" || exit 2
167 gzip_status=$(
168 exec 4>&1
169 ('gzip' -cdfq -- "$file1" 4>&-; echo $? >&4) 3>&- |
170 eval "$cmp" - '"$tmp"' >&3
172 cmp_status=$?
173 rm -f "$tmp" || gzip_status=$?
174 trap - HUP INT PIPE TERM 0
175 (exit $cmp_status)
176 fi;;
178 gzip_status=$(
179 exec 4>&1
180 ('gzip' -cdfq -- "$file1" 4>&-; echo $? >&4) 3>&- |
181 eval "$cmp" - '"$file2"' >&3
183 esac;;
184 *) case $file2 in
185 *[-.]gz* | *[-.][zZ] | *.t[ga]z | -)
186 gzip_status=$(
187 exec 4>&1
188 ('gzip' -cdfq -- "$file2" 4>&-; echo $? >&4) 3>&- |
189 eval "$cmp" '"$file1"' - >&3
191 *) eval "$cmp" '"$file1"' '"$file2"';;
192 esac;;
193 esac;;
194 esac;;
195 esac
197 cmp_status=$?
198 test "$gzip_status" -eq 0 || exit 2
199 exit $cmp_status