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