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