xref: /openbsd-src/usr.bin/diff/diff.c (revision e582024b69ffd25a0b394be3f34fa70ffaaaec9e)
1 /*	$OpenBSD: diff.c,v 1.18 2003/06/27 17:08:45 david 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 <string.h>
41 #include <unistd.h>
42 
43 #include "diff.h"
44 #include "pathnames.h"
45 
46 #if 0
47 static char const sccsid[] = "@(#)diff.c 4.7 5/11/89";
48 #endif
49 
50 /*
51  * diff - driver and subroutines
52  */
53 int	opt;
54 int	aflag;			/* treat all files as text */
55 int	tflag;			/* expand tabs on output */
56 /* Algorithm related options. */
57 int	hflag;			/* -h, use halfhearted DIFFH */
58 int	bflag;			/* ignore blanks in comparisons */
59 int	wflag;			/* totally ignore blanks in comparisons */
60 int	iflag;			/* ignore case in comparisons */
61 /* Options on hierarchical diffs. */
62 int	lflag;			/* long output format with header */
63 int	rflag;			/* recursively trace directories */
64 int	sflag;			/* announce files which are same */
65 char	*start;			/* do file only if name >= this */
66 /* Variables for -D D_IFDEF option. */
67 char	*ifdefname;		/* What we will print for #ifdef/#endif */
68 int	inifdef;
69 /* Variables for -c and -u context option. */
70 int	context;		/* lines of context to be printed */
71 /* State for exit status. */
72 int	status;
73 int	anychange;
74 char	*tempfile;		/* used when comparing against std input */
75 /* Variables for diffdir. */
76 char	**diffargv;		/* option list to pass to recursive diffs */
77 
78 /*
79  * Input file names.
80  * With diffdir, file1 and file2 are allocated BUFSIZ space,
81  * and padded with a '/', and then efile0 and efile1 point after
82  * the '/'.
83  */
84 char	*file1, *file2, *efile1, *efile2;
85 struct	stat stb1, stb2;
86 
87 const char *diff = _PATH_DIFF;
88 const char *diffh = _PATH_DIFFH;
89 const char *pr = _PATH_PR;
90 
91 __dead void usage(void);
92 
93 int
94 main(int argc, char **argv)
95 {
96 	int ch;
97 
98 	status = 2;
99 	diffargv = argv;
100 
101 	while ((ch = getopt(argc, argv, "abC:cD:efhilnrS:stU:uw")) != -1) {
102 		switch (ch) {
103 		case 'a':
104 			aflag++;
105 			break;
106 		case 'b':
107 			bflag++;
108 			break;
109 		case 'C':
110 			opt = D_CONTEXT;
111 			if (!isdigit(*optarg))
112 				usage();
113 			context = atoi(optarg);	/* XXX - use strtol */
114 			break;
115 		case 'c':
116 			opt = D_CONTEXT;
117 			context = 3;
118 			break;
119 		case 'D':
120 			opt = D_IFDEF;
121 			ifdefname = optarg;
122 			break;
123 		case 'e':
124 			opt = D_EDIT;
125 			break;
126 		case 'f':
127 			opt = D_REVERSE;
128 			break;
129 		case 'h':
130 			hflag++;
131 			break;
132 		case 'i':
133 			iflag++;
134 			break;
135 		case 'l':
136 			lflag++;
137 			break;
138 		case 'n':
139 			opt = D_NREVERSE;
140 			break;
141 		case 'r':
142 			rflag++;
143 			break;
144 		case 'S':
145 			start = optarg;
146 			break;
147 		case 's':
148 			sflag++;
149 			break;
150 		case 't':
151 			tflag++;
152 			break;
153 		case 'U':
154 			opt = D_UNIFIED;
155 			if (!isdigit(*optarg))
156 				usage();
157 			context = atoi(optarg);	/* XXX - use strtol */
158 			break;
159 		case 'u':
160 			opt = D_UNIFIED;
161 			context = 3;
162 			break;
163 		case 'w':
164 			wflag++;
165 			break;
166 		default:
167 			usage();
168 			break;
169 		}
170 	}
171 	argc -= optind;
172 	argv += optind;
173 
174 	if (argc != 2)
175 		errorx("two filename arguments required");
176 	file1 = argv[0];
177 	file2 = argv[1];
178 	if (hflag && opt)
179 		errorx("-h doesn't support -D, -c, -C, -e, -f, -I, -n, -u or -U");
180 	if (!strcmp(file1, "-"))
181 		stb1.st_mode = S_IFREG;
182 	else if (stat(file1, &stb1) < 0)
183 		error("%s", file1);
184 	if (!strcmp(file2, "-"))
185 		stb2.st_mode = S_IFREG;
186 	else if (stat(file2, &stb2) < 0)
187 		error("%s", file2);
188 	if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode))
189 		diffdir(argv);
190 	else
191 		diffreg();
192 	done(0);
193 }
194 
195 int
196 min(int a, int b)
197 {
198 
199 	return (a < b ? a : b);
200 }
201 
202 int
203 max(int a, int b)
204 {
205 
206 	return (a > b ? a : b);
207 }
208 
209 __dead void
210 done(int sig)
211 {
212 	if (tempfiles[0] != NULL)
213 		unlink(tempfiles[0]);
214 	if (tempfiles[1] != NULL)
215 		unlink(tempfiles[1]);
216 	if (sig)
217 		_exit(status);
218 	exit(status);
219 }
220 
221 void *
222 emalloc(size_t n)
223 {
224 	void *p;
225 
226 	if ((p = malloc(n)) == NULL)
227 		error("files too big, try -h");
228 	return (p);
229 }
230 
231 void *
232 erealloc(void *p, size_t n)
233 {
234 	void *q;
235 
236 	if ((q = realloc(p, n)) == NULL)
237 		error("files too big, try -h");
238 	return (q);
239 }
240 
241 __dead void
242 error(const char *fmt, ...)
243 {
244 	va_list ap;
245 	int sverrno = errno;
246 
247 	if (tempfiles[0] != NULL)
248 		unlink(tempfiles[0]);
249 	if (tempfiles[1] != NULL)
250 		unlink(tempfiles[1]);
251 	errno = sverrno;
252 	va_start(ap, fmt);
253 	verr(status, fmt, ap);
254 	va_end(ap);
255 }
256 
257 __dead void
258 errorx(const char *fmt, ...)
259 {
260 	va_list ap;
261 
262 	if (tempfiles[0] != NULL)
263 		unlink(tempfiles[0]);
264 	if (tempfiles[1] != NULL)
265 		unlink(tempfiles[1]);
266 	va_start(ap, fmt);
267 	verrx(status, fmt, ap);
268 	va_end(ap);
269 }
270 
271 __dead void
272 usage(void)
273 {
274 	(void)fprintf(stderr,
275 	    "usage: diff [-bitw] [-c | -e | -f | -h | -n | -u ] file1 file2\n"
276 	    "       diff [-bitw] -C number file1 file2\n"
277 	    "       diff [-bitw] -D string file1 file2\n"
278 	    "       diff [-bitw] -U number file1 file2\n"
279 	    "       diff [-biwt] [-c | -e | -f | -h | -n | -u ] "
280 	    "[-l] [-r] [-s] [-S name]\n            dir1 dir2\n");
281 
282 	exit(2);
283 }
284