xref: /openbsd-src/usr.bin/diff/diff.c (revision a0daf5cc29ed13f870b1fca9a13cd2216d45abdc)
1 /*	$OpenBSD: diff.c,v 1.22 2003/07/04 17:52:35 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 <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	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	rflag;			/* recursively trace directories */
62 int	sflag;			/* announce files which are same */
63 char	*start;			/* do file only if name >= this */
64 /* Variable for -D D_IFDEF option. */
65 char	*ifdefname;		/* What we will print for #ifdef/#endif */
66 /* Variables for -c and -u context option. */
67 int	context;		/* lines of context to be printed */
68 /* State for exit status. */
69 int	status;
70 int	anychange;
71 /* Variables for diffdir. */
72 char	**diffargv;		/* option list to pass to recursive diffs */
73 
74 /*
75  * Input file names.
76  * With diffdir, file1 and file2 are allocated MAXPATHLEN space,
77  * and padded with a '/', and then efile1 and efile2 point after
78  * the '/'.
79  */
80 char	*file1, *file2, *efile1, *efile2;
81 struct	stat stb1, stb2;
82 
83 __dead void usage(void);
84 
85 int
86 main(int argc, char **argv)
87 {
88 	int ch;
89 
90 	status = 2;
91 	diffargv = argv;
92 
93 	while ((ch = getopt(argc, argv, "abC:cD:efhinrS:stU:uw")) != -1) {
94 		switch (ch) {
95 		case 'a':
96 			aflag++;
97 			break;
98 		case 'b':
99 			bflag++;
100 			break;
101 		case 'C':
102 			opt = D_CONTEXT;
103 			if (!isdigit(*optarg))
104 				usage();
105 			context = atoi(optarg);	/* XXX - use strtol */
106 			break;
107 		case 'c':
108 			opt = D_CONTEXT;
109 			context = 3;
110 			break;
111 		case 'D':
112 			opt = D_IFDEF;
113 			ifdefname = optarg;
114 			break;
115 		case 'e':
116 			opt = D_EDIT;
117 			break;
118 		case 'f':
119 			opt = D_REVERSE;
120 			break;
121 		case 'h':
122 			/* silently ignore for backwards compatibility */
123 			break;
124 		case 'i':
125 			iflag++;
126 			break;
127 		case 'n':
128 			opt = D_NREVERSE;
129 			break;
130 		case 'r':
131 			rflag++;
132 			break;
133 		case 'S':
134 			start = optarg;
135 			break;
136 		case 's':
137 			sflag++;
138 			break;
139 		case 't':
140 			tflag++;
141 			break;
142 		case 'U':
143 			opt = D_UNIFIED;
144 			if (!isdigit(*optarg))
145 				usage();
146 			context = atoi(optarg);	/* XXX - use strtol */
147 			break;
148 		case 'u':
149 			opt = D_UNIFIED;
150 			context = 3;
151 			break;
152 		case 'w':
153 			wflag++;
154 			break;
155 		default:
156 			usage();
157 			break;
158 		}
159 	}
160 	argc -= optind;
161 	argv += optind;
162 
163 	if (argc != 2)
164 		errorx("two filename arguments required");
165 	file1 = argv[0];
166 	file2 = argv[1];
167 	if (!strcmp(file1, "-"))
168 		stb1.st_mode = S_IFREG;
169 	else if (stat(file1, &stb1) < 0)
170 		error("%s", file1);
171 	if (!strcmp(file2, "-"))
172 		stb2.st_mode = S_IFREG;
173 	else if (stat(file2, &stb2) < 0)
174 		error("%s", file2);
175 	if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode))
176 		diffdir(argv);
177 	else
178 		diffreg();
179 	done(0);
180 }
181 
182 int
183 min(int a, int b)
184 {
185 
186 	return (a < b ? a : b);
187 }
188 
189 int
190 max(int a, int b)
191 {
192 
193 	return (a > b ? a : b);
194 }
195 
196 __dead void
197 done(int sig)
198 {
199 	if (tempfiles[0] != NULL)
200 		unlink(tempfiles[0]);
201 	if (tempfiles[1] != NULL)
202 		unlink(tempfiles[1]);
203 	if (sig)
204 		_exit(status);
205 	exit(status);
206 }
207 
208 void *
209 emalloc(size_t n)
210 {
211 	void *p;
212 
213 	if ((p = malloc(n)) == NULL)
214 		error(NULL);
215 	return (p);
216 }
217 
218 void *
219 erealloc(void *p, size_t n)
220 {
221 	void *q;
222 
223 	if ((q = realloc(p, n)) == NULL)
224 		error(NULL);
225 	return (q);
226 }
227 
228 __dead void
229 error(const char *fmt, ...)
230 {
231 	va_list ap;
232 	int sverrno = errno;
233 
234 	if (tempfiles[0] != NULL)
235 		unlink(tempfiles[0]);
236 	if (tempfiles[1] != NULL)
237 		unlink(tempfiles[1]);
238 	errno = sverrno;
239 	va_start(ap, fmt);
240 	verr(status, fmt, ap);
241 	va_end(ap);
242 }
243 
244 __dead void
245 errorx(const char *fmt, ...)
246 {
247 	va_list ap;
248 
249 	if (tempfiles[0] != NULL)
250 		unlink(tempfiles[0]);
251 	if (tempfiles[1] != NULL)
252 		unlink(tempfiles[1]);
253 	va_start(ap, fmt);
254 	verrx(status, fmt, ap);
255 	va_end(ap);
256 }
257 
258 __dead void
259 usage(void)
260 {
261 	(void)fprintf(stderr,
262 	    "usage: diff [-bitw] [-c | -e | -f | -n | -u ] file1 file2\n"
263 	    "       diff [-bitw] -C number file1 file2\n"
264 	    "       diff [-bitw] -D string file1 file2\n"
265 	    "       diff [-bitw] -U number file1 file2\n"
266 	    "       diff [-biwt] [-c | -e | -f | -n | -u ] "
267 	    "[-r] [-s] [-S name]\n            dir1 dir2\n");
268 
269 	exit(2);
270 }
271