Annotation of src/usr.bin/diff/diffdir.c, Revision 1.14
1.14 ! millert 1: /* $OpenBSD: diffdir.c,v 1.13 2003/06/25 22:14:43 millert Exp $ */
1.2 deraadt 2:
3: /*
4: * Copyright (C) Caldera International Inc. 2001-2002.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code and documentation must retain the above
11: * copyright notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed or owned by Caldera
18: * International, Inc.
19: * 4. Neither the name of Caldera International, Inc. nor the names of other
20: * contributors may be used to endorse or promote products derived from
21: * this software without specific prior written permission.
22: *
23: * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24: * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27: * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28: * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33: * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34: * POSSIBILITY OF SUCH DAMAGE.
35: */
36:
1.3 tedu 37: #include <sys/types.h>
38: #include <sys/wait.h>
39:
1.12 millert 40: #include <dirent.h>
41: #include <errno.h>
42: #include <fcntl.h>
1.8 tedu 43: #include <stdio.h>
1.3 tedu 44: #include <stdlib.h>
1.12 millert 45: #include <string.h>
1.3 tedu 46: #include <unistd.h>
1.1 deraadt 47:
48: #include "diff.h"
1.3 tedu 49:
50: #if 0
51: static const char sccsid[] = "@(#)diffdir.c 4.12 (Berkeley) 4/30/89";
52: #endif
53:
1.1 deraadt 54: /*
55: * diff - directory comparison
56: */
57: #define d_flags d_ino
58:
59: #define ONLY 1 /* Only in this directory */
60: #define SAME 2 /* Both places and same */
61: #define DIFFER 4 /* Both places and different */
62: #define DIRECT 8 /* Directory */
63:
64: struct dir {
1.3 tedu 65: u_long d_ino;
66: short d_reclen;
67: short d_namlen;
68: char *d_entry;
1.1 deraadt 69: };
70:
1.3 tedu 71: int header;
72: static int dirstatus; /* exit status from diffdir */
73: extern int status;
74: char title[2 * BUFSIZ];
75: char *etitle;
76: char *prargs[] = {"pr", "-h", 0, "-f", 0, 0};
77:
78:
79: static struct dir *setupdir(char *);
80: static int ascii(int);
81: static void compare(struct dir *);
82: static void calldiff(char *);
83: static void setfile(char **fpp, char **epp, char *file);
84: static int useless(char *);
1.4 tedu 85: static void only(struct dir *dp, int which);
1.3 tedu 86: static void scanpr(struct dir *, int, char *, char *, char *, char *, char *);
1.8 tedu 87: static int entcmp(const void *, const void *);
88:
1.3 tedu 89: void
90: diffdir(char **argv)
1.1 deraadt 91: {
1.9 deraadt 92: struct dir *dir1, *dir2;
1.3 tedu 93: struct dir *d1, *d2;
1.9 deraadt 94: int i, cmp;
1.1 deraadt 95:
1.11 millert 96: if (opt == D_IFDEF)
97: warnx("can't specify -I with directories");
1.1 deraadt 98: if (opt == D_EDIT && (sflag || lflag))
1.11 millert 99: warnx("warning: shouldn't give -s or -l with -e");
1.3 tedu 100: strlcpy(title, "diff ", sizeof title);
101: for (i = 1; diffargv[i + 2]; i++) {
1.1 deraadt 102: if (!strcmp(diffargv[i], "-"))
103: continue; /* was -S, dont look silly */
1.3 tedu 104: strlcat(title, diffargv[i], sizeof title);
105: strlcat(title, " ", sizeof title);
1.1 deraadt 106: }
1.10 deraadt 107: for (etitle = title; *etitle; etitle++)
108: ;
1.1 deraadt 109: setfile(&file1, &efile1, file1);
110: setfile(&file2, &efile2, file2);
111: argv[0] = file1;
112: argv[1] = file2;
113: dir1 = setupdir(file1);
114: dir2 = setupdir(file2);
1.3 tedu 115: d1 = dir1;
116: d2 = dir2;
1.1 deraadt 117: while (d1->d_entry != 0 || d2->d_entry != 0) {
118: if (d1->d_entry && useless(d1->d_entry)) {
119: d1++;
120: continue;
121: }
122: if (d2->d_entry && useless(d2->d_entry)) {
123: d2++;
124: continue;
125: }
126: if (d1->d_entry == 0)
127: cmp = 1;
128: else if (d2->d_entry == 0)
129: cmp = -1;
130: else
131: cmp = strcmp(d1->d_entry, d2->d_entry);
132: if (cmp < 0) {
133: if (lflag)
134: d1->d_flags |= ONLY;
135: else if (opt == 0 || opt == 2)
136: only(d1, 1);
137: d1++;
138: dirstatus |= 1;
139: } else if (cmp == 0) {
140: compare(d1);
141: d1++;
142: d2++;
143: } else {
144: if (lflag)
145: d2->d_flags |= ONLY;
146: else if (opt == 0 || opt == 2)
147: only(d2, 2);
148: d2++;
149: dirstatus |= 1;
150: }
151: }
152: if (lflag) {
153: scanpr(dir1, ONLY, "Only in %.*s", file1, efile1, 0, 0);
154: scanpr(dir2, ONLY, "Only in %.*s", file2, efile2, 0, 0);
155: scanpr(dir1, SAME, "Common identical files in %.*s and %.*s",
156: file1, efile1, file2, efile2);
157: scanpr(dir1, DIFFER, "Binary files which differ in %.*s and %.*s",
158: file1, efile1, file2, efile2);
159: scanpr(dir1, DIRECT, "Common subdirectories of %.*s and %.*s",
160: file1, efile1, file2, efile2);
161: }
162: if (rflag) {
163: if (header && lflag)
164: printf("\f");
1.3 tedu 165: for (d1 = dir1; d1->d_entry; d1++) {
1.1 deraadt 166: if ((d1->d_flags & DIRECT) == 0)
167: continue;
1.12 millert 168: strlcpy(efile1, d1->d_entry,
169: file1 + MAXPATHLEN - efile1);
170: strlcpy(efile2, d1->d_entry,
171: file2 + MAXPATHLEN - efile2);
1.1 deraadt 172: calldiff(0);
173: }
174: }
175: status = dirstatus;
176: }
177:
1.3 tedu 178: void
179: setfile(char **fpp, char **epp, char *file)
1.1 deraadt 180: {
1.3 tedu 181: char *cp;
1.12 millert 182: size_t len;
1.1 deraadt 183:
1.12 millert 184: if (*file == '\0')
185: file = ".";
186: *fpp = emalloc(MAXPATHLEN);
187: len = strlcpy(*fpp, file, MAXPATHLEN);
188: if (len >= MAXPATHLEN - 1)
1.14 ! millert 189: errorx("%s: %s", file, strerror(ENAMETOOLONG));
1.12 millert 190: cp = *fpp + len - 1;
191: if (*cp == '/')
192: ++cp;
193: else {
194: *++cp = '/';
195: *++cp = '\0';
196: }
1.1 deraadt 197: *epp = cp;
198: }
199:
1.3 tedu 200: static void
1.4 tedu 201: scanpr(struct dir *dp, int test, char *title, char *file1, char *efile1,
1.3 tedu 202: char *file2, char *efile2)
1.1 deraadt 203: {
204: int titled = 0;
205:
206: for (; dp->d_entry; dp++) {
207: if ((dp->d_flags & test) == 0)
208: continue;
209: if (titled == 0) {
210: if (header == 0)
211: header = 1;
212: else
213: printf("\n");
214: printf(title,
215: efile1 - file1 - 1, file1,
216: efile2 - file2 - 1, file2);
217: printf(":\n");
218: titled = 1;
219: }
220: printf("\t%s\n", dp->d_entry);
221: }
222: }
223:
1.3 tedu 224: void
1.4 tedu 225: only(struct dir *dp, int which)
1.1 deraadt 226: {
227: char *file = which == 1 ? file1 : file2;
228: char *efile = which == 1 ? efile1 : efile2;
229:
1.3 tedu 230: printf("Only in %.*s: %s\n", (int)(efile - file - 1), file, dp->d_entry);
1.1 deraadt 231: }
232:
233: struct dir *
1.3 tedu 234: setupdir(char *cp)
1.1 deraadt 235: {
1.3 tedu 236: struct dir *dp, *ep;
1.12 millert 237: struct dirent *rp;
1.3 tedu 238: int nitems;
1.1 deraadt 239: DIR *dirp;
240:
241: dirp = opendir(cp);
1.14 ! millert 242: if (dirp == NULL)
! 243: error("%s", cp);
1.1 deraadt 244: nitems = 0;
1.12 millert 245: dp = emalloc(sizeof(struct dir));
1.3 tedu 246: while ((rp = readdir(dirp))) {
1.1 deraadt 247: ep = &dp[nitems++];
248: ep->d_reclen = rp->d_reclen;
249: ep->d_namlen = rp->d_namlen;
250: ep->d_entry = 0;
251: ep->d_flags = 0;
252: if (ep->d_namlen > 0) {
1.12 millert 253: ep->d_entry = emalloc(ep->d_namlen + 1);
254: strlcpy(ep->d_entry, rp->d_name, ep->d_namlen + 1);
1.1 deraadt 255: }
1.12 millert 256: dp = erealloc(dp, (nitems + 1) * sizeof(struct dir));
1.1 deraadt 257: }
1.3 tedu 258: dp[nitems].d_entry = 0; /* delimiter */
1.1 deraadt 259: closedir(dirp);
1.3 tedu 260: qsort(dp, nitems, sizeof(struct dir), entcmp);
1.1 deraadt 261: return (dp);
262: }
263:
1.8 tedu 264: static int
265: entcmp(const void *v1, const void *v2)
1.1 deraadt 266: {
1.8 tedu 267: const struct dir *d1, *d2;
268:
269: d1 = v1;
270: d2 = v2;
1.1 deraadt 271: return (strcmp(d1->d_entry, d2->d_entry));
272: }
273:
1.3 tedu 274: static void
1.4 tedu 275: compare(struct dir *dp)
1.1 deraadt 276: {
1.9 deraadt 277: char buf1[BUFSIZ], buf2[BUFSIZ];
278: int i, j, f1, f2, fmt1, fmt2;
1.1 deraadt 279: struct stat stb1, stb2;
280:
1.12 millert 281: strlcpy(efile1, dp->d_entry, file1 + MAXPATHLEN - efile1);
282: strlcpy(efile2, dp->d_entry, file2 + MAXPATHLEN - efile2);
1.1 deraadt 283: f1 = open(file1, 0);
284: if (f1 < 0) {
1.14 ! millert 285: warn("%s", file1);
1.1 deraadt 286: return;
287: }
288: f2 = open(file2, 0);
289: if (f2 < 0) {
1.14 ! millert 290: warn("%s", file2);
1.1 deraadt 291: close(f1);
292: return;
293: }
1.3 tedu 294: fstat(f1, &stb1);
295: fstat(f2, &stb2);
1.1 deraadt 296: fmt1 = stb1.st_mode & S_IFMT;
297: fmt2 = stb2.st_mode & S_IFMT;
298: if (fmt1 != S_IFREG || fmt2 != S_IFREG) {
299: if (fmt1 == fmt2) {
300: if (fmt1 != S_IFDIR && stb1.st_rdev == stb2.st_rdev)
301: goto same;
302: if (fmt1 == S_IFDIR) {
303: dp->d_flags = DIRECT;
304: if (lflag || opt == D_EDIT)
305: goto closem;
306: printf("Common subdirectories: %s and %s\n",
307: file1, file2);
308: goto closem;
309: }
310: }
311: goto notsame;
312: }
313: if (stb1.st_size != stb2.st_size)
314: goto notsame;
315: for (;;) {
316: i = read(f1, buf1, BUFSIZ);
317: j = read(f2, buf2, BUFSIZ);
318: if (i < 0 || j < 0 || i != j)
319: goto notsame;
320: if (i == 0 && j == 0)
321: goto same;
322: for (j = 0; j < i; j++)
323: if (buf1[j] != buf2[j])
324: goto notsame;
325: }
326: same:
327: if (sflag == 0)
328: goto closem;
329: if (lflag)
330: dp->d_flags = SAME;
331: else
332: printf("Files %s and %s are identical\n", file1, file2);
333: goto closem;
334: notsame:
335: dirstatus |= 1;
336: if (!ascii(f1) || !ascii(f2)) {
337: if (lflag)
338: dp->d_flags |= DIFFER;
1.13 millert 339: else if (opt == D_NORMAL || opt == D_CONTEXT || opt == D_UNIFIED)
1.1 deraadt 340: printf("Binary files %s and %s differ\n",
341: file1, file2);
342: goto closem;
343: }
1.3 tedu 344: close(f1);
345: close(f2);
1.1 deraadt 346: anychange = 1;
347: if (lflag)
348: calldiff(title);
349: else {
350: if (opt == D_EDIT) {
351: printf("ed - %s << '-*-END-*-'\n", dp->d_entry);
352: calldiff(0);
353: } else {
354: printf("%s%s %s\n", title, file1, file2);
355: calldiff(0);
356: }
357: if (opt == D_EDIT)
358: printf("w\nq\n-*-END-*-\n");
359: }
360: return;
361: closem:
1.3 tedu 362: close(f1);
363: close(f2);
1.1 deraadt 364: }
365:
1.3 tedu 366: static void
367: calldiff(char *wantpr)
1.1 deraadt 368: {
1.9 deraadt 369: int lstatus, lstatus2, pv[2];
370: pid_t pid;
1.1 deraadt 371:
372: prargs[2] = wantpr;
373: fflush(stdout);
374: if (wantpr) {
1.5 deraadt 375: snprintf(etitle, title + sizeof title - etitle,
376: "%s %s", file1, file2);
1.1 deraadt 377: pipe(pv);
378: pid = fork();
1.14 ! millert 379: if (pid == -1)
! 380: errorx("No more processes");
1.1 deraadt 381: if (pid == 0) {
382: close(0);
383: dup(pv[0]);
384: close(pv[0]);
385: close(pv[1]);
1.3 tedu 386: execv(pr + 4, prargs);
1.1 deraadt 387: execv(pr, prargs);
1.14 ! millert 388: errorx("%s", pr);
1.1 deraadt 389: }
390: }
391: pid = fork();
1.14 ! millert 392: if (pid == -1)
! 393: errorx("No more processes");
1.1 deraadt 394: if (pid == 0) {
395: if (wantpr) {
396: close(1);
397: dup(pv[1]);
398: close(pv[0]);
399: close(pv[1]);
400: }
1.3 tedu 401: execv(diff + 4, diffargv);
1.1 deraadt 402: execv(diff, diffargv);
1.14 ! millert 403: error("%s", diff);
1.1 deraadt 404: }
405: if (wantpr) {
406: close(pv[0]);
407: close(pv[1]);
408: }
409: while (wait(&lstatus) != pid)
410: continue;
411: while (wait(&lstatus2) != -1)
412: continue;
1.3 tedu 413: /*
414: if ((lstatus >> 8) >= 2)
1.7 deraadt 415: done(0);
1.3 tedu 416: */
1.1 deraadt 417: dirstatus |= lstatus >> 8;
418: }
419:
1.3 tedu 420: int
421: ascii(int f)
1.1 deraadt 422: {
1.9 deraadt 423: char buf[BUFSIZ], *cp;
1.3 tedu 424: int cnt;
1.1 deraadt 425:
1.6 deraadt 426: lseek(f, (off_t)0, SEEK_SET);
1.1 deraadt 427: cnt = read(f, buf, BUFSIZ);
428: cp = buf;
429: while (--cnt >= 0)
430: if (*cp++ & 0200)
431: return (0);
432: return (1);
433: }
434:
435: /*
436: * THIS IS CRUDE.
437: */
1.3 tedu 438: int
439: useless(char *cp)
1.1 deraadt 440: {
441: if (cp[0] == '.') {
442: if (cp[1] == '\0')
443: return (1); /* directory "." */
444: if (cp[1] == '.' && cp[2] == '\0')
445: return (1); /* directory ".." */
446: }
447: if (start && strcmp(start, cp) > 0)
448: return (1);
449: return (0);
450: }