xref: /freebsd/usr.bin/gzip/zdiff (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1#!/bin/sh -
2#
3# $NetBSD: zdiff,v 1.5 2010/04/14 20:30:28 joerg Exp $
4#
5# $OpenBSD: zdiff,v 1.2 2003/07/29 07:42:44 otto Exp $
6#
7#-
8# Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
9# Copyright (c) 2010 Joerg Sonnenberger <joerg@NetBSD.org>
10#
11# Permission to use, copy, modify, and distribute this software for any
12# purpose with or without fee is hereby granted, provided that the above
13# copyright notice and this permission notice appear in all copies.
14#
15# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22#
23# Sponsored in part by the Defense Advanced Research Projects
24# Agency (DARPA) and Air Force Research Laboratory, Air Force
25# Materiel Command, USAF, under agreement number F39502-99-1-0512.
26#
27
28# Set $prog based on $0
29case $0 in
30	*cmp)	prog=cmp
31		;;
32	*)	prog=diff
33		;;
34esac
35USAGE="usage: $0 [options] file1 [file2]"
36
37check_suffix() {
38	case "$1" in
39	*[._-][Zz])
40		eval "$2=\${1%??}"
41		eval "$3=\"gzip -cdqf\""
42		;;
43	*[._-]bz)
44		eval "$2=\${1%???}"
45		eval "$3=\"bzip2 -cdqf\""
46		;;
47	*[._-]gz)
48		eval "$2=\${1%???}"
49		eval "$3=\"gzip -cdqf\""
50		;;
51	*[._-]xz)
52		eval "$2=\${1%???}"
53		eval "$3=\"xz -cdqf\""
54		;;
55	*[._-]bz2)
56		eval "$2=\${1%????}"
57		eval "$3=\"bzip2 -cdqf\""
58		;;
59	*[._-]lzma)
60		eval "$2=\${1%?????}"
61		eval "$3=\"xz -cdqf\""
62		;;
63	*.t[ag]z)
64		eval "$2=\${1%??}ar"
65		eval "$3=\"gzip -cdqf\""
66		;;
67	*.tbz)
68		eval "$2=\${1%??}ar"
69		eval "$3=\"bzip2 -cdqf\""
70		;;
71	*.tbz2)
72		eval "$2=\${1%???}ar"
73		eval "$3=\"bzip2 -cdqf\""
74		;;
75	*.t[lx]z)
76		eval "$2=\${1%??}ar"
77		eval "$3=\"xz -cdqf\""
78		;;
79	*)
80		eval "$2=\$1"
81		eval "$3=\"\""
82		;;
83	esac
84}
85
86
87# Pull out any command line flags so we can pass them to diff/cmp
88# XXX - assumes there is no optarg
89flags=
90while test $# -ne 0; do
91	case "$1" in
92		--)
93			shift
94			break
95			;;
96		-)
97			break
98			;;
99		-*)
100			flags="$flags $1"
101			shift
102			;;
103		*)
104			break
105			;;
106	esac
107done
108
109if [ $# -eq 1 ]; then
110	# One file given, compare compressed to uncompressed
111	files="$1"
112	check_suffix "$1" files filt
113	if [ -z "$filt" ]; then
114		echo "z$prog: unknown suffix" 1>&2
115		exit 1
116	fi
117	$filt -- "$1" | $prog $flags -- - "$files"
118	status=$?
119elif [ $# -eq 2 ]; then
120	# Two files given, compare the two uncompressing as needed
121	check_suffix "$1" files filt
122	check_suffix "$2" files2 filt2
123	if [ -z "$filt" -a -z "$filt2" ]; then
124		$prog $flags -- "$1" "$2"
125	elif [ -z "$filt" -a -n "$filt2" -a "$1" != "-" ]; then
126		$filt2 -- "$2" | $prog $flags -- "$1" -
127	elif [ -n "$filt" -a -z "$filt2" -a "$2" != "-" ]; then
128		$filt -- "$1" | $prog $flags -- - "$2"
129	else
130		tmp=`mktemp -t z$prog.XXXXXXXXXX` || exit 1
131		trap "rm -f $tmp" 0 1 2 3 13 15
132		${filt2:-cat} -- "$2" > $tmp || exit $?
133		${filt:-cat} -- "$1" | $prog $flags -- - "$tmp"
134	fi
135	status=$?
136else
137	echo "$USAGE" 1>&2
138	exit 1
139fi
140
141exit $status
142