xref: /openbsd-src/usr.bin/diff/diff.c (revision 90f56ad89a9a2865b738e2fb968a31f24e4f9aa7)
1*90f56ad8Smillert /*	$OpenBSD: diff.c,v 1.17 2003/06/26 22:04:45 millert Exp $	*/
2d0c3f575Sderaadt 
3d0c3f575Sderaadt /*
4d0c3f575Sderaadt  * Copyright (C) Caldera International Inc.  2001-2002.
5d0c3f575Sderaadt  * All rights reserved.
6d0c3f575Sderaadt  *
7d0c3f575Sderaadt  * Redistribution and use in source and binary forms, with or without
8d0c3f575Sderaadt  * modification, are permitted provided that the following conditions
9d0c3f575Sderaadt  * are met:
10d0c3f575Sderaadt  * 1. Redistributions of source code and documentation must retain the above
11d0c3f575Sderaadt  *    copyright notice, this list of conditions and the following disclaimer.
12d0c3f575Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
13d0c3f575Sderaadt  *    notice, this list of conditions and the following disclaimer in the
14d0c3f575Sderaadt  *    documentation and/or other materials provided with the distribution.
15d0c3f575Sderaadt  * 3. All advertising materials mentioning features or use of this software
16d0c3f575Sderaadt  *    must display the following acknowledgement:
17d0c3f575Sderaadt  *	This product includes software developed or owned by Caldera
18d0c3f575Sderaadt  *	International, Inc.
19d0c3f575Sderaadt  * 4. Neither the name of Caldera International, Inc. nor the names of other
20d0c3f575Sderaadt  *    contributors may be used to endorse or promote products derived from
21d0c3f575Sderaadt  *    this software without specific prior written permission.
22d0c3f575Sderaadt  *
23d0c3f575Sderaadt  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24d0c3f575Sderaadt  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25d0c3f575Sderaadt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26d0c3f575Sderaadt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27d0c3f575Sderaadt  * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28d0c3f575Sderaadt  * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29d0c3f575Sderaadt  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30d0c3f575Sderaadt  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31d0c3f575Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32d0c3f575Sderaadt  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33d0c3f575Sderaadt  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34d0c3f575Sderaadt  * POSSIBILITY OF SUCH DAMAGE.
35d0c3f575Sderaadt  */
36d0c3f575Sderaadt 
3766e5764eSmillert #include <errno.h>
3826da422aStedu #include <stdlib.h>
3966e5764eSmillert #include <stdarg.h>
4026da422aStedu #include <unistd.h>
41ae8d569bSderaadt 
42ae8d569bSderaadt #include "diff.h"
43ae8d569bSderaadt #include "pathnames.h"
44ae8d569bSderaadt 
4526da422aStedu #if 0
4626da422aStedu static char const sccsid[] = "@(#)diff.c 4.7 5/11/89";
4726da422aStedu #endif
4826da422aStedu 
49ae8d569bSderaadt /*
50ae8d569bSderaadt  * diff - driver and subroutines
51ae8d569bSderaadt  */
52d5d5ac6cStedu int	opt;
53d5d5ac6cStedu int	aflag;			/* treat all files as text */
54d5d5ac6cStedu int	tflag;			/* expand tabs on output */
55d5d5ac6cStedu /* Algorithm related options. */
56d5d5ac6cStedu int	hflag;			/* -h, use halfhearted DIFFH */
57d5d5ac6cStedu int	bflag;			/* ignore blanks in comparisons */
58d5d5ac6cStedu int	wflag;			/* totally ignore blanks in comparisons */
59d5d5ac6cStedu int	iflag;			/* ignore case in comparisons */
60d5d5ac6cStedu /* Options on hierarchical diffs. */
61d5d5ac6cStedu int	lflag;			/* long output format with header */
62d5d5ac6cStedu int	rflag;			/* recursively trace directories */
63d5d5ac6cStedu int	sflag;			/* announce files which are same */
64d5d5ac6cStedu char	*start;			/* do file only if name >= this */
65*90f56ad8Smillert /* Variables for -D D_IFDEF option. */
66*90f56ad8Smillert char	*ifdefname;		/* What we will print for #ifdef/#endif */
67d5d5ac6cStedu int	inifdef;
68d5d5ac6cStedu /* Variables for -c and -u context option. */
69d5d5ac6cStedu int	context;		/* lines of context to be printed */
70d5d5ac6cStedu /* State for exit status. */
71d5d5ac6cStedu int	status;
72d5d5ac6cStedu int	anychange;
73d5d5ac6cStedu char	*tempfile;		/* used when comparing against std input */
74d5d5ac6cStedu /* Variables for diffdir. */
75d5d5ac6cStedu char	**diffargv;		/* option list to pass to recursive diffs */
76d5d5ac6cStedu 
77d5d5ac6cStedu /*
78d5d5ac6cStedu  * Input file names.
79d5d5ac6cStedu  * With diffdir, file1 and file2 are allocated BUFSIZ space,
80d5d5ac6cStedu  * and padded with a '/', and then efile0 and efile1 point after
81d5d5ac6cStedu  * the '/'.
82d5d5ac6cStedu  */
83d5d5ac6cStedu char	*file1, *file2, *efile1, *efile2;
84d5d5ac6cStedu struct	stat stb1, stb2;
85ae8d569bSderaadt 
8648b947b7Smillert const char *diff = _PATH_DIFF;
8748b947b7Smillert const char *diffh = _PATH_DIFFH;
8848b947b7Smillert const char *pr = _PATH_PR;
89ae8d569bSderaadt 
90c42aed39Smillert __dead void usage(void);
91ae8d569bSderaadt 
9226da422aStedu int
9326da422aStedu main(int argc, char **argv)
9426da422aStedu {
95c42aed39Smillert 	int ch;
9626da422aStedu 
97ae8d569bSderaadt 	status = 2;
98ae8d569bSderaadt 	diffargv = argv;
99c42aed39Smillert 
100d5d5ac6cStedu 	while ((ch = getopt(argc, argv, "abC:cD:efhilnrS:stU:uw")) != -1) {
101c42aed39Smillert 		switch (ch) {
102d5d5ac6cStedu 		case 'a':
103d5d5ac6cStedu 			aflag++;
104d5d5ac6cStedu 			break;
105ae8d569bSderaadt 		case 'b':
106c42aed39Smillert 			bflag++;
107c42aed39Smillert 			break;
108c42aed39Smillert 		case 'C':
109c42aed39Smillert 			opt = D_CONTEXT;
110c42aed39Smillert 			if (!isdigit(*optarg))
111c42aed39Smillert 				usage();
112c42aed39Smillert 			context = atoi(optarg);	/* XXX - use strtol */
113c42aed39Smillert 			break;
114ae8d569bSderaadt 		case 'c':
115ae8d569bSderaadt 			opt = D_CONTEXT;
116ae8d569bSderaadt 			context = 3;
117c42aed39Smillert 			break;
118c42aed39Smillert 		case 'D':
119c42aed39Smillert 			opt = D_IFDEF;
120*90f56ad8Smillert 			ifdefname = optarg;
121c42aed39Smillert 			break;
122c42aed39Smillert 		case 'e':
123c42aed39Smillert 			opt = D_EDIT;
124c42aed39Smillert 			break;
125c42aed39Smillert 		case 'f':
126c42aed39Smillert 			opt = D_REVERSE;
127c42aed39Smillert 			break;
128ae8d569bSderaadt 		case 'h':
129ae8d569bSderaadt 			hflag++;
130c42aed39Smillert 			break;
131c42aed39Smillert 		case 'i':
132c42aed39Smillert 			iflag++;
133c42aed39Smillert 			break;
134ae8d569bSderaadt 		case 'l':
135ae8d569bSderaadt 			lflag++;
136c42aed39Smillert 			break;
137c42aed39Smillert 		case 'n':
138c42aed39Smillert 			opt = D_NREVERSE;
139c42aed39Smillert 			break;
140c42aed39Smillert 		case 'r':
141d0c85965Smillert 			rflag++;
142c42aed39Smillert 			break;
143c42aed39Smillert 		case 'S':
144c42aed39Smillert 			start = optarg;
145c42aed39Smillert 			break;
146c42aed39Smillert 		case 's':
147c42aed39Smillert 			sflag++;
148c42aed39Smillert 			break;
149c42aed39Smillert 		case 't':
150c42aed39Smillert 			tflag++;
151c42aed39Smillert 			break;
1529de32c1bSmillert 		case 'U':
1539de32c1bSmillert 			opt = D_UNIFIED;
1549de32c1bSmillert 			if (!isdigit(*optarg))
1559de32c1bSmillert 				usage();
1569de32c1bSmillert 			context = atoi(optarg);	/* XXX - use strtol */
1579de32c1bSmillert 			break;
1589de32c1bSmillert 		case 'u':
1599de32c1bSmillert 			opt = D_UNIFIED;
1609de32c1bSmillert 			context = 3;
1619de32c1bSmillert 			break;
162c42aed39Smillert 		case 'w':
163c42aed39Smillert 			wflag++;
164c42aed39Smillert 			break;
165ae8d569bSderaadt 		default:
166c42aed39Smillert 			usage();
167c42aed39Smillert 			break;
168ae8d569bSderaadt 		}
169ae8d569bSderaadt 	}
170c42aed39Smillert 	argc -= optind;
171c42aed39Smillert 	argv += optind;
172c42aed39Smillert 
173c42aed39Smillert 	if (argc != 2)
17466e5764eSmillert 		errorx("two filename arguments required");
175ae8d569bSderaadt 	file1 = argv[0];
176ae8d569bSderaadt 	file2 = argv[1];
177c42aed39Smillert 	if (hflag && opt)
17866e5764eSmillert 		errorx("-h doesn't support -D, -c, -C, -e, -f, -I, -n, -u or -U");
179ae8d569bSderaadt 	if (!strcmp(file1, "-"))
180ae8d569bSderaadt 		stb1.st_mode = S_IFREG;
181c42aed39Smillert 	else if (stat(file1, &stb1) < 0)
18266e5764eSmillert 		error("%s", file1);
183ae8d569bSderaadt 	if (!strcmp(file2, "-"))
184ae8d569bSderaadt 		stb2.st_mode = S_IFREG;
185c42aed39Smillert 	else if (stat(file2, &stb2) < 0)
18666e5764eSmillert 		error("%s", file2);
18749dffe13Smillert 	if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode))
188ae8d569bSderaadt 		diffdir(argv);
18949dffe13Smillert 	else
190ae8d569bSderaadt 		diffreg();
191a6a14831Sderaadt 	done(0);
192ae8d569bSderaadt }
193ae8d569bSderaadt 
19426da422aStedu int
19526da422aStedu min(int a, int b)
196ae8d569bSderaadt {
197ae8d569bSderaadt 
198ae8d569bSderaadt 	return (a < b ? a : b);
199ae8d569bSderaadt }
200ae8d569bSderaadt 
20126da422aStedu int
20226da422aStedu max(int a, int b)
203ae8d569bSderaadt {
204ae8d569bSderaadt 
205ae8d569bSderaadt 	return (a > b ? a : b);
206ae8d569bSderaadt }
207ae8d569bSderaadt 
208c42aed39Smillert __dead void
209a6a14831Sderaadt done(int sig)
210ae8d569bSderaadt {
21166e5764eSmillert 	if (tempfiles[0] != NULL)
21248b947b7Smillert 		unlink(tempfiles[0]);
21366e5764eSmillert 	if (tempfiles[1] != NULL)
21448b947b7Smillert 		unlink(tempfiles[1]);
215a6a14831Sderaadt 	if (sig)
216a6a14831Sderaadt 		_exit(status);
217ae8d569bSderaadt 	exit(status);
218ae8d569bSderaadt }
219ae8d569bSderaadt 
22026da422aStedu void *
22149dffe13Smillert emalloc(size_t n)
222ae8d569bSderaadt {
22326da422aStedu 	void *p;
224ae8d569bSderaadt 
22526da422aStedu 	if ((p = malloc(n)) == NULL)
22666e5764eSmillert 		error("files too big, try -h");
22726da422aStedu 	return (p);
22826da422aStedu }
22926da422aStedu 
23026da422aStedu void *
23149dffe13Smillert erealloc(void *p, size_t n)
23226da422aStedu {
23326da422aStedu 	void *q;
23426da422aStedu 
23526da422aStedu 	if ((q = realloc(p, n)) == NULL)
23666e5764eSmillert 		error("files too big, try -h");
237ae8d569bSderaadt 	return (q);
238ae8d569bSderaadt }
239ae8d569bSderaadt 
24066e5764eSmillert __dead void
24166e5764eSmillert error(const char *fmt, ...)
242ae8d569bSderaadt {
24366e5764eSmillert 	va_list ap;
24466e5764eSmillert 	int sverrno = errno;
24566e5764eSmillert 
24666e5764eSmillert 	if (tempfiles[0] != NULL)
24766e5764eSmillert 		unlink(tempfiles[0]);
24866e5764eSmillert 	if (tempfiles[1] != NULL)
24966e5764eSmillert 		unlink(tempfiles[1]);
25066e5764eSmillert 	errno = sverrno;
25166e5764eSmillert 	va_start(ap, fmt);
25266e5764eSmillert 	verr(status, fmt, ap);
25366e5764eSmillert 	va_end(ap);
25466e5764eSmillert }
25566e5764eSmillert 
25666e5764eSmillert __dead void
25766e5764eSmillert errorx(const char *fmt, ...)
25866e5764eSmillert {
25966e5764eSmillert 	va_list ap;
26066e5764eSmillert 
26166e5764eSmillert 	if (tempfiles[0] != NULL)
26266e5764eSmillert 		unlink(tempfiles[0]);
26366e5764eSmillert 	if (tempfiles[1] != NULL)
26466e5764eSmillert 		unlink(tempfiles[1]);
26566e5764eSmillert 	va_start(ap, fmt);
26666e5764eSmillert 	verrx(status, fmt, ap);
26766e5764eSmillert 	va_end(ap);
268ae8d569bSderaadt }
269c42aed39Smillert 
270c42aed39Smillert __dead void
271c42aed39Smillert usage(void)
272c42aed39Smillert {
273c012fe98Sderaadt 	(void)fprintf(stderr,
274c012fe98Sderaadt 	    "usage: diff [-bitw] [-c | -e | -f | -h | -n | -u ] file1 file2\n"
2758dd7bf08Smillert 	    "       diff [-bitw] -C number file1 file2\n"
2768dd7bf08Smillert 	    "       diff [-bitw] -D string file1 file2\n"
2778dd7bf08Smillert 	    "       diff [-bitw] -U number file1 file2\n"
278c012fe98Sderaadt 	    "       diff [-biwt] [-c | -e | -f | -h | -n | -u ] "
279c012fe98Sderaadt 	    "[-l] [-r] [-s] [-S name]\n            dir1 dir2\n");
280c42aed39Smillert 
28166e5764eSmillert 	exit(2);
282c42aed39Smillert }
283