Annotation of src/usr.bin/diff/diff.c, Revision 1.40
1.40 ! tedu 1: /* $OpenBSD: diff.c,v 1.39 2003/09/06 05:25:22 tedu 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.40 ! tedu 24: static const char rcsid[] = "$OpenBSD: diff.c,v 1.39 2003/09/06 05:25:22 tedu Exp $";
1.23 millert 25: #endif /* not lint */
26:
27: #include <sys/param.h>
28: #include <sys/stat.h>
29:
30: #include <err.h>
1.15 millert 31: #include <errno.h>
1.23 millert 32: #include <getopt.h>
1.28 millert 33: #include <signal.h>
1.3 tedu 34: #include <stdlib.h>
1.23 millert 35: #include <stdio.h>
1.15 millert 36: #include <stdarg.h>
1.18 david 37: #include <string.h>
1.3 tedu 38: #include <unistd.h>
1.1 deraadt 39:
40: #include "diff.h"
1.12 tedu 41:
1.37 deraadt 42: int aflag, bflag, dflag, iflag, lflag, Nflag, Pflag, rflag;
43: int sflag, tflag, Tflag, wflag;
1.23 millert 44: int format, context, status;
1.33 millert 45: char *start, *ifdefname, *diffargs, *label;
1.23 millert 46: struct stat stb1, stb2;
47: struct excludes *excludes_list;
48:
1.40 ! tedu 49: #define OPTIONS "0123456789abC:cdD:efhiL:lnNPqrS:sTtU:uwX:x:"
1.23 millert 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' },
1.35 otto 55: { "minimal", no_argument, 0, 'd' },
1.23 millert 56: { "ed", no_argument, 0, 'e' },
57: { "forward-ed", no_argument, 0, 'f' },
58: { "ignore-case", no_argument, 0, 'i' },
1.27 millert 59: { "paginate", no_argument, 0, 'l' },
1.33 millert 60: { "label", required_argument, 0, 'L' },
1.23 millert 61: { "new-file", no_argument, 0, 'N' },
62: { "rcs", no_argument, 0, 'n' },
1.24 millert 63: { "unidirectional-new-file", no_argument, 0, 'P' },
1.25 millert 64: { "brief", no_argument, 0, 'q' },
1.23 millert 65: { "recursive", no_argument, 0, 'r' },
66: { "report-identical-files", no_argument, 0, 's' },
67: { "starting-file", required_argument, 0, 'S' },
68: { "expand-tabs", no_argument, 0, 't' },
1.38 david 69: { "initial-tab", no_argument, 0, 'T' },
1.23 millert 70: { "unified", optional_argument, 0, 'U' },
71: { "ignore-all-space", no_argument, 0, 'w' },
72: { "exclude", required_argument, 0, 'x' },
73: { "exclude-from", required_argument, 0, 'X' },
1.34 millert 74: { NULL, 0, 0, '\0'}
1.23 millert 75: };
1.1 deraadt 76:
1.6 millert 77: __dead void usage(void);
1.23 millert 78: void push_excludes(char *);
79: void read_excludes_file(char *file);
80: void set_argstr(char **, char **);
1.3 tedu 81:
82: int
83: main(int argc, char **argv)
1.1 deraadt 84: {
1.23 millert 85: char *ep, **oargv;
86: long l;
1.40 ! tedu 87: int ch, lastch, gotstdin;
1.1 deraadt 88:
1.23 millert 89: oargv = argv;
90: gotstdin = 0;
1.6 millert 91:
1.40 ! tedu 92: lastch = 0;
1.23 millert 93: while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
1.6 millert 94: switch (ch) {
1.40 ! tedu 95: case '0': case '1': case '2': case '3': case '4':
! 96: case '5': case '6': case '7': case '8': case '9':
! 97: if (!(lastch == 'c' || lastch == 'u' ||
! 98: (lastch >= '0' && lastch <= '9')))
! 99: usage();
! 100: if (lastch == 'c' || lastch == 'u')
! 101: context = 0;
! 102: context = context * 10 + ch - '0';
! 103: break;
1.12 tedu 104: case 'a':
1.23 millert 105: aflag = 1;
1.12 tedu 106: break;
1.6 millert 107: case 'b':
1.23 millert 108: bflag = 1;
1.6 millert 109: break;
110: case 'C':
111: case 'c':
1.23 millert 112: format = D_CONTEXT;
113: if (optarg != NULL) {
114: l = strtol(optarg, &ep, 10);
115: if (*ep != '\0' || l < 0 || l >= INT_MAX)
116: usage();
117: context = (int)l;
118: } else
119: context = 3;
1.6 millert 120: break;
1.35 otto 121: case 'd':
122: dflag = 1;
123: break;
1.6 millert 124: case 'D':
1.23 millert 125: format = D_IFDEF;
1.17 millert 126: ifdefname = optarg;
1.6 millert 127: break;
128: case 'e':
1.23 millert 129: format = D_EDIT;
1.6 millert 130: break;
131: case 'f':
1.23 millert 132: format = D_REVERSE;
1.6 millert 133: break;
1.22 millert 134: case 'h':
135: /* silently ignore for backwards compatibility */
136: break;
1.6 millert 137: case 'i':
1.23 millert 138: iflag = 1;
139: break;
1.33 millert 140: case 'L':
141: label = optarg;
142: break;
1.27 millert 143: case 'l':
144: lflag = 1;
1.28 millert 145: signal(SIGPIPE, SIG_IGN);
1.27 millert 146: break;
1.23 millert 147: case 'N':
148: Nflag = 1;
1.6 millert 149: break;
150: case 'n':
1.23 millert 151: format = D_NREVERSE;
1.6 millert 152: break;
1.24 millert 153: case 'P':
154: Pflag = 1;
155: break;
1.6 millert 156: case 'r':
1.23 millert 157: rflag = 1;
1.6 millert 158: break;
1.25 millert 159: case 'q':
160: format = D_BRIEF;
161: break;
1.6 millert 162: case 'S':
163: start = optarg;
164: break;
165: case 's':
1.23 millert 166: sflag = 1;
1.6 millert 167: break;
1.33 millert 168: case 'T':
169: Tflag = 1;
170: break;
1.6 millert 171: case 't':
1.23 millert 172: tflag = 1;
1.6 millert 173: break;
1.9 millert 174: case 'U':
175: case 'u':
1.23 millert 176: format = D_UNIFIED;
177: if (optarg != NULL) {
178: l = strtol(optarg, &ep, 10);
179: if (*ep != '\0' || l < 0 || l >= INT_MAX)
180: usage();
181: context = (int)l;
182: } else
183: context = 3;
1.9 millert 184: break;
1.6 millert 185: case 'w':
1.23 millert 186: wflag = 1;
187: break;
188: case 'X':
189: read_excludes_file(optarg);
190: break;
191: case 'x':
192: push_excludes(optarg);
1.6 millert 193: break;
194: default:
195: usage();
196: break;
197: }
1.40 ! tedu 198: lastch = ch;
1.1 deraadt 199: }
1.6 millert 200: argc -= optind;
201: argv += optind;
202:
1.23 millert 203: /*
204: * Do sanity checks, fill in stb1 and stb2 and call the appropriate
205: * driver routine. Both drivers use the contents of stb1 and stb2.
206: */
1.6 millert 207: if (argc != 2)
1.23 millert 208: usage();
209: if (strcmp(argv[0], "-") == 0) {
1.26 millert 210: fstat(STDIN_FILENO, &stb1);
1.23 millert 211: gotstdin = 1;
212: } else if (stat(argv[0], &stb1) != 0)
1.28 millert 213: err(2, "%s", argv[0]);
1.23 millert 214: if (strcmp(argv[1], "-") == 0) {
1.26 millert 215: fstat(STDIN_FILENO, &stb2);
1.23 millert 216: gotstdin = 1;
217: } else if (stat(argv[1], &stb2) != 0)
1.28 millert 218: err(2, "%s", argv[1]);
1.23 millert 219: if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
1.28 millert 220: errx(2, "can't compare - to a directory");
1.36 millert 221: set_argstr(oargv + 1, argv);
1.23 millert 222: if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
223: if (format == D_IFDEF)
1.28 millert 224: errx(2, "-D option not supported with directories");
1.23 millert 225: diffdir(argv[0], argv[1]);
1.27 millert 226: } else {
1.28 millert 227: if (S_ISDIR(stb1.st_mode)) {
228: argv[0] = splice(argv[0], argv[1]);
229: if (stat(argv[0], &stb1) < 0)
230: err(2, "%s", argv[0]);
231: }
232: if (S_ISDIR(stb2.st_mode)) {
233: argv[1] = splice(argv[1], argv[0]);
234: if (stat(argv[1], &stb2) < 0)
235: err(2, "%s", argv[1]);
236: }
1.27 millert 237: print_status(diffreg(argv[0], argv[1], 0), argv[0], argv[1],
238: NULL);
239: }
1.23 millert 240: exit(status);
1.1 deraadt 241: }
242:
1.3 tedu 243: void *
1.8 millert 244: emalloc(size_t n)
1.3 tedu 245: {
246: void *p;
247:
248: if ((p = malloc(n)) == NULL)
1.28 millert 249: err(2, NULL);
1.3 tedu 250: return (p);
1.1 deraadt 251: }
252:
1.3 tedu 253: void *
1.8 millert 254: erealloc(void *p, size_t n)
1.1 deraadt 255: {
1.3 tedu 256: void *q;
1.1 deraadt 257:
1.3 tedu 258: if ((q = realloc(p, n)) == NULL)
1.28 millert 259: err(2, NULL);
1.3 tedu 260: return (q);
1.1 deraadt 261: }
262:
1.27 millert 263: int
264: easprintf(char **ret, const char *fmt, ...)
265: {
266: int len;
267: va_list ap;
268:
269: va_start(ap, fmt);
270: len = vasprintf(ret, fmt, ap);
271: va_end(ap);
272:
273: if (len == -1)
1.28 millert 274: err(2, NULL);
1.30 henning 275: return (len);
1.27 millert 276: }
277:
1.23 millert 278: void
279: set_argstr(char **av, char **ave)
280: {
281: size_t argsize;
282: char **ap;
283:
1.36 millert 284: argsize = 4 + *ave - *av + 1;
1.23 millert 285: diffargs = emalloc(argsize);
286: strlcpy(diffargs, "diff", argsize);
287: for (ap = av + 1; ap < ave; ap++) {
288: if (strcmp(*ap, "--") != 0) {
289: strlcat(diffargs, " ", argsize);
290: strlcat(diffargs, *ap, argsize);
291: }
292: }
293: }
294:
295: /*
296: * Read in an excludes file and push each line.
297: */
298: void
299: read_excludes_file(char *file)
300: {
301: FILE *fp;
302: char *buf, *pattern;
303: size_t len;
304:
305: if (strcmp(file, "-") == 0)
306: fp = stdin;
307: else if ((fp = fopen(file, "r")) == NULL)
1.28 millert 308: err(2, "%s", file);
1.23 millert 309: while ((buf = fgetln(fp, &len)) != NULL) {
310: if (buf[len - 1] == '\n')
311: len--;
312: pattern = emalloc(len + 1);
313: memcpy(pattern, buf, len);
314: pattern[len] = '\0';
315: push_excludes(pattern);
316: }
317: if (strcmp(file, "-") != 0)
318: fclose(fp);
319: }
320:
321: /*
322: * Push a pattern onto the excludes list.
323: */
324: void
325: push_excludes(char *pattern)
326: {
327: struct excludes *entry;
328:
329: entry = emalloc(sizeof(*entry));
330: entry->pattern = pattern;
331: entry->next = excludes_list;
332: excludes_list = entry;
1.27 millert 333: }
334:
335: void
336: print_status(int val, char *path1, char *path2, char *entry)
337: {
338: switch (val) {
339: case D_ONLY:
1.32 millert 340: /* must strip off the trailing '/' */
341: printf("Only in %.*s: %s\n", (int)(strlen(path1) - 1),
342: path1, entry);
1.27 millert 343: break;
344: case D_COMMON:
345: printf("Common subdirectories: %s%s and %s%s\n",
346: path1, entry ? entry : "", path2, entry ? entry : "");
347: break;
348: case D_BINARY:
349: printf("Binary files %s%s and %s%s differ\n",
350: path1, entry ? entry : "", path2, entry ? entry : "");
351: break;
352: case D_DIFFER:
353: if (format == D_BRIEF)
354: printf("Files %s%s and %s%s differ\n",
355: path1, entry ? entry : "",
356: path2, entry ? entry : "");
357: break;
358: case D_SAME:
359: if (sflag)
360: printf("Files %s%s and %s%s are identical\n",
361: path1, entry ? entry : "",
362: path2, entry ? entry : "");
1.28 millert 363: break;
1.29 millert 364: case D_MISMATCH1:
1.31 millert 365: printf("File %s%s is a directory while file %s%s is a regular file\n",
1.29 millert 366: path1, entry ? entry : "", path2, entry ? entry : "");
367: break;
368: case D_MISMATCH2:
1.31 millert 369: printf("File %s%s is a regular file while file %s%s is a directory\n",
1.28 millert 370: path1, entry ? entry : "", path2, entry ? entry : "");
1.27 millert 371: break;
372: }
1.23 millert 373: }
374:
1.6 millert 375: __dead void
376: usage(void)
377: {
1.14 deraadt 378: (void)fprintf(stderr,
1.35 otto 379: "usage: diff [-bdilqtTw] [-c | -e | -f | -n | -u] [-L label] file1 file2\n"
380: " diff [-bdilqtTw] [-L label] -C number file1 file2\n"
381: " diff [-bdilqtw] -D string file1 file2\n"
382: " diff [-bdilqtTw] [-L label] -U number file1 file2\n"
383: " diff [-bdilNPqwtT] [-c | -e | -f | -n | -u ] [-L label] [-r] [-s] [-S name]\n"
1.33 millert 384: " [-X file] [-x pattern] dir1 dir2\n");
1.6 millert 385:
1.15 millert 386: exit(2);
1.1 deraadt 387: }