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
|
2025-02-24 04:59:04 +01:00
|
|
|
while [ -n "$1" ] ; do
|
2025-02-24 04:42:45 +01:00
|
|
|
|
|
|
|
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"
|
2025-02-24 04:58:36 +01:00
|
|
|
echo "content is used. The supported compressors are bzip2, gzip, lzip and xz."
|
2025-02-24 04:42:45 +01:00
|
|
|
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"
|
2025-02-24 04:58:36 +01:00
|
|
|
echo "FILE1.[bz2|gz|lz|xz] (the first one that is found)."
|
2025-02-24 04:44:01 +01:00
|
|
|
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)
|
2025-02-24 04:59:04 +01:00
|
|
|
echo "Zdiff (zutils) VERSION"
|
2025-02-24 04:42:45 +01:00
|
|
|
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
|
2025-02-24 04:58:36 +01:00
|
|
|
if [ -f "$i" ] ; then
|
|
|
|
if [ -z "${file1}" ] ; then file1="$i"
|
2025-02-24 04:42:45 +01:00
|
|
|
else
|
2025-02-24 04:58:36 +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:58:36 +01:00
|
|
|
if [ -z "${file1}" ] ; then
|
2025-02-24 04:44:01 +01:00
|
|
|
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:58:36 +01:00
|
|
|
if [ -z "${file2}" ] ; then
|
2025-02-24 04:42:45 +01:00
|
|
|
case "${file1}" in
|
2025-02-24 04:51:33 +01:00
|
|
|
*.bz2) file2=`printf "%s" "${file1}" | sed -e 's,.bz2$,,'` ;;
|
|
|
|
*.tbz) file2=`printf "%s" "${file1}" | sed -e 's,tbz$,tar,'` ;;
|
|
|
|
*.tbz2) file2=`printf "%s" "${file1}" | sed -e 's,tbz2$,tar,'` ;;
|
2025-02-24 04:58:36 +01:00
|
|
|
*.gz) file2=`printf "%s" "${file1}" | sed -e 's,.gz$,,'` ;;
|
|
|
|
*.tgz) file2=`printf "%s" "${file1}" | sed -e 's,tgz$,tar,'` ;;
|
2025-02-24 04:51:33 +01:00
|
|
|
*.lz) file2=`printf "%s" "${file1}" | sed -e 's,.lz$,,'` ;;
|
|
|
|
*.tlz) file2=`printf "%s" "${file1}" | sed -e 's,tlz$,tar,'` ;;
|
|
|
|
*.xz) file2=`printf "%s" "${file1}" | sed -e 's,.xz$,,'` ;;
|
|
|
|
*.txz) file2=`printf "%s" "${file1}" | sed -e 's,txz$,tar,'` ;;
|
2025-02-24 04:42:45 +01:00
|
|
|
*)
|
2025-02-24 04:58:36 +01:00
|
|
|
if [ -f "${file1}.bz2" ] ; then file2="${file1}.bz2"
|
|
|
|
elif [ -f "${file1}.gz" ] ; then file2="${file1}.gz"
|
|
|
|
elif [ -f "${file1}.lz" ] ; then file2="${file1}.lz"
|
|
|
|
elif [ -f "${file1}.xz" ] ; then file2="${file1}.xz"
|
2025-02-24 04:44:01 +01:00
|
|
|
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=
|
2025-02-24 04:51:33 +01:00
|
|
|
bindir=`echo "$0" | sed -e 's,[^/]*$,,'`
|
2025-02-24 04:58:36 +01:00
|
|
|
if [ -f "${file1}" ] ; then
|
2025-02-24 04:51:33 +01:00
|
|
|
prog_name=`"${bindir}"zutils -t -- "${file1}"`
|
|
|
|
case "${prog_name}" in
|
2025-02-24 04:58:36 +01:00
|
|
|
bzip2 | gzip | lzip | xz) prog1=${prog_name} ;;
|
2025-02-24 04:51:33 +01:00
|
|
|
esac
|
|
|
|
fi
|
2025-02-24 04:58:36 +01:00
|
|
|
if [ -f "${file2}" ] ; then
|
2025-02-24 04:51:33 +01:00
|
|
|
prog_name=`"${bindir}"zutils -t -- "${file2}"`
|
|
|
|
case "${prog_name}" in
|
2025-02-24 04:58:36 +01:00
|
|
|
bzip2 | gzip | lzip | xz) prog2=${prog_name} ;;
|
2025-02-24 04:51:33 +01:00
|
|
|
esac
|
|
|
|
fi
|
2025-02-24 04:42:45 +01:00
|
|
|
|
|
|
|
retval=0
|
2025-02-24 04:58:36 +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:58:36 +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}
|