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