xref: /openbsd-src/usr.bin/diff/diff.c (revision 66e5764e648f1cef39cdf0423fa96337157ae2bb)
1 /*	$OpenBSD: diff.c,v 1.15 2003/06/26 18:19:29 millert Exp $	*/
2 
3 /*
4  * Copyright (C) Caldera International Inc.  2001-2002.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code and documentation must retain the above
11  *    copyright notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed or owned by Caldera
18  *	International, Inc.
19  * 4. Neither the name of Caldera International, Inc. nor the names of other
20  *    contributors may be used to endorse or promote products derived from
21  *    this software without specific prior written permission.
22  *
23  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28  * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include <errno.h>
38 #include <stdlib.h>
39 #include <stdarg.h>
40 #include <unistd.h>
41 
42 #include "diff.h"
43 #include "pathnames.h"
44 
45 #if 0
46 static char const sccsid[] = "@(#)diff.c 4.7 5/11/89";
47 #endif
48 
49 /*
50  * diff - driver and subroutines
51  */
52 int	opt;
53 int	aflag;			/* treat all files as text */
54 int	tflag;			/* expand tabs on output */
55 /* Algorithm related options. */
56 int	hflag;			/* -h, use halfhearted DIFFH */
57 int	bflag;			/* ignore blanks in comparisons */
58 int	wflag;			/* totally ignore blanks in comparisons */
59 int	iflag;			/* ignore case in comparisons */
60 /* Options on hierarchical diffs. */
61 int	lflag;			/* long output format with header */
62 int	rflag;			/* recursively trace directories */
63 int	sflag;			/* announce files which are same */
64 char	*start;			/* do file only if name >= this */
65 /* Variables for -I D_IFDEF option. */
66 int	wantelses;		/* -E */
67 char	*ifdef1;		/* String for -1 */
68 char	*ifdef2;		/* String for -2 */
69 char	*endifname;		/* What we will print on next #endif */
70 int	inifdef;
71 /* Variables for -c and -u context option. */
72 int	context;		/* lines of context to be printed */
73 /* State for exit status. */
74 int	status;
75 int	anychange;
76 char	*tempfile;		/* used when comparing against std input */
77 /* Variables for diffdir. */
78 char	**diffargv;		/* option list to pass to recursive diffs */
79 
80 /*
81  * Input file names.
82  * With diffdir, file1 and file2 are allocated BUFSIZ space,
83  * and padded with a '/', and then efile0 and efile1 point after
84  * the '/'.
85  */
86 char	*file1, *file2, *efile1, *efile2;
87 struct	stat stb1, stb2;
88 
89 const char *diff = _PATH_DIFF;
90 const char *diffh = _PATH_DIFFH;
91 const char *pr = _PATH_PR;
92 
93 __dead void usage(void);
94 
95 int
96 main(int argc, char **argv)
97 {
98 	int ch;
99 
100 	ifdef1 = "FILE1";
101 	ifdef2 = "FILE2";
102 	status = 2;
103 	diffargv = argv;
104 
105 	while ((ch = getopt(argc, argv, "abC:cD:efhilnrS:stU:uw")) != -1) {
106 		switch (ch) {
107 		case 'a':
108 			aflag++;
109 			break;
110 		case 'b':
111 			bflag++;
112 			break;
113 		case 'C':
114 			opt = D_CONTEXT;
115 			if (!isdigit(*optarg))
116 				usage();
117 			context = atoi(optarg);	/* XXX - use strtol */
118 			break;
119 		case 'c':
120 			opt = D_CONTEXT;
121 			context = 3;
122 			break;
123 		case 'D':
124 			/* -Dfoo = -E -1 -2foo */
125 			opt = D_IFDEF;
126 			ifdef1 = "";
127 			ifdef2 = optarg;
128 			wantelses++;
129 			break;
130 		case 'e':
131 			opt = D_EDIT;
132 			break;
133 		case 'f':
134 			opt = D_REVERSE;
135 			break;
136 		case 'h':
137 			hflag++;
138 			break;
139 		case 'i':
140 			iflag++;
141 			break;
142 		case 'l':
143 			lflag++;
144 			break;
145 		case 'n':
146 			opt = D_NREVERSE;
147 			break;
148 		case 'r':
149 			opt = D_REVERSE;
150 			break;
151 		case 'S':
152 			start = optarg;
153 			break;
154 		case 's':
155 			sflag++;
156 			break;
157 		case 't':
158 			tflag++;
159 			break;
160 		case 'U':
161 			opt = D_UNIFIED;
162 			if (!isdigit(*optarg))
163 				usage();
164 			context = atoi(optarg);	/* XXX - use strtol */
165 			break;
166 		case 'u':
167 			opt = D_UNIFIED;
168 			context = 3;
169 			break;
170 		case 'w':
171 			wflag++;
172 			break;
173 		default:
174 			usage();
175 			break;
176 		}
177 	}
178 	argc -= optind;
179 	argv += optind;
180 
181 	if (argc != 2)
182 		errorx("two filename arguments required");
183 	file1 = argv[0];
184 	file2 = argv[1];
185 	if (hflag && opt)
186 		errorx("-h doesn't support -D, -c, -C, -e, -f, -I, -n, -u or -U");
187 	if (!strcmp(file1, "-"))
188 		stb1.st_mode = S_IFREG;
189 	else if (stat(file1, &stb1) < 0)
190 		error("%s", file1);
191 	if (!strcmp(file2, "-"))
192 		stb2.st_mode = S_IFREG;
193 	else if (stat(file2, &stb2) < 0)
194 		error("%s", file2);
195 	if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode))
196 		diffdir(argv);
197 	else
198 		diffreg();
199 	done(0);
200 }
201 
202 int
203 min(int a, int b)
204 {
205 
206 	return (a < b ? a : b);
207 }
208 
209 int
210 max(int a, int b)
211 {
212 
213 	return (a > b ? a : b);
214 }
215 
216 __dead void
217 done(int sig)
218 {
219 	if (tempfiles[0] != NULL)
220 		unlink(tempfiles[0]);
221 	if (tempfiles[1] != NULL)
222 		unlink(tempfiles[1]);
223 	if (sig)
224 		_exit(status);
225 	exit(status);
226 }
227 
228 void *
229 emalloc(size_t n)
230 {
231 	void *p;
232 
233 	if ((p = malloc(n)) == NULL)
234 		error("files too big, try -h");
235 	return (p);
236 }
237 
238 void *
239 erealloc(void *p, size_t n)
240 {
241 	void *q;
242 
243 	if ((q = realloc(p, n)) == NULL)
244 		error("files too big, try -h");
245 	return (q);
246 }
247 
248 __dead void
249 error(const char *fmt, ...)
250 {
251 	va_list ap;
252 	int sverrno = errno;
253 
254 	if (tempfiles[0] != NULL)
255 		unlink(tempfiles[0]);
256 	if (tempfiles[1] != NULL)
257 		unlink(tempfiles[1]);
258 	errno = sverrno;
259 	va_start(ap, fmt);
260 	verr(status, fmt, ap);
261 	va_end(ap);
262 }
263 
264 __dead void
265 errorx(const char *fmt, ...)
266 {
267 	va_list ap;
268 
269 	if (tempfiles[0] != NULL)
270 		unlink(tempfiles[0]);
271 	if (tempfiles[1] != NULL)
272 		unlink(tempfiles[1]);
273 	va_start(ap, fmt);
274 	verrx(status, fmt, ap);
275 	va_end(ap);
276 }
277 
278 __dead void
279 usage(void)
280 {
281 	(void)fprintf(stderr,
282 	    "usage: diff [-bitw] [-c | -e | -f | -h | -n | -u ] file1 file2\n"
283 	    "       diff [-bitw] -C number file1 file2\n"
284 	    "       diff [-bitw] -D string file1 file2\n"
285 	    "       diff [-bitw] -U number file1 file2\n"
286 	    "       diff [-biwt] [-c | -e | -f | -h | -n | -u ] "
287 	    "[-l] [-r] [-s] [-S name]\n            dir1 dir2\n");
288 
289 	exit(2);
290 }
291