Annotation of src/usr.bin/diff/diffdir.c, Revision 1.16
1.16 ! millert 1: /* $OpenBSD: diffdir.c,v 1.15 2003/06/27 20:28:13 tedu 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.16 ! millert 49: #include "pathnames.h"
1.3 tedu 50:
51: #if 0
52: static const char sccsid[] = "@(#)diffdir.c 4.12 (Berkeley) 4/30/89";
53: #endif
54:
1.1 deraadt 55: /*
56: * diff - directory comparison
57: */
58: #define d_flags d_ino
59:
60: #define ONLY 1 /* Only in this directory */
61: #define SAME 2 /* Both places and same */
62: #define DIFFER 4 /* Both places and different */
63: #define DIRECT 8 /* Directory */
64:
65: struct dir {
1.3 tedu 66: u_long d_ino;
67: short d_reclen;
68: short d_namlen;
69: char *d_entry;
1.1 deraadt 70: };
71:
1.3 tedu 72: int header;
73: static int dirstatus; /* exit status from diffdir */
74: extern int status;
75: char title[2 * BUFSIZ];
76: char *etitle;
77: char *prargs[] = {"pr", "-h", 0, "-f", 0, 0};
78:
79:
80: static struct dir *setupdir(char *);
81: static int ascii(int);
82: static void compare(struct dir *);
83: static void calldiff(char *);
84: static void setfile(char **fpp, char **epp, char *file);
85: static int useless(char *);
1.4 tedu 86: static void only(struct dir *dp, int which);
1.3 tedu 87: static void scanpr(struct dir *, int, char *, char *, char *, char *, char *);
1.8 tedu 88: static int entcmp(const void *, const void *);
89:
1.3 tedu 90: void
91: diffdir(char **argv)
1.1 deraadt 92: {
1.9 deraadt 93: struct dir *dir1, *dir2;
1.3 tedu 94: struct dir *d1, *d2;
1.9 deraadt 95: int i, cmp;
1.1 deraadt 96:
1.11 millert 97: if (opt == D_IFDEF)
98: warnx("can't specify -I with directories");
1.1 deraadt 99: if (opt == D_EDIT && (sflag || lflag))
1.11 millert 100: warnx("warning: shouldn't give -s or -l with -e");
1.3 tedu 101: strlcpy(title, "diff ", sizeof title);
102: for (i = 1; diffargv[i + 2]; i++) {
1.1 deraadt 103: if (!strcmp(diffargv[i], "-"))
104: continue; /* was -S, dont look silly */
1.3 tedu 105: strlcat(title, diffargv[i], sizeof title);
106: strlcat(title, " ", sizeof title);
1.1 deraadt 107: }
1.10 deraadt 108: for (etitle = title; *etitle; etitle++)
109: ;
1.1 deraadt 110: setfile(&file1, &efile1, file1);
111: setfile(&file2, &efile2, file2);
112: argv[0] = file1;
113: argv[1] = file2;
114: dir1 = setupdir(file1);
115: dir2 = setupdir(file2);
1.3 tedu 116: d1 = dir1;
117: d2 = dir2;
1.1 deraadt 118: while (d1->d_entry != 0 || d2->d_entry != 0) {
119: if (d1->d_entry && useless(d1->d_entry)) {
120: d1++;
121: continue;
122: }
123: if (d2->d_entry && useless(d2->d_entry)) {
124: d2++;
125: continue;
126: }
127: if (d1->d_entry == 0)
128: cmp = 1;
129: else if (d2->d_entry == 0)
130: cmp = -1;
131: else
132: cmp = strcmp(d1->d_entry, d2->d_entry);
133: if (cmp < 0) {
134: if (lflag)
135: d1->d_flags |= ONLY;
136: else if (opt == 0 || opt == 2)
137: only(d1, 1);
138: d1++;
139: dirstatus |= 1;
140: } else if (cmp == 0) {
141: compare(d1);
142: d1++;
143: d2++;
144: } else {
145: if (lflag)
146: d2->d_flags |= ONLY;
147: else if (opt == 0 || opt == 2)
148: only(d2, 2);
149: d2++;
150: dirstatus |= 1;
151: }
152: }
153: if (lflag) {
154: scanpr(dir1, ONLY, "Only in %.*s", file1, efile1, 0, 0);
155: scanpr(dir2, ONLY, "Only in %.*s", file2, efile2, 0, 0);
156: scanpr(dir1, SAME, "Common identical files in %.*s and %.*s",
157: file1, efile1, file2, efile2);
158: scanpr(dir1, DIFFER, "Binary files which differ in %.*s and %.*s",
159: file1, efile1, file2, efile2);
160: scanpr(dir1, DIRECT, "Common subdirectories of %.*s and %.*s",
161: file1, efile1, file2, efile2);
162: }
163: if (rflag) {
164: if (header && lflag)
165: printf("\f");
1.3 tedu 166: for (d1 = dir1; d1->d_entry; d1++) {
1.1 deraadt 167: if ((d1->d_flags & DIRECT) == 0)
168: continue;
1.12 millert 169: strlcpy(efile1, d1->d_entry,
170: file1 + MAXPATHLEN - efile1);
171: strlcpy(efile2, d1->d_entry,
172: file2 + MAXPATHLEN - efile2);
1.1 deraadt 173: calldiff(0);
174: }
175: }
176: status = dirstatus;
177: }
178:
1.3 tedu 179: void
180: setfile(char **fpp, char **epp, char *file)
1.1 deraadt 181: {
1.3 tedu 182: char *cp;
1.12 millert 183: size_t len;
1.1 deraadt 184:
1.12 millert 185: if (*file == '\0')
186: file = ".";
187: *fpp = emalloc(MAXPATHLEN);
188: len = strlcpy(*fpp, file, MAXPATHLEN);
189: if (len >= MAXPATHLEN - 1)
1.14 millert 190: errorx("%s: %s", file, strerror(ENAMETOOLONG));
1.12 millert 191: cp = *fpp + len - 1;
192: if (*cp == '/')
193: ++cp;
194: else {
195: *++cp = '/';
196: *++cp = '\0';
197: }
1.1 deraadt 198: *epp = cp;
199: }
200:
1.3 tedu 201: static void
1.4 tedu 202: scanpr(struct dir *dp, int test, char *title, char *file1, char *efile1,
1.3 tedu 203: char *file2, char *efile2)
1.1 deraadt 204: {
205: int titled = 0;
206:
207: for (; dp->d_entry; dp++) {
208: if ((dp->d_flags & test) == 0)
209: continue;
210: if (titled == 0) {
211: if (header == 0)
212: header = 1;
213: else
214: printf("\n");
215: printf(title,
216: efile1 - file1 - 1, file1,
217: efile2 - file2 - 1, file2);
218: printf(":\n");
219: titled = 1;
220: }
221: printf("\t%s\n", dp->d_entry);
222: }
223: }
224:
1.3 tedu 225: void
1.4 tedu 226: only(struct dir *dp, int which)
1.1 deraadt 227: {
228: char *file = which == 1 ? file1 : file2;
229: char *efile = which == 1 ? efile1 : efile2;
230:
1.3 tedu 231: printf("Only in %.*s: %s\n", (int)(efile - file - 1), file, dp->d_entry);
1.1 deraadt 232: }
233:
234: struct dir *
1.3 tedu 235: setupdir(char *cp)
1.1 deraadt 236: {
1.3 tedu 237: struct dir *dp, *ep;
1.12 millert 238: struct dirent *rp;
1.3 tedu 239: int nitems;
1.1 deraadt 240: DIR *dirp;
241:
242: dirp = opendir(cp);
1.14 millert 243: if (dirp == NULL)
244: error("%s", cp);
1.1 deraadt 245: nitems = 0;
1.12 millert 246: dp = emalloc(sizeof(struct dir));
1.3 tedu 247: while ((rp = readdir(dirp))) {
1.1 deraadt 248: ep = &dp[nitems++];
249: ep->d_reclen = rp->d_reclen;
250: ep->d_namlen = rp->d_namlen;
251: ep->d_entry = 0;
252: ep->d_flags = 0;
253: if (ep->d_namlen > 0) {
1.12 millert 254: ep->d_entry = emalloc(ep->d_namlen + 1);
255: strlcpy(ep->d_entry, rp->d_name, ep->d_namlen + 1);
1.1 deraadt 256: }
1.12 millert 257: dp = erealloc(dp, (nitems + 1) * sizeof(struct dir));
1.1 deraadt 258: }
1.3 tedu 259: dp[nitems].d_entry = 0; /* delimiter */
1.1 deraadt 260: closedir(dirp);
1.3 tedu 261: qsort(dp, nitems, sizeof(struct dir), entcmp);
1.1 deraadt 262: return (dp);
263: }
264:
1.8 tedu 265: static int
266: entcmp(const void *v1, const void *v2)
1.1 deraadt 267: {
1.8 tedu 268: const struct dir *d1, *d2;
269:
270: d1 = v1;
271: d2 = v2;
1.1 deraadt 272: return (strcmp(d1->d_entry, d2->d_entry));
273: }
274:
1.3 tedu 275: static void
1.4 tedu 276: compare(struct dir *dp)
1.1 deraadt 277: {
1.9 deraadt 278: char buf1[BUFSIZ], buf2[BUFSIZ];
279: int i, j, f1, f2, fmt1, fmt2;
1.1 deraadt 280: struct stat stb1, stb2;
281:
1.12 millert 282: strlcpy(efile1, dp->d_entry, file1 + MAXPATHLEN - efile1);
283: strlcpy(efile2, dp->d_entry, file2 + MAXPATHLEN - efile2);
1.1 deraadt 284: f1 = open(file1, 0);
285: if (f1 < 0) {
1.14 millert 286: warn("%s", file1);
1.1 deraadt 287: return;
288: }
289: f2 = open(file2, 0);
290: if (f2 < 0) {
1.14 millert 291: warn("%s", file2);
1.1 deraadt 292: close(f1);
293: return;
294: }
1.3 tedu 295: fstat(f1, &stb1);
296: fstat(f2, &stb2);
1.1 deraadt 297: fmt1 = stb1.st_mode & S_IFMT;
298: fmt2 = stb2.st_mode & S_IFMT;
299: if (fmt1 != S_IFREG || fmt2 != S_IFREG) {
300: if (fmt1 == fmt2) {
301: if (fmt1 != S_IFDIR && stb1.st_rdev == stb2.st_rdev)
302: goto same;
303: if (fmt1 == S_IFDIR) {
304: dp->d_flags = DIRECT;
305: if (lflag || opt == D_EDIT)
306: goto closem;
307: printf("Common subdirectories: %s and %s\n",
308: file1, file2);
309: goto closem;
310: }
311: }
312: goto notsame;
313: }
314: if (stb1.st_size != stb2.st_size)
315: goto notsame;
316: for (;;) {
317: i = read(f1, buf1, BUFSIZ);
318: j = read(f2, buf2, BUFSIZ);
319: if (i < 0 || j < 0 || i != j)
320: goto notsame;
321: if (i == 0 && j == 0)
322: goto same;
323: for (j = 0; j < i; j++)
324: if (buf1[j] != buf2[j])
325: goto notsame;
326: }
327: same:
328: if (sflag == 0)
329: goto closem;
330: if (lflag)
331: dp->d_flags = SAME;
332: else
333: printf("Files %s and %s are identical\n", file1, file2);
334: goto closem;
335: notsame:
336: dirstatus |= 1;
337: if (!ascii(f1) || !ascii(f2)) {
338: if (lflag)
339: dp->d_flags |= DIFFER;
1.13 millert 340: else if (opt == D_NORMAL || opt == D_CONTEXT || opt == D_UNIFIED)
1.1 deraadt 341: printf("Binary files %s and %s differ\n",
342: file1, file2);
343: goto closem;
344: }
1.3 tedu 345: close(f1);
346: close(f2);
1.1 deraadt 347: anychange = 1;
348: if (lflag)
349: calldiff(title);
350: else {
351: if (opt == D_EDIT) {
352: printf("ed - %s << '-*-END-*-'\n", dp->d_entry);
353: calldiff(0);
354: } else {
355: printf("%s%s %s\n", title, file1, file2);
356: calldiff(0);
357: }
358: if (opt == D_EDIT)
359: printf("w\nq\n-*-END-*-\n");
360: }
361: return;
362: closem:
1.3 tedu 363: close(f1);
364: close(f2);
1.1 deraadt 365: }
366:
1.3 tedu 367: static void
368: calldiff(char *wantpr)
1.1 deraadt 369: {
1.9 deraadt 370: int lstatus, lstatus2, pv[2];
371: pid_t pid;
1.1 deraadt 372:
373: prargs[2] = wantpr;
374: fflush(stdout);
375: if (wantpr) {
1.5 deraadt 376: snprintf(etitle, title + sizeof title - etitle,
377: "%s %s", file1, file2);
1.1 deraadt 378: pipe(pv);
379: pid = fork();
1.14 millert 380: if (pid == -1)
381: errorx("No more processes");
1.1 deraadt 382: if (pid == 0) {
383: close(0);
384: dup(pv[0]);
385: close(pv[0]);
386: close(pv[1]);
1.16 ! millert 387: execv(_PATH_PR, prargs);
! 388: errorx("%s", _PATH_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.16 ! millert 401: execv(_PATH_DIFF, diffargv);
! 402: error("%s", _PATH_DIFF);
1.1 deraadt 403: }
404: if (wantpr) {
405: close(pv[0]);
406: close(pv[1]);
407: }
408: while (wait(&lstatus) != pid)
409: continue;
410: while (wait(&lstatus2) != -1)
411: continue;
1.3 tedu 412: /*
413: if ((lstatus >> 8) >= 2)
1.7 deraadt 414: done(0);
1.3 tedu 415: */
1.1 deraadt 416: dirstatus |= lstatus >> 8;
417: }
418:
1.3 tedu 419: int
420: ascii(int f)
1.1 deraadt 421: {
1.9 deraadt 422: char buf[BUFSIZ], *cp;
1.3 tedu 423: int cnt;
1.15 tedu 424:
425: if (aflag)
426: return (1);
1.1 deraadt 427:
1.6 deraadt 428: lseek(f, (off_t)0, SEEK_SET);
1.1 deraadt 429: cnt = read(f, buf, BUFSIZ);
430: cp = buf;
431: while (--cnt >= 0)
432: if (*cp++ & 0200)
433: return (0);
434: return (1);
435: }
436:
437: /*
438: * THIS IS CRUDE.
439: */
1.3 tedu 440: int
441: useless(char *cp)
1.1 deraadt 442: {
443: if (cp[0] == '.') {
444: if (cp[1] == '\0')
445: return (1); /* directory "." */
446: if (cp[1] == '.' && cp[2] == '\0')
447: return (1); /* directory ".." */
448: }
449: if (start && strcmp(start, cp) > 0)
450: return (1);
451: return (0);
452: }