summaryrefslogtreecommitdiffstats
path: root/lzdiff
blob: c3c605d007ab7ad4e0edf47ef981d7d4052ee530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#! /bin/sh
# Lzdiff - 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 "Lzdiff - Diff/cmp wrapper for compressed files."
		echo
		echo "Lzdiff 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 and lzip."
		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 "      --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 "Lzdiff 0.4"
		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 ;;
	--diff)
		diff_prog=diff ;;
	--cmp)
		diff_prog=cmp ;;
	-)
		echo "${invocation_name}: reading from stdin not supported\n"
		exit 1 ;;
	--)
		;;
	-?*)
		args="${args} $1" ;;
	*)
		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 ;;
	esac
	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=`echo "${file1}" | sed 's/.gz$//'` ;;
	*.tgz)
		file2=`echo "${file1}" | sed 's/tgz$/tar/'` ;;
	*.bz2)
		file2=`echo "${file1}" | sed 's/.bz2$//'` ;;
	*.tbz)
		file2=`echo "${file1}" | sed 's/tbz$/tar/'` ;;
	*.tbz2)
		file2=`echo "${file1}" | sed 's/tbz2$/tar/'` ;;
	*.lz)
		file2=`echo "${file1}" | sed 's/.lz$//'` ;;
	*.tlz)
		file2=`echo "${file1}" | sed 's/tlz$/tar/'` ;;
	*)
		file2="${file1}"; file1="${file1}${default_ext}" ;;
	esac
fi

prog1=
prog2=
case "${file1}" in
*.gz | *.tgz)		prog1=gzip ;;
*.bz2 | *.tbz | *.tbz2)	prog1=bzip2 ;;
*.lz | *.tlz)		prog1=lzip ;;
esac
case "${file2}" in
*.gz | *.tgz)		prog2=gzip ;;
*.bz2 | *.tbz | *.tbz2)	prog2=bzip2 ;;
*.lz | *.tlz)		prog2=lzip ;;
esac

retval=0
if test -n "${prog1}"; then
	if test -n "${prog2}"; then
		tmp_file=`mktemp "${TMPDIR:-/tmp}"/lzdiff.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}