zdiff revision 1.1
11.1Smrg#!/bin/sh -
21.1Smrg#
31.1Smrg# $OpenBSD: zdiff,v 1.2 2003/07/29 07:42:44 otto Exp $
41.1Smrg#
51.1Smrg# Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
61.1Smrg#
71.1Smrg# Permission to use, copy, modify, and distribute this software for any
81.1Smrg# purpose with or without fee is hereby granted, provided that the above
91.1Smrg# copyright notice and this permission notice appear in all copies.
101.1Smrg#
111.1Smrg# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
121.1Smrg# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
131.1Smrg# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
141.1Smrg# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
151.1Smrg# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
161.1Smrg# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
171.1Smrg# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
181.1Smrg#
191.1Smrg# Sponsored in part by the Defense Advanced Research Projects
201.1Smrg# Agency (DARPA) and Air Force Research Laboratory, Air Force
211.1Smrg# Materiel Command, USAF, under agreement number F39502-99-1-0512.
221.1Smrg#
231.1Smrg
241.1Smrg# Set $prog based on $0
251.1Smrgcase $0 in
261.1Smrg	*cmp)	prog=cmp
271.1Smrg		;;
281.1Smrg	*)	prog=diff
291.1Smrg		;;
301.1Smrgesac
311.1SmrgUSAGE="usage: z$prog [options] file1 [file2]"
321.1Smrg
331.1Smrg# Pull out any command line flags so we can pass them to diff/cmp
341.1Smrg# XXX - assumes there is no optarg
351.1Smrgflags=
361.1Smrgwhile test $# -ne 0; do
371.1Smrg	case "$1" in
381.1Smrg		--)
391.1Smrg			shift
401.1Smrg			break
411.1Smrg			;;
421.1Smrg		-*)
431.1Smrg			flags="$flags $1"
441.1Smrg			shift
451.1Smrg			;;
461.1Smrg		*)
471.1Smrg			break
481.1Smrg			;;
491.1Smrg	esac
501.1Smrgdone
511.1Smrg
521.1Smrgif [ $# -eq 1 ]; then
531.1Smrg	# One file given, compare compressed to uncompressed
541.1Smrg	files="$1"
551.1Smrg	case "$1" in
561.1Smrg		*[._-][Zz])
571.1Smrg			files="${1%??}"
581.1Smrg			;;
591.1Smrg		*[._-]gz)
601.1Smrg			files="${1%???}"
611.1Smrg			;;
621.1Smrg		*.t[ag]z)
631.1Smrg			files="${1%??}"ar
641.1Smrg			;;
651.1Smrg		*)	echo "z$prog: unknown suffix" 1>&2
661.1Smrg			exit 1
671.1Smrg	esac
681.1Smrg	compress -cdfq "$1" | $prog $flags - "$files"
691.1Smrg	status=$?
701.1Smrgelif [ $# -eq 2 ]; then
711.1Smrg	# Two files given, compare the two uncompressing as needed
721.1Smrg	case "$1" in
731.1Smrg		*[._-][Zz]|*[._-]gz|*.t[ag]z)
741.1Smrg			files=-
751.1Smrg			filt="compress -cdfq $1"
761.1Smrg			;;
771.1Smrg		*)
781.1Smrg			files="$1"
791.1Smrg			;;
801.1Smrg	esac
811.1Smrg	case "$2" in
821.1Smrg		*[._-][Zz]|*[._-]gz|*.t[ag]z)
831.1Smrg			if [ "$files" = "-" ]; then
841.1Smrg				tmp=`mktemp -t z$prog.XXXXXXXXXX` || exit 1
851.1Smrg				trap "rm -f $tmp" 0 1 2 3 13 15
861.1Smrg				compress -cdfq "$2" > $tmp
871.1Smrg				files="$files $tmp"
881.1Smrg			else
891.1Smrg				files="$files -"
901.1Smrg				filt="compress -cdfq $2"
911.1Smrg			fi
921.1Smrg			;;
931.1Smrg		*)
941.1Smrg			files="$files $2"
951.1Smrg			;;
961.1Smrg	esac
971.1Smrg	if [ -n "$filt" ]; then
981.1Smrg		$filt | $prog $flags $files
991.1Smrg	else
1001.1Smrg		$prog $flags $files
1011.1Smrg	fi
1021.1Smrg	status=$?
1031.1Smrgelse
1041.1Smrg	echo "$USAGE" 1>&2
1051.1Smrg	exit 1
1061.1Smrgfi
1071.1Smrg
1081.1Smrgexit $status
109