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