1
0
Fork 0
zutils/zdiff.in

161 lines
4.6 KiB
Text
Raw Normal View History

#! /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=
two_hyphens=0
# Loop over args
while [ -n "$1" ] ; 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 bzip2, gzip, lzip and xz."
echo
echo "Zcmp is a shortcut for \"zdiff --cmp\""
echo
echo "Usage: $0 [OPTIONS] [DIFF_OPTIONS] FILE1 [FILE2]"
echo
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.[bz2|gz|lz|xz] (the first one that is found)."
echo "DIFF_OPTIONS are passed directly to diff or cmp."
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
echo "Report bugs to zutils-bug@nongnu.org"
echo "Zutils home page: http://www.nongnu.org/zutils/zutils.html"
exit 0 ;;
--version | --ve* | -V)
echo "Zdiff (zutils) 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 ;;
-)
echo "$0: reading from stdin not supported"
exit 1 ;;
--)
shift; two_hyphens=1 ; break ;;
-?*)
args="${args} $1" ;;
*)
break ;;
esac
shift
done
# Loop over files
for i in "$@" ; do
if [ "$i" = "--" ] && [ ${two_hyphens} = 0 ] ; then two_hyphens=1
else
if [ -f "$i" ] ; then
if [ -z "${file1}" ] ; then file1="$i"
else
if [ -z "${file2}" ] ; then file2="$i"
else
echo "$0: Too many files; use --help for usage." 1>&2
fi
fi
else
echo "$0: File \"$i\" not found or not a regular file" 1>&2
exit 1
fi
fi
done
if [ -z "${file1}" ] ; then
echo "$0: No files given; use --help for usage." 1>&2
exit 1
fi
if [ -z "${file2}" ] ; then
case "${file1}" in
*.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,'` ;;
*.gz) file2=`printf "%s" "${file1}" | sed -e 's,.gz$,,'` ;;
*.tgz) file2=`printf "%s" "${file1}" | sed -e 's,tgz$,tar,'` ;;
*.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,'` ;;
*)
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"
else
echo "$0: Compressed version of ${file1} not found; use --help for usage." 1>&2
exit 1
fi ;;
esac
fi
prog1=
prog2=
bindir=`echo "$0" | sed -e 's,[^/]*$,,'`
if [ -f "${file1}" ] ; then
prog_name=`"${bindir}"zutils -t -- "${file1}"`
case "${prog_name}" in
bzip2 | gzip | lzip | xz) prog1=${prog_name} ;;
esac
fi
if [ -f "${file2}" ] ; then
prog_name=`"${bindir}"zutils -t -- "${file2}"`
case "${prog_name}" in
bzip2 | gzip | lzip | xz) prog2=${prog_name} ;;
esac
fi
retval=0
if [ -n "${prog1}" ] ; then
if [ -n "${prog2}" ] ; then
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
if [ -n "${prog2}" ] ; then
${prog2} -cdfq -- "${file2}" | ${diff_prog} ${args} -- "${file1}" -
retval=$?
else
${diff_prog} ${args} -- "${file1}" "${file2}"
retval=$?
fi
fi
exit ${retval}