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