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