summaryrefslogtreecommitdiffstats
path: root/zdiff.in
diff options
context:
space:
mode:
Diffstat (limited to 'zdiff.in')
-rw-r--r--zdiff.in171
1 files changed, 171 insertions, 0 deletions
diff --git a/zdiff.in b/zdiff.in
new file mode 100644
index 0000000..4be41d8
--- /dev/null
+++ b/zdiff.in
@@ -0,0 +1,171 @@
+#! /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
+invocation_name=$0
+args=
+default_ext=.lz
+diff_prog=diff
+file1=
+file2=
+
+# 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
+ echo "Usage: ${invocation_name} [OPTIONS] [DIFF_OPTIONS] FILE1 [FILE2]"
+ echo
+ echo "Compares FILE1 to FILE2. If FILE2 is omitted, compares FILE1 to the"
+ echo "uncompressed contents of FILE1.[gz|bz2|lz] (depending on the default"
+ echo "compressor selected). 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 " --gzip use gzip as default decompressor"
+ echo " --bzip2 use bzip2 as default decompressor"
+ echo " --lzip use lzip as default decompressor (default)"
+ echo " --xz use xz as default decompressor"
+ echo " --diff use diff to compare files (default)"
+ echo " --cmp use cmp to compare files"
+ echo
+ echo "Report bugs to lzip-bug@nongnu.org"
+ echo "Lzip home page: http://www.nongnu.org/lzip/lzip.html"
+ 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 ;;
+ --gz*)
+ default_ext=.gz ;;
+ --bz*)
+ default_ext=.bz2 ;;
+ --lz*)
+ default_ext=.lz ;;
+ --xz*)
+ default_prog=xz ;;
+ --diff)
+ diff_prog=diff ;;
+ --cmp)
+ diff_prog=cmp ;;
+ -)
+ echo "${invocation_name}: reading from stdin not supported"
+ exit 1 ;;
+ --)
+ shift; break ;;
+ -?*)
+ args="${args} $1" ;;
+ *)
+ break ;;
+ esac
+ shift
+done
+
+# Loop over files
+while [ x"$1" != x ] ; do
+ if [ "$1" != "--" ] ; then
+ if test -f "$1"; then
+ if test -z "${file1}"; then file1="$1"
+ else
+ if test -z "${file2}"; then file2="$1"
+ else
+ echo "${invocation_name}: Too many files; use --help for usage." 1>&2
+ fi
+ fi
+ else
+ echo "${invocation_name}: File \"$1\" not found or not a regular file" 1>&2
+ exit 1
+ fi
+ fi
+ shift
+done
+
+if test -z "${file1}"; then
+ echo "${invocation_name}: No files given; use --help for usage." 1>&2
+ exit 1
+fi
+
+if test -z "${file2}"; then
+ 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/'` ;;
+ *)
+ file2="${file1}${default_ext}" ;;
+ 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
+if test -n "${prog1}"; then
+ if test -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 test -n "${prog2}"; then
+ ${prog2} -cdfq -- "${file2}" | ${diff_prog} ${args} -- "${file1}" -
+ retval=$?
+ else
+ ${diff_prog} ${args} -- "${file1}" "${file2}"
+ retval=$?
+ fi
+fi
+
+exit ${retval}