xref: /openbsd-src/usr.bin/diff/diff.c (revision ccd55a2c66ccad56efb56a57f629414c2b2ba510)
1 /*	$OpenBSD: diff.c,v 1.46 2004/06/20 18:47:45 otto Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * Sponsored in part by the Defense Advanced Research Projects
19  * Agency (DARPA) and Air Force Research Laboratory, Air Force
20  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
21  */
22 
23 #ifndef lint
24 static const char rcsid[] = "$OpenBSD: diff.c,v 1.46 2004/06/20 18:47:45 otto Exp $";
25 #endif /* not lint */
26 
27 #include <sys/param.h>
28 #include <sys/stat.h>
29 
30 #include <ctype.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <getopt.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include "diff.h"
42 
43 int	 aflag, bflag, dflag, iflag, lflag, Nflag, Pflag, pflag, rflag;
44 int	 sflag, tflag, Tflag, wflag;
45 int	 format, context, status;
46 char	*start, *ifdefname, *diffargs, *label, *ignore_pats;
47 struct stat stb1, stb2;
48 struct excludes *excludes_list;
49 regex_t	 ignore_re;
50 
51 #define	OPTIONS	"0123456789abC:cdD:efhI:iL:lnNPpqrS:sTtU:uwX:x:"
52 static struct option longopts[] = {
53 	{ "text",			no_argument,		0,	'a' },
54 	{ "ignore-space-change",	no_argument,		0,	'b' },
55 	{ "context",			optional_argument,	0,	'C' },
56 	{ "ifdef",			required_argument,	0,	'D' },
57 	{ "minimal",			no_argument,		0,	'd' },
58 	{ "ed",				no_argument,		0,	'e' },
59 	{ "forward-ed",			no_argument,		0,	'f' },
60 	{ "ignore-matching-lines",	required_argument,	0,	'I' },
61 	{ "ignore-case",		no_argument,		0,	'i' },
62 	{ "paginate",			no_argument,		0,	'l' },
63 	{ "label",			required_argument,	0,	'L' },
64 	{ "new-file",			no_argument,		0,	'N' },
65 	{ "rcs",			no_argument,		0,	'n' },
66 	{ "unidirectional-new-file",	no_argument,		0,	'P' },
67 	{ "show-c-function",		no_argument,		0,	'p' },
68 	{ "brief",			no_argument,		0,	'q' },
69 	{ "recursive",			no_argument,		0,	'r' },
70 	{ "report-identical-files",	no_argument,		0,	's' },
71 	{ "starting-file",		required_argument,	0,	'S' },
72 	{ "expand-tabs",		no_argument,		0,	't' },
73 	{ "initial-tab",		no_argument,		0,	'T' },
74 	{ "unified",			optional_argument,	0,	'U' },
75 	{ "ignore-all-space",		no_argument,		0,	'w' },
76 	{ "exclude",			required_argument,	0,	'x' },
77 	{ "exclude-from",		required_argument,	0,	'X' },
78 	{ NULL,				0,			0,	'\0'}
79 };
80 
81 __dead void usage(void);
82 void push_excludes(char *);
83 void push_ignore_pats(char *);
84 void read_excludes_file(char *file);
85 void set_argstr(char **, char **);
86 
87 int
88 main(int argc, char **argv)
89 {
90 	char *ep, **oargv;
91 	long  l;
92 	int   ch, lastch, gotstdin, prevoptind, newarg;
93 
94 	oargv = argv;
95 	gotstdin = 0;
96 
97 	lastch = '\0';
98 	prevoptind = 1;
99 	newarg = 1;
100 	while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
101 		switch (ch) {
102 		case '0': case '1': case '2': case '3': case '4':
103 		case '5': case '6': case '7': case '8': case '9':
104 			if (newarg)
105 				usage();	/* disallow -[0-9]+ */
106 			else if (lastch == 'c' || lastch == 'u')
107 				context = 0;
108 			else if (!isdigit(lastch) || context > INT_MAX / 10)
109 				usage();
110 			context = (context * 10) + (ch - '0');
111 			break;
112 		case 'a':
113 			aflag = 1;
114 			break;
115 		case 'b':
116 			bflag = 1;
117 			break;
118 		case 'C':
119 		case 'c':
120 			format = D_CONTEXT;
121 			if (optarg != NULL) {
122 				l = strtol(optarg, &ep, 10);
123 				if (*ep != '\0' || l < 0 || l >= INT_MAX)
124 					usage();
125 				context = (int)l;
126 			} else
127 				context = 3;
128 			break;
129 		case 'd':
130 			dflag = 1;
131 			break;
132 		case 'D':
133 			format = D_IFDEF;
134 			ifdefname = optarg;
135 			break;
136 		case 'e':
137 			format = D_EDIT;
138 			break;
139 		case 'f':
140 			format = D_REVERSE;
141 			break;
142 		case 'h':
143 			/* silently ignore for backwards compatibility */
144 			break;
145 		case 'I':
146 			push_ignore_pats(optarg);
147 			break;
148 		case 'i':
149 			iflag = 1;
150 			break;
151 		case 'L':
152 			label = optarg;
153 			break;
154 		case 'l':
155 			lflag = 1;
156 			signal(SIGPIPE, SIG_IGN);
157 			break;
158 		case 'N':
159 			Nflag = 1;
160 			break;
161 		case 'n':
162 			format = D_NREVERSE;
163 			break;
164 		case 'p':
165 			pflag = 1;
166 			break;
167 		case 'P':
168 			Pflag = 1;
169 			break;
170 		case 'r':
171 			rflag = 1;
172 			break;
173 		case 'q':
174 			format = D_BRIEF;
175 			break;
176 		case 'S':
177 			start = optarg;
178 			break;
179 		case 's':
180 			sflag = 1;
181 			break;
182 		case 'T':
183 			Tflag = 1;
184 			break;
185 		case 't':
186 			tflag = 1;
187 			break;
188 		case 'U':
189 		case 'u':
190 			format = D_UNIFIED;
191 			if (optarg != NULL) {
192 				l = strtol(optarg, &ep, 10);
193 				if (*ep != '\0' || l < 0 || l >= INT_MAX)
194 					usage();
195 				context = (int)l;
196 			} else
197 				context = 3;
198 			break;
199 		case 'w':
200 			wflag = 1;
201 			break;
202 		case 'X':
203 			read_excludes_file(optarg);
204 			break;
205 		case 'x':
206 			push_excludes(optarg);
207 			break;
208 		default:
209 			usage();
210 			break;
211 		}
212 		lastch = ch;
213 		newarg = optind != prevoptind;
214 		prevoptind = optind;
215 	}
216 	argc -= optind;
217 	argv += optind;
218 
219 	/*
220 	 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
221 	 * driver routine.  Both drivers use the contents of stb1 and stb2.
222 	 */
223 	if (argc != 2)
224 		usage();
225 	if (ignore_pats != NULL) {
226 		char buf[BUFSIZ];
227 		int error;
228 
229 		if ((error = regcomp(&ignore_re, ignore_pats,
230 				     REG_NEWLINE | REG_EXTENDED)) != 0) {
231 			regerror(error, &ignore_re, buf, sizeof(buf));
232 			if (*ignore_pats != '\0')
233 				errx(2, "%s: %s", ignore_pats, buf);
234 			else
235 				errx(2, "%s", buf);
236 		}
237 	}
238 	if (strcmp(argv[0], "-") == 0) {
239 		fstat(STDIN_FILENO, &stb1);
240 		gotstdin = 1;
241 	} else if (stat(argv[0], &stb1) != 0)
242 		err(2, "%s", argv[0]);
243 	if (strcmp(argv[1], "-") == 0) {
244 		fstat(STDIN_FILENO, &stb2);
245 		gotstdin = 1;
246 	} else if (stat(argv[1], &stb2) != 0)
247 		err(2, "%s", argv[1]);
248 	if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
249 		errx(2, "can't compare - to a directory");
250 	set_argstr(oargv + 1, argv);
251 	if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
252 		if (format == D_IFDEF)
253 			errx(2, "-D option not supported with directories");
254 		diffdir(argv[0], argv[1]);
255 	} else {
256 		if (S_ISDIR(stb1.st_mode)) {
257 			argv[0] = splice(argv[0], argv[1]);
258 			if (stat(argv[0], &stb1) < 0)
259 				err(2, "%s", argv[0]);
260 		}
261 		if (S_ISDIR(stb2.st_mode)) {
262 			argv[1] = splice(argv[1], argv[0]);
263 			if (stat(argv[1], &stb2) < 0)
264 				err(2, "%s", argv[1]);
265 		}
266 		print_status(diffreg(argv[0], argv[1], 0), argv[0], argv[1],
267 		    NULL);
268 	}
269 	exit(status);
270 }
271 
272 void *
273 emalloc(size_t n)
274 {
275 	void *p;
276 
277 	if ((p = malloc(n)) == NULL)
278 		err(2, NULL);
279 	return (p);
280 }
281 
282 void *
283 erealloc(void *p, size_t n)
284 {
285 	void *q;
286 
287 	if ((q = realloc(p, n)) == NULL)
288 		err(2, NULL);
289 	return (q);
290 }
291 
292 int
293 easprintf(char **ret, const char *fmt, ...)
294 {
295 	int len;
296 	va_list ap;
297 
298 	va_start(ap, fmt);
299 	len = vasprintf(ret, fmt, ap);
300 	va_end(ap);
301 
302 	if (len == -1)
303 		err(2, NULL);
304 	return (len);
305 }
306 
307 void
308 set_argstr(char **av, char **ave)
309 {
310 	size_t argsize;
311 	char **ap;
312 
313 	argsize = 4 + *ave - *av + 1;
314 	diffargs = emalloc(argsize);
315 	strlcpy(diffargs, "diff", argsize);
316 	for (ap = av + 1; ap < ave; ap++) {
317 		if (strcmp(*ap, "--") != 0) {
318 			strlcat(diffargs, " ", argsize);
319 			strlcat(diffargs, *ap, argsize);
320 		}
321 	}
322 }
323 
324 /*
325  * Read in an excludes file and push each line.
326  */
327 void
328 read_excludes_file(char *file)
329 {
330 	FILE *fp;
331 	char *buf, *pattern;
332 	size_t len;
333 
334 	if (strcmp(file, "-") == 0)
335 		fp = stdin;
336 	else if ((fp = fopen(file, "r")) == NULL)
337 		err(2, "%s", file);
338 	while ((buf = fgetln(fp, &len)) != NULL) {
339 		if (buf[len - 1] == '\n')
340 			len--;
341 		pattern = emalloc(len + 1);
342 		memcpy(pattern, buf, len);
343 		pattern[len] = '\0';
344 		push_excludes(pattern);
345 	}
346 	if (strcmp(file, "-") != 0)
347 		fclose(fp);
348 }
349 
350 /*
351  * Push a pattern onto the excludes list.
352  */
353 void
354 push_excludes(char *pattern)
355 {
356 	struct excludes *entry;
357 
358 	entry = emalloc(sizeof(*entry));
359 	entry->pattern = pattern;
360 	entry->next = excludes_list;
361 	excludes_list = entry;
362 }
363 
364 void
365 push_ignore_pats(char *pattern)
366 {
367 	size_t len;
368 
369 	if (ignore_pats == NULL) {
370 		/* XXX: estrdup */
371 		len = strlen(pattern) + 1;
372 		ignore_pats = emalloc(len);
373 		strlcpy(ignore_pats, pattern, len);
374 	} else {
375 		/* old + "|" + new + NUL */
376 		len = strlen(ignore_pats) + strlen(pattern) + 2;
377 		ignore_pats = erealloc(ignore_pats, len);
378 		strlcat(ignore_pats, "|", len);
379 		strlcat(ignore_pats, pattern, len);
380 	}
381 }
382 
383 void
384 print_only(const char *path, size_t dirlen, const char *entry)
385 {
386 	if (dirlen > 1)
387 		dirlen--;
388 	printf("Only in %.*s: %s\n", (int)dirlen, path, entry);
389 }
390 
391 void
392 print_status(int val, char *path1, char *path2, char *entry)
393 {
394 	switch (val) {
395 	case D_ONLY:
396 		print_only(path1, strlen(path1), entry);
397 		break;
398 	case D_COMMON:
399 		printf("Common subdirectories: %s%s and %s%s\n",
400 		    path1, entry ? entry : "", path2, entry ? entry : "");
401 		break;
402 	case D_BINARY:
403 		printf("Binary files %s%s and %s%s differ\n",
404 		    path1, entry ? entry : "", path2, entry ? entry : "");
405 		break;
406 	case D_DIFFER:
407 		if (format == D_BRIEF)
408 			printf("Files %s%s and %s%s differ\n",
409 			    path1, entry ? entry : "",
410 			    path2, entry ? entry : "");
411 		break;
412 	case D_SAME:
413 		if (sflag)
414 			printf("Files %s%s and %s%s are identical\n",
415 			    path1, entry ? entry : "",
416 			    path2, entry ? entry : "");
417 		break;
418 	case D_MISMATCH1:
419 		printf("File %s%s is a directory while file %s%s is a regular file\n",
420 		    path1, entry ? entry : "", path2, entry ? entry : "");
421 		break;
422 	case D_MISMATCH2:
423 		printf("File %s%s is a regular file while file %s%s is a directory\n",
424 		    path1, entry ? entry : "", path2, entry ? entry : "");
425 		break;
426 	case D_SKIPPED1:
427 		printf("File %s%s is not a regular file or directory and was skipped\n",
428 		    path1, entry ? entry : "");
429 		break;
430 	case D_SKIPPED2:
431 		printf("File %s%s is not a regular file or directory and was skipped\n",
432 		    path2, entry ? entry : "");
433 		break;
434 	}
435 }
436 
437 __dead void
438 usage(void)
439 {
440 	(void)fprintf(stderr,
441 	    "usage: diff [-abdilpqtTw] [-I pattern] [-c | -e | -f | -n | -u]\n"
442 	    "            [-L label] file1 file2\n"
443 	    "       diff [-abdilpqtTw] [-I pattern] [-L label] -C number file1 file2\n"
444 	    "       diff [-abdilqtw] [-I pattern] -D string file1 file2\n"
445 	    "       diff [-abdilpqtTw] [-I pattern] [-L label] -U number file1 file2\n"
446 	    "       diff [-abdilNPpqtTw] [-I pattern] [-c | -e | -f | -n | -u]\n"
447 	    "            [-L label] [-r] [-s] [-S name] [-X file] [-x pattern] dir1\n"
448 	    "            dir2\n");
449 
450 	exit(2);
451 }
452