187011Smsmith#!/bin/sh -
287011Smsmith#
387011Smsmith# $OpenBSD: zdiff,v 1.3 2019/01/25 00:19:26 millert Exp $
487011Smsmith#
587011Smsmith# Copyright (c) 2003 Todd C. Miller <millert@openbsd.org>
687011Smsmith#
787011Smsmith# Permission to use, copy, modify, and distribute this software for any
887011Smsmith# purpose with or without fee is hereby granted, provided that the above
987011Smsmith# copyright notice and this permission notice appear in all copies.
1087011Smsmith#
1187011Smsmith# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1287011Smsmith# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1387011Smsmith# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1487011Smsmith# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1587011Smsmith# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1687011Smsmith# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1787011Smsmith# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1887011Smsmith#
1987011Smsmith# Sponsored in part by the Defense Advanced Research Projects
2087011Smsmith# Agency (DARPA) and Air Force Research Laboratory, Air Force
2187011Smsmith# Materiel Command, USAF, under agreement number F39502-99-1-0512.
2287011Smsmith#
2387011Smsmith
2487011Smsmith# Set $prog based on $0
2587011Smsmithcase $0 in
2687011Smsmith	*cmp)	prog=cmp
2787011Smsmith		;;
2887011Smsmith	*)	prog=diff
2987011Smsmith		;;
3087011Smsmithesac
3187011SmsmithUSAGE="usage: z$prog [options] file1 [file2]"
3287011Smsmith
33180454Sscottl# Pull out any command line flags so we can pass them to diff/cmp
34180454Sscottl# XXX - assumes there is no optarg
3587011Smsmithflags=
3687011Smsmithwhile test $# -ne 0; do
3787011Smsmith	case "$1" in
3887011Smsmith		--)
3987011Smsmith			shift
4087011Smsmith			break
4187011Smsmith			;;
4287011Smsmith		-*)
4387011Smsmith			flags="$flags $1"
44204648Smav			shift
4587011Smsmith			;;
4687011Smsmith		*)
4787011Smsmith			break
48246152Ssbruno			;;
49246152Ssbruno	esac
50246152Ssbrunodone
5187011Smsmith
52246152Ssbrunoif [ $# -eq 1 ]; then
5387011Smsmith	# One file given, compare compressed to uncompressed
5487011Smsmith	files="$1"
55128337Sps	case "$1" in
56128337Sps		*[._-][Zz])
57128337Sps			files="${1%??}"
58128337Sps			;;
59128337Sps		*[._-]gz)
6087011Smsmith			files="${1%???}"
6187011Smsmith			;;
6287011Smsmith		*.t[ag]z)
6387011Smsmith			files="${1%??}"ar
6487011Smsmith			;;
6587011Smsmith		*)	echo "z$prog: unknown suffix" 1>&2
6687011Smsmith			exit 1
6787011Smsmith	esac
6887011Smsmith	compress -cdfq "$1" | $prog $flags - "$files"
69180454Sscottl	status=$?
7087011Smsmithelif [ $# -eq 2 ]; then
7187011Smsmith	# Two files given, compare the two uncompressing as needed
7287011Smsmith	case "$1" in
7387011Smsmith		*[._-][Zz]|*[._-]gz|*.t[ag]z)
7487011Smsmith			files=-
7587011Smsmith			filt="compress -cdfq $1"
7687011Smsmith			;;
7787011Smsmith		*)
7887011Smsmith			files="$1"
7987011Smsmith			;;
8087187Smsmith	esac
8187187Smsmith	case "$2" in
8287187Smsmith		*[._-][Zz]|*[._-]gz|*.t[ag]z)
8387187Smsmith			if [ "$files" = "-" ]; then
8487187Smsmith				tmp=`mktemp -t z$prog.XXXXXXXXXX` || exit 1
8587187Smsmith				trap "rm -f $tmp" 0 1 2 3 13 15
8687187Smsmith				compress -cdfq "$2" > $tmp
8787187Smsmith				files="$files $tmp"
8887011Smsmith			else
8987011Smsmith				files="$files -"
9087187Smsmith				filt="compress -cdfq $2"
9187011Smsmith			fi
9287011Smsmith			;;
9387011Smsmith		*)
9487011Smsmith			files="$files $2"
9587011Smsmith			;;
9687011Smsmith	esac
9787011Smsmith	if [ -n "$filt" ]; then
9887011Smsmith		$filt | $prog $flags $files
9987011Smsmith	else
10087011Smsmith		$prog $flags $files
10187011Smsmith	fi
102180454Sscottl	status=$?
10387011Smsmithelse
10487011Smsmith	echo "$USAGE" 1>&2
10587011Smsmith	exit 1
10687011Smsmithfi
10787011Smsmith
10887011Smsmithexit $status
109197263Sscottl