xref: /openbsd-src/usr.bin/compress/zdiff (revision bf198cc6eba0ca1f6d79f71e8e2243d386241fa8)
1479fbb82Smillert#!/bin/sh -
2479fbb82Smillert#
3*bf198cc6Smillert# $OpenBSD: zdiff,v 1.3 2019/01/25 00:19:26 millert Exp $
472223939Sotto#
5*bf198cc6Smillert# Copyright (c) 2003 Todd C. Miller <millert@openbsd.org>
6479fbb82Smillert#
7479fbb82Smillert# Permission to use, copy, modify, and distribute this software for any
8479fbb82Smillert# purpose with or without fee is hereby granted, provided that the above
9479fbb82Smillert# copyright notice and this permission notice appear in all copies.
10479fbb82Smillert#
11479fbb82Smillert# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12479fbb82Smillert# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13479fbb82Smillert# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14479fbb82Smillert# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15479fbb82Smillert# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16479fbb82Smillert# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17479fbb82Smillert# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18479fbb82Smillert#
19479fbb82Smillert# Sponsored in part by the Defense Advanced Research Projects
20479fbb82Smillert# Agency (DARPA) and Air Force Research Laboratory, Air Force
21479fbb82Smillert# Materiel Command, USAF, under agreement number F39502-99-1-0512.
22479fbb82Smillert#
23479fbb82Smillert
24479fbb82Smillert# Set $prog based on $0
25479fbb82Smillertcase $0 in
26479fbb82Smillert	*cmp)	prog=cmp
27479fbb82Smillert		;;
28479fbb82Smillert	*)	prog=diff
29479fbb82Smillert		;;
30479fbb82Smillertesac
31479fbb82SmillertUSAGE="usage: z$prog [options] file1 [file2]"
32479fbb82Smillert
33479fbb82Smillert# Pull out any command line flags so we can pass them to diff/cmp
34479fbb82Smillert# XXX - assumes there is no optarg
35479fbb82Smillertflags=
36479fbb82Smillertwhile test $# -ne 0; do
37479fbb82Smillert	case "$1" in
38479fbb82Smillert		--)
39479fbb82Smillert			shift
40479fbb82Smillert			break
41479fbb82Smillert			;;
42479fbb82Smillert		-*)
43479fbb82Smillert			flags="$flags $1"
44479fbb82Smillert			shift
45479fbb82Smillert			;;
46479fbb82Smillert		*)
47479fbb82Smillert			break
48479fbb82Smillert			;;
49479fbb82Smillert	esac
50479fbb82Smillertdone
51479fbb82Smillert
52479fbb82Smillertif [ $# -eq 1 ]; then
53479fbb82Smillert	# One file given, compare compressed to uncompressed
54479fbb82Smillert	files="$1"
55479fbb82Smillert	case "$1" in
56479fbb82Smillert		*[._-][Zz])
57479fbb82Smillert			files="${1%??}"
58479fbb82Smillert			;;
59479fbb82Smillert		*[._-]gz)
60479fbb82Smillert			files="${1%???}"
61479fbb82Smillert			;;
62479fbb82Smillert		*.t[ag]z)
63479fbb82Smillert			files="${1%??}"ar
64479fbb82Smillert			;;
65479fbb82Smillert		*)	echo "z$prog: unknown suffix" 1>&2
66479fbb82Smillert			exit 1
67479fbb82Smillert	esac
68479fbb82Smillert	compress -cdfq "$1" | $prog $flags - "$files"
69479fbb82Smillert	status=$?
70479fbb82Smillertelif [ $# -eq 2 ]; then
71479fbb82Smillert	# Two files given, compare the two uncompressing as needed
72479fbb82Smillert	case "$1" in
73479fbb82Smillert		*[._-][Zz]|*[._-]gz|*.t[ag]z)
74479fbb82Smillert			files=-
75479fbb82Smillert			filt="compress -cdfq $1"
76479fbb82Smillert			;;
77479fbb82Smillert		*)
78479fbb82Smillert			files="$1"
79479fbb82Smillert			;;
80479fbb82Smillert	esac
81479fbb82Smillert	case "$2" in
82479fbb82Smillert		*[._-][Zz]|*[._-]gz|*.t[ag]z)
83479fbb82Smillert			if [ "$files" = "-" ]; then
84479fbb82Smillert				tmp=`mktemp -t z$prog.XXXXXXXXXX` || exit 1
85479fbb82Smillert				trap "rm -f $tmp" 0 1 2 3 13 15
86479fbb82Smillert				compress -cdfq "$2" > $tmp
87479fbb82Smillert				files="$files $tmp"
88479fbb82Smillert			else
89479fbb82Smillert				files="$files -"
90479fbb82Smillert				filt="compress -cdfq $2"
91479fbb82Smillert			fi
92479fbb82Smillert			;;
93479fbb82Smillert		*)
94479fbb82Smillert			files="$files $2"
95479fbb82Smillert			;;
96479fbb82Smillert	esac
97479fbb82Smillert	if [ -n "$filt" ]; then
98479fbb82Smillert		$filt | $prog $flags $files
99479fbb82Smillert	else
100479fbb82Smillert		$prog $flags $files
101479fbb82Smillert	fi
102479fbb82Smillert	status=$?
103479fbb82Smillertelse
104479fbb82Smillert	echo "$USAGE" 1>&2
105479fbb82Smillert	exit 1
106479fbb82Smillertfi
107479fbb82Smillert
108479fbb82Smillertexit $status
109