2025-02-24 04:42:45 +01:00
|
|
|
#! /bin/sh
|
|
|
|
# Zdiff - Diff/cmp wrapper for compressed files.
|
|
|
|
# Copyright (C) 2008, 2009 Antonio Diaz Diaz.
|
|
|
|
#
|
|
|
|
# This script is free software: you have unlimited permission
|
|
|
|
# to copy, distribute and modify it.
|
|
|
|
|
|
|
|
LC_ALL=C
|
|
|
|
export LC_ALL
|
|
|
|
args=
|
|
|
|
diff_prog=diff
|
|
|
|
file1=
|
|
|
|
file2=
|
2025-02-24 04:44:01 +01:00
|
|
|
two_hyphens=0
|
2025-02-24 04:42:45 +01:00
|
|
|
|
|
|
|
# Loop over args
|
|
|
|
while [ x"$1" != x ] ; do
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
--help | --he* | -h)
|
|
|
|
echo "Zdiff - Diff/cmp wrapper for compressed files."
|
|
|
|
echo
|
|
|
|
echo "Zdiff is a wrapper script around the diff and cmp commands that allows"
|
|
|
|
echo "transparent comparison of any combination of compressed and"
|
|
|
|
echo "non-compressed files. If any given file is compressed, its uncompressed"
|
|
|
|
echo "content is used. The supported compressors are gzip, bzip2, lzip and xz."
|
|
|
|
echo
|
|
|
|
echo "Zcmp is a shortcut for \"zdiff --cmp\""
|
|
|
|
echo
|
2025-02-24 04:44:01 +01:00
|
|
|
echo "Usage: $0 [OPTIONS] [DIFF_OPTIONS] FILE1 [FILE2]"
|
2025-02-24 04:42:45 +01:00
|
|
|
echo
|
2025-02-24 04:44:01 +01:00
|
|
|
echo "Compares FILE1 to FILE2. If FILE2 is omitted and FILE1 is compressed,"
|
|
|
|
echo "compares FILE1 to the file with the corresponding decompressed file"
|
|
|
|
echo "name (removes the extension from FILE1). If FILE2 is omitted and FILE1"
|
|
|
|
echo "is not compressed, compares FILE1 to the uncompressed contents of"
|
|
|
|
echo "FILE1.[gz|bz2|lz|xz] (the first one that is found)."
|
|
|
|
echo "DIFF_OPTIONS are passed directly to diff or cmp."
|
2025-02-24 04:42:45 +01:00
|
|
|
echo "The exit status from diff or cmp is preserved."
|
|
|
|
echo
|
|
|
|
echo "Options:"
|
|
|
|
echo " -h, --help display this help and exit"
|
|
|
|
echo " -V, --version output version information and exit"
|
|
|
|
echo " --diff use diff to compare files (default)"
|
|
|
|
echo " --cmp use cmp to compare files"
|
|
|
|
echo
|
2025-02-24 04:44:01 +01:00
|
|
|
echo "Report bugs to zutils-bug@nongnu.org"
|
|
|
|
echo "Zutils home page: http://www.nongnu.org/zutils/zutils.html"
|
2025-02-24 04:42:45 +01:00
|
|
|
exit 0 ;;
|
|
|
|
--version | --ve* | -V)
|
|
|
|
echo "Zdiff VERSION"
|
|
|
|
echo "Copyright (C) 2009 Antonio Diaz Diaz."
|
|
|
|
echo "This script is free software: you have unlimited permission"
|
|
|
|
echo "to copy, distribute and modify it."
|
|
|
|
exit 0 ;;
|
|
|
|
--diff)
|
|
|
|
diff_prog=diff ;;
|
|
|
|
--cmp)
|
|
|
|
diff_prog=cmp ;;
|
|
|
|
-)
|
2025-02-24 04:44:01 +01:00
|
|
|
echo "$0: reading from stdin not supported"
|
2025-02-24 04:42:45 +01:00
|
|
|
exit 1 ;;
|
|
|
|
--)
|
2025-02-24 04:44:01 +01:00
|
|
|
shift; two_hyphens=1 ; break ;;
|
2025-02-24 04:42:45 +01:00
|
|
|
-?*)
|
|
|
|
args="${args} $1" ;;
|
|
|
|
*)
|
|
|
|
break ;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
# Loop over files
|
2025-02-24 04:44:01 +01:00
|
|
|
for i in "$@" ; do
|
|
|
|
if [ "$i" = "--" ] && [ ${two_hyphens} = 0 ] ; then two_hyphens=1
|
|
|
|
else
|
|
|
|
if [ -f "$i" ]; then
|
|
|
|
if [ -z "${file1}" ]; then file1="$i"
|
2025-02-24 04:42:45 +01:00
|
|
|
else
|
2025-02-24 04:44:01 +01:00
|
|
|
if [ -z "${file2}" ]; then file2="$i"
|
2025-02-24 04:42:45 +01:00
|
|
|
else
|
2025-02-24 04:44:01 +01:00
|
|
|
echo "$0: Too many files; use --help for usage." 1>&2
|
2025-02-24 04:42:45 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
2025-02-24 04:44:01 +01:00
|
|
|
echo "$0: File \"$i\" not found or not a regular file" 1>&2
|
2025-02-24 04:42:45 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2025-02-24 04:44:01 +01:00
|
|
|
if [ -z "${file1}" ]; then
|
|
|
|
echo "$0: No files given; use --help for usage." 1>&2
|
2025-02-24 04:42:45 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2025-02-24 04:44:01 +01:00
|
|
|
if [ -z "${file2}" ]; then
|
2025-02-24 04:42:45 +01:00
|
|
|
case "${file1}" in
|
|
|
|
*.gz)
|
|
|
|
file2=`printf "%s" "${file1}" | sed 's/.gz$//'` ;;
|
|
|
|
*.tgz)
|
|
|
|
file2=`printf "%s" "${file1}" | sed 's/tgz$/tar/'` ;;
|
|
|
|
*.bz2)
|
|
|
|
file2=`printf "%s" "${file1}" | sed 's/.bz2$//'` ;;
|
|
|
|
*.tbz)
|
|
|
|
file2=`printf "%s" "${file1}" | sed 's/tbz$/tar/'` ;;
|
|
|
|
*.tbz2)
|
|
|
|
file2=`printf "%s" "${file1}" | sed 's/tbz2$/tar/'` ;;
|
|
|
|
*.lz)
|
|
|
|
file2=`printf "%s" "${file1}" | sed 's/.lz$//'` ;;
|
|
|
|
*.tlz)
|
|
|
|
file2=`printf "%s" "${file1}" | sed 's/tlz$/tar/'` ;;
|
|
|
|
*.xz)
|
|
|
|
file2=`printf "%s" "${file1}" | sed 's/.xz$//'` ;;
|
|
|
|
*.txz)
|
|
|
|
file2=`printf "%s" "${file1}" | sed 's/txz$/tar/'` ;;
|
|
|
|
*)
|
2025-02-24 04:44:01 +01:00
|
|
|
if [ -f "${file1}.gz" ]; then file2="${file1}.gz"
|
|
|
|
elif [ -f "${file1}.bz2" ]; then file2="${file1}.bz2"
|
|
|
|
elif [ -f "${file1}.lz" ]; then file2="${file1}.lz"
|
|
|
|
elif [ -f "${file1}.xz" ]; then file2="${file1}.xz"
|
|
|
|
else
|
|
|
|
echo "$0: Compressed version of ${file1} not found; use --help for usage." 1>&2
|
|
|
|
exit 1
|
|
|
|
fi ;;
|
2025-02-24 04:42:45 +01:00
|
|
|
esac
|
|
|
|
fi
|
|
|
|
|
|
|
|
prog1=
|
|
|
|
prog2=
|
|
|
|
case "${file1}" in
|
|
|
|
*.gz | *.tgz) prog1=gzip ;;
|
|
|
|
*.bz2 | *.tbz | *.tbz2) prog1=bzip2 ;;
|
|
|
|
*.lz | *.tlz) prog1=lzip ;;
|
|
|
|
*.xz | *.txz) prog1=xz ;;
|
|
|
|
esac
|
|
|
|
case "${file2}" in
|
|
|
|
*.gz | *.tgz) prog2=gzip ;;
|
|
|
|
*.bz2 | *.tbz | *.tbz2) prog2=bzip2 ;;
|
|
|
|
*.lz | *.tlz) prog2=lzip ;;
|
|
|
|
*.xz | *.txz) prog2=xz ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
retval=0
|
2025-02-24 04:44:01 +01:00
|
|
|
if [ -n "${prog1}" ]; then
|
|
|
|
if [ -n "${prog2}" ]; then
|
2025-02-24 04:42:45 +01:00
|
|
|
tmp_file=`mktemp "${TMPDIR:-/tmp}"/zdiff.XXXXXXXXXX` || {
|
|
|
|
echo 'cannot create a temporary file' 1>&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
${prog2} -cdfq -- "${file2}" > "${tmp_file}" || exit 1
|
|
|
|
${prog1} -cdfq -- "${file1}" | ${diff_prog} ${args} - -- "${tmp_file}"
|
|
|
|
retval=$?
|
|
|
|
rm -f "${tmp_file}" || retval=$?
|
|
|
|
else
|
|
|
|
${prog1} -cdfq -- "${file1}" | ${diff_prog} ${args} - -- "${file2}"
|
|
|
|
retval=$?
|
|
|
|
fi
|
|
|
|
else
|
2025-02-24 04:44:01 +01:00
|
|
|
if [ -n "${prog2}" ]; then
|
2025-02-24 04:42:45 +01:00
|
|
|
${prog2} -cdfq -- "${file2}" | ${diff_prog} ${args} -- "${file1}" -
|
|
|
|
retval=$?
|
|
|
|
else
|
|
|
|
${diff_prog} ${args} -- "${file1}" "${file2}"
|
|
|
|
retval=$?
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit ${retval}
|