2 # SPDX-License-Identifier: GPL-2.0-or-later
4 # Copyright (C) 1998, 2002, 2006, 2007 Free Software Foundation
5 # Copyright (C) 1993 Jean-loup Gailly
7 # Modified for XZ Utils by Andrew Dudman and Lasse Collin.
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 @enable_path_for_scripts@
20 #SET_PATH - This line is a placeholder to ease patching this script.
22 # Instead of unsetting XZ_OPT, just make sure that xz will use file format
23 # autodetection. This way memory usage limit and thread limit can be
24 # specified via XZ_OPT. With gzip, bzip2, and lzop it's OK to just unset the
25 # environment variables.
26 xz='@xz@ --format=auto'
27 unset GZIP BZIP BZIP2 LZOP
30 *cmp*) prog=xzcmp; cmp=${CMP:-cmp};;
31 *) prog=xzdiff; cmp=${DIFF:-diff};;
34 version="$prog (@PACKAGE_NAME@) @PACKAGE_VERSION@"
36 usage="Usage: ${0##*/} [OPTION]... FILE1 [FILE2]
37 Compare FILE1 to FILE2, using their uncompressed contents if they are
38 compressed. If FILE2 is omitted, then the files compared are FILE1 and
39 FILE1 from which the compression format suffix has been stripped.
41 Do comparisons like '$cmp' does. OPTIONs are the same as for '$cmp'.
43 Report bugs to <@PACKAGE_BUGREPORT@>."
45 # sed script to escape all ' for the shell, and then (to handle trailing
46 # newlines correctly) turn trailing X on last line into '.
48 s/'\''/'\''\\'\'''\''/g
54 --h*) printf '%s\n' "$usage" || exit 2; exit;;
55 --v*) printf '%s\n' "$version" || exit 2; exit;;
57 -*\'*) cmp="$cmp '"`printf '%sX\n' "$1" | sed "$escape"`;;
58 -?*) cmp="$cmp '$1'";;
66 test "X$file" = X- || <"$file" || exit 2
69 # xz needs -qQ to ignore warnings like unsupported check type.
75 if test $# -eq 1; then
77 *[-.]xz | *[-.]lzma | *[-.]lz | *.t[lx]z)
79 *[-.]bz2 | *.tbz | *.tbz2)
81 *[-.][zZ] | *_z | *[-.]gz | *.t[ag]z)
90 printf '%s\n' "$0: $1: Unknown compressed file name suffix" >&2
94 *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *[-.]lz | *[-.]lzo | *[-.]zst | *[-.]lz4)
95 FILE=`expr "X$1" : 'X\(.*\)[-.][abglmostxzZ24]*$'`;;
97 FILE=`expr "X$1" : 'X\(.*[-.]t\)[abglx]z$'`ar;;
99 FILE=`expr "X$1" : 'X\(.*[-.]t\)bz2$'`ar;;
101 FILE=`expr "X$1" : 'X\(.*[-.]t\)zo$'`ar;;
103 FILE=`expr "X$1" : 'X\(.*[-.]t\)zst$'`ar;;
107 ($xz1 -cd -- "$1" 4>&-; echo $? >&4) 3>&- | eval "$cmp" - '"$FILE"' >&3
109 elif test $# -eq 2; then
111 *[-.]bz2 | *.tbz | *.tbz2) xz1=bzip2;;
112 *[-.][zZ] | *_z | *[-.]gz | *.t[ag]z) xz1=gzip;;
113 *[-.]lzo | *.tzo) xz1=lzop;;
114 *[-.]zst | *.tzst) xz1='zstd -q';;
118 *[-.]bz2 | *.tbz | *.tbz2) xz2=bzip2;;
119 *[-.][zZ] | *_z | *[-.]gz | *.t[ag]z) xz2=gzip;;
120 *[-.]lzo | *.tzo) xz2=lzop;;
121 *[-.]zst | *.tzst) xz2='zstd -q';;
125 *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *[-.]lz | *.t[abglx]z | *.tbz2 | *[-.]lzo | *.tzo | *[-.]zst | *.tzst | *[-.]lz4 | -)
127 *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *[-.]lz | *.t[abglx]z | *.tbz2 | *[-.]lzo | *.tzo | *[-.]zst | *.tzst | *[-.]lz4 | -)
128 if test "$1$2" = --; then
131 ($xz1 -cdf - 4>&-; echo $? >&4) 3>&- |
134 elif # Reject Solaris 8's buggy /bin/bash 2.03.
135 echo X | (echo X | eval "$cmp" /dev/fd/5 - >/dev/null 2>&1) 5<&0; then
136 # NOTE: xz_status will contain two numbers.
139 ($xz1 -cdf -- "$1" 4>&-; echo $? >&4) 3>&- |
140 ( ($xz2 -cdf -- "$2" 4>&-; echo $? >&4) 3>&- 5<&- </dev/null |
141 eval "$cmp" /dev/fd/5 - >&3) 5<&0
144 F=`expr "/$2" : '.*/\(.*\)[-.][ablmotxz2]*$'` || F=$prog
147 test -n "$tmp" && rm -rf "$tmp"
149 ' HUP INT PIPE TERM 0
150 if type mktemp >/dev/null 2>&1; then
151 # Note that FreeBSD's mktemp isn't fully compatible with
152 # the implementations from mktemp.org and GNU coreutils.
153 # It is important that the -t argument is the last argument
154 # and that no "--" is used between -t and the template argument.
155 # This way this command works on all implementations.
156 tmp=`mktemp -d -t "$prog.XXXXXXXXXX"` || exit 2
158 # Fallback code if mktemp is missing. This isn't as
159 # robust as using mktemp since this doesn't try with
160 # different file names in case of a file name conflict.
162 # There's no need to save the original umask since
163 # we don't create any non-temp files. Note that using
164 # mkdir -m 0077 isn't secure since some mkdir implementations
165 # create the dir with the default umask and chmod the
166 # the dir afterwards.
168 mkdir -- "${TMPDIR-/tmp}/$prog.$$" || exit 2
169 tmp="${TMPDIR-/tmp}/$prog.$$"
171 $xz2 -cdf -- "$2" > "$tmp/$F" || exit 2
174 ($xz1 -cdf -- "$1" 4>&-; echo $? >&4) 3>&- |
175 eval "$cmp" - '"$tmp/$F"' >&3
178 rm -rf "$tmp" || xz_status=$?
179 trap - HUP INT PIPE TERM 0
185 ($xz1 -cdf -- "$1" 4>&-; echo $? >&4) 3>&- |
186 eval "$cmp" - '"$2"' >&3
191 *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *[-.]lz | *.t[abglx]z | *.tbz2 | *[-.]lzo | *.tzo | *[-.]zst | *.tzst | *[-.]lz4 | -)
194 ($xz2 -cdf -- "$2" 4>&-; echo $? >&4) 3>&- |
195 eval "$cmp" '"$1"' - >&3
198 eval "$cmp" '"$1"' '"$2"';;
202 printf '%s\n' "$0: Invalid number of operands; try '${0##*/} --help' for help" >&2
207 for num in $xz_status ; do
208 # 0 from decompressor means successful decompression. SIGPIPE from
209 # decompressor is possible when diff or cmp exits before the whole file
210 # has been decompressed. In that case we want to retain the exit status
211 # from diff or cmp. Note that using "trap '' PIPE" is not possible
212 # because gzip changes its behavior (including exit status) if SIGPIPE
214 test "$num" -eq 0 && continue
215 test "$num" -ge 128 \
216 && test "$(kill -l "$num" 2> /dev/null)" = "PIPE" \