xref: /illumos-gate/usr/src/cmd/diff3/diff3.sh (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
1#!/usr/bin/sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License, Version 1.0 only
7# (the "License").  You may not use this file except in compliance
8# with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or http://www.opensolaris.org/os/licensing.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23#	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
24#	  All Rights Reserved
25
26
27#	Copyright (c) 1999, 2001 by Sun Microsystems, Inc.
28#	All rights reserved.
29
30usage="usage: diff3 file1 file2 file3"
31
32# mktmpdir - Create a private (mode 0700) temporary directory inside of /tmp
33# for this process's temporary files.  We set up a trap to remove the
34# directory on exit (trap 0), and also on SIGHUP, SIGINT, SIGQUIT, SIGPIPE,
35# and SIGTERM.
36
37mktmpdir() {
38	tmpdir=/tmp/diff3.$$
39	trap '/usr/bin/rm -rf $tmpdir' 0 1 2 3 13 15
40	/usr/bin/mkdir -m 700 $tmpdir || exit 1
41}
42mktmpdir
43
44e=
45case $1 in
46-*)
47	e=$1
48	shift;;
49esac
50if [ $# != 3 ]; then
51	echo ${usage} 1>&2
52	exit 1
53fi
54if [ \( -f $1 -o -c $1 \) -a \( -f $2 -o -c $2 \) -a \( -f $3 -o -c $3 \) ]; then
55	:
56else
57	echo ${usage} 1>&2
58	exit 1
59fi
60f1=$1 f2=$2 f3=$3
61if [ -c $f1 ]
62then
63	/usr/bin/cat $f1 > $tmpdir/d3c$$
64	f1=$tmpdir/d3c$$
65fi
66if [ -c $f2 ]
67then
68	/usr/bin/cat $f2 > $tmpdir/d3d$$
69	f2=$tmpdir/d3d$$
70fi
71if [ -c $f3 ]
72then
73	/usr/bin/cat $f3 > $tmpdir/d3e$$
74	f3=$tmpdir/d3e$$
75fi
76
77/usr/bin/diff $f1 $f3 > $tmpdir/d3a$$ 2> $tmpdir/d3a$$.err
78STATUS=$?
79if [ $STATUS -eq 1 ]
80then
81	/usr/bin/grep -q "^[<>]" $tmpdir/d3a$$
82	RET=$?
83	if [ $RET -eq 1 ]
84	then
85		/usr/bin/cat $tmpdir/d3a$$
86		exit $STATUS
87	fi
88
89	if [ $RET -gt 1 ]
90	then
91		echo "diff3 failed" 1>&2
92		exit $STATUS
93	fi
94fi
95
96if [ $STATUS -gt 1 ]
97then
98	/usr/bin/cat $tmpdir/d3a$$.err
99	exit $STATUS
100fi
101
102/usr/bin/diff $f2 $f3 > $tmpdir/d3b$$ 2> $tmpdir/d3b$$.err
103STATUS=$?
104if [ $STATUS -eq 1 ]
105then
106	/usr/bin/grep -q "^[<>]" $tmpdir/d3b$$
107	RET=$?
108	if [ $RET -eq 1 ]
109	then
110		/usr/bin/cat $tmpdir/d3b$$
111		exit $STATUS
112	fi
113
114	if [ $RET -gt 1 ]
115	then
116		echo "diff3 failed" 1>&2
117		exit $STATUS
118	fi
119fi
120
121if [ $STATUS -gt 1 ]
122then
123	/usr/bin/cat $tmpdir/d3b$$.err
124	exit $STATUS
125fi
126
127/usr/lib/diff3prog $e $tmpdir/d3[ab]$$ $f1 $f2 $f3
128