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