1166255Sdelphij#!/bin/sh -
2166255Sdelphij#
3222210Sdelphij# $NetBSD: zdiff,v 1.5 2010/04/14 20:30:28 joerg Exp $
4222210Sdelphij#
5166255Sdelphij# $OpenBSD: zdiff,v 1.2 2003/07/29 07:42:44 otto Exp $
6166255Sdelphij#
7166255Sdelphij#-
8166255Sdelphij# Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
9222210Sdelphij# Copyright (c) 2010 Joerg Sonnenberger <joerg@NetBSD.org>
10166255Sdelphij#
11166255Sdelphij# Permission to use, copy, modify, and distribute this software for any
12166255Sdelphij# purpose with or without fee is hereby granted, provided that the above
13166255Sdelphij# copyright notice and this permission notice appear in all copies.
14166255Sdelphij#
15166255Sdelphij# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16166255Sdelphij# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17166255Sdelphij# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18166255Sdelphij# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19166255Sdelphij# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20166255Sdelphij# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21166255Sdelphij# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22166255Sdelphij#
23166255Sdelphij# Sponsored in part by the Defense Advanced Research Projects
24166255Sdelphij# Agency (DARPA) and Air Force Research Laboratory, Air Force
25166255Sdelphij# Materiel Command, USAF, under agreement number F39502-99-1-0512.
26166255Sdelphij#
27166255Sdelphij# $FreeBSD$
28166255Sdelphij
29166255Sdelphij# Set $prog based on $0
30166255Sdelphijcase $0 in
31166255Sdelphij	*cmp)	prog=cmp
32166255Sdelphij		;;
33166255Sdelphij	*)	prog=diff
34166255Sdelphij		;;
35166255Sdelphijesac
36222210SdelphijUSAGE="usage: $0 [options] file1 [file2]"
37166255Sdelphij
38222210Sdelphijcheck_suffix() {
39222210Sdelphij	case "$1" in
40222210Sdelphij	*[._-][Zz])
41222210Sdelphij		setvar $2 "${1%??}"
42222210Sdelphij		setvar $3 "gzip -cdqf"
43222210Sdelphij		;;
44222210Sdelphij	*[._-]bz)
45222210Sdelphij		setvar $2 "${1%???}"
46222210Sdelphij		setvar $3 "bzip2 -cdqf"
47222210Sdelphij		;;
48222210Sdelphij	*[._-]gz)
49222210Sdelphij		setvar $2 "${1%???}"
50222210Sdelphij		setvar $3 "gzip -cdqf"
51222210Sdelphij		;;
52222210Sdelphij	*[._-]xz)
53222210Sdelphij		setvar $2 "${1%???}"
54222210Sdelphij		setvar $3 "xz -cdqf"
55222210Sdelphij		;;
56222210Sdelphij	*[._-]bz2)
57222210Sdelphij		setvar $2 "${1%????}"
58222210Sdelphij		setvar $3 "bzip2 -cdqf"
59222210Sdelphij		;;
60222210Sdelphij	*[._-]lzma)
61222210Sdelphij		setvar $2 "${1%?????}"
62222210Sdelphij		setvar $3 "xz -cdqf"
63222210Sdelphij		;;
64222210Sdelphij	*.t[ag]z)
65222210Sdelphij		setvar $2 "${1%??}"ar
66222210Sdelphij		setvar $3 "gzip -cdqf"
67222210Sdelphij		;;
68222210Sdelphij	*.tbz)
69222210Sdelphij		setvar $2 "${1%??}"ar
70222210Sdelphij		setvar $3 "bzip2 -cdqf"
71222210Sdelphij		;;
72222210Sdelphij	*.tbz2)
73222210Sdelphij		setvar $2 "${1%???}"ar
74222210Sdelphij		setvar $3 "bzip2 -cdqf"
75222210Sdelphij		;;
76222210Sdelphij	*.t[lx]z)
77222210Sdelphij		setvar $2 "${1%??}"ar
78222210Sdelphij		setvar $3 "xz -cdqf"
79222210Sdelphij		;;
80222210Sdelphij	*)
81222210Sdelphij		setvar $2 "$1"
82222210Sdelphij		setvar $3 ""
83222210Sdelphij		;;
84222210Sdelphij	esac
85222210Sdelphij}
86222210Sdelphij
87222210Sdelphij
88166255Sdelphij# Pull out any command line flags so we can pass them to diff/cmp
89166255Sdelphij# XXX - assumes there is no optarg
90166255Sdelphijflags=
91166255Sdelphijwhile test $# -ne 0; do
92166255Sdelphij	case "$1" in
93166255Sdelphij		--)
94166255Sdelphij			shift
95166255Sdelphij			break
96166255Sdelphij			;;
97222210Sdelphij		-)
98222210Sdelphij			break
99222210Sdelphij			;;
100166255Sdelphij		-*)
101166255Sdelphij			flags="$flags $1"
102166255Sdelphij			shift
103166255Sdelphij			;;
104166255Sdelphij		*)
105166255Sdelphij			break
106166255Sdelphij			;;
107166255Sdelphij	esac
108166255Sdelphijdone
109166255Sdelphij
110166255Sdelphijif [ $# -eq 1 ]; then
111166255Sdelphij	# One file given, compare compressed to uncompressed
112166255Sdelphij	files="$1"
113222210Sdelphij	check_suffix "$1" files filt
114222210Sdelphij	if [ -z "$filt" ]; then
115222210Sdelphij		echo "z$prog: unknown suffix" 1>&2
116222210Sdelphij		exit 1
117222210Sdelphij	fi
118222210Sdelphij	$filt -- "$1" | $prog $flags -- - "$files"
119166255Sdelphij	status=$?
120166255Sdelphijelif [ $# -eq 2 ]; then
121166255Sdelphij	# Two files given, compare the two uncompressing as needed
122222210Sdelphij	check_suffix "$1" files filt
123222210Sdelphij	check_suffix "$2" files2 filt2
124222210Sdelphij	if [ -z "$filt" -a -z "$filt2" ]; then
125222210Sdelphij		$prog $flags -- "$1" "$2"
126222210Sdelphij	elif [ -z "$filt" -a -n "$filt2" -a "$1" != "-" ]; then
127222210Sdelphij		$filt2 -- "$2" | $prog $flags -- "$1" -
128222210Sdelphij	elif [ -n "$filt" -a -z "$filt2" -a "$2" != "-" ]; then
129222210Sdelphij		$filt -- "$1" | $prog $flags -- - "$2"
130166255Sdelphij	else
131222210Sdelphij		tmp=`mktemp -t z$prog.XXXXXXXXXX` || exit 1
132222210Sdelphij		trap "rm -f $tmp" 0 1 2 3 13 15
133222210Sdelphij		${filt2:-cat} -- "$2" > $tmp || exit $?
134222210Sdelphij		${filt:-cat} -- "$1" | $prog $flags -- - "$tmp"
135166255Sdelphij	fi
136166255Sdelphij	status=$?
137166255Sdelphijelse
138166255Sdelphij	echo "$USAGE" 1>&2
139166255Sdelphij	exit 1
140166255Sdelphijfi
141166255Sdelphij
142166255Sdelphijexit $status
143