[BACK]Return to diffdir.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / diff

Annotation of src/usr.bin/diff/diffdir.c, Revision 1.10

1.10    ! deraadt     1: /*     $OpenBSD: diffdir.c,v 1.9 2003/06/25 03:44:49 deraadt 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.8       tedu       40: #include <stdio.h>
1.3       tedu       41: #include <stdlib.h>
                     42: #include <fcntl.h>
                     43: #include <unistd.h>
                     44: #include <string.h>
1.1       deraadt    45:
                     46: #include "diff.h"
1.3       tedu       47:
                     48: #if 0
                     49: static const char sccsid[] = "@(#)diffdir.c    4.12 (Berkeley) 4/30/89";
                     50: #endif
                     51:
1.1       deraadt    52: /*
                     53:  * diff - directory comparison
                     54:  */
                     55: #define        d_flags d_ino
                     56:
                     57: #define        ONLY    1               /* Only in this directory */
                     58: #define        SAME    2               /* Both places and same */
                     59: #define        DIFFER  4               /* Both places and different */
                     60: #define        DIRECT  8               /* Directory */
                     61:
                     62: struct dir {
1.3       tedu       63:        u_long d_ino;
                     64:        short d_reclen;
                     65:        short d_namlen;
                     66:        char *d_entry;
1.1       deraadt    67: };
                     68:
1.3       tedu       69: int header;
                     70: static int dirstatus;          /* exit status from diffdir */
                     71: extern int status;
                     72: char title[2 * BUFSIZ];
                     73: char *etitle;
                     74: char *prargs[] = {"pr", "-h", 0, "-f", 0, 0};
                     75:
                     76:
                     77: static struct dir *setupdir(char *);
                     78: static int ascii(int);
                     79: static void compare(struct dir *);
                     80: static void calldiff(char *);
                     81: static void setfile(char **fpp, char **epp, char *file);
                     82: static int useless(char *);
1.4       tedu       83: static void only(struct dir *dp, int which);
1.3       tedu       84: static void scanpr(struct dir *, int, char *, char *, char *, char *, char *);
1.8       tedu       85: static int entcmp(const void *, const void *);
                     86:
1.3       tedu       87: void
                     88: diffdir(char **argv)
1.1       deraadt    89: {
1.9       deraadt    90:        struct dir *dir1, *dir2;
1.3       tedu       91:        struct dir *d1, *d2;
1.9       deraadt    92:        int i, cmp;
1.1       deraadt    93:
                     94:        if (opt == D_IFDEF) {
                     95:                fprintf(stderr, "diff: can't specify -I with directories\n");
1.7       deraadt    96:                done(0);
1.1       deraadt    97:        }
                     98:        if (opt == D_EDIT && (sflag || lflag))
                     99:                fprintf(stderr,
                    100:                    "diff: warning: shouldn't give -s or -l with -e\n");
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;
                    169:                        strcpy(efile1, d1->d_entry);
                    170:                        strcpy(efile2, d1->d_entry);
                    171:                        calldiff(0);
                    172:                }
                    173:        }
                    174:        status = dirstatus;
                    175: }
                    176:
1.3       tedu      177: void
                    178: setfile(char **fpp, char **epp, char *file)
1.1       deraadt   179: {
1.3       tedu      180:        char *cp;
1.1       deraadt   181:
1.3       tedu      182:        *fpp = talloc(BUFSIZ);
1.1       deraadt   183:        strcpy(*fpp, file);
                    184:        for (cp = *fpp; *cp; cp++)
                    185:                continue;
                    186:        *cp++ = '/';
                    187:        *epp = cp;
                    188: }
                    189:
1.3       tedu      190: static void
1.4       tedu      191: scanpr(struct dir *dp, int test, char *title, char *file1, char *efile1,
1.3       tedu      192:     char *file2, char *efile2)
1.1       deraadt   193: {
                    194:        int titled = 0;
                    195:
                    196:        for (; dp->d_entry; dp++) {
                    197:                if ((dp->d_flags & test) == 0)
                    198:                        continue;
                    199:                if (titled == 0) {
                    200:                        if (header == 0)
                    201:                                header = 1;
                    202:                        else
                    203:                                printf("\n");
                    204:                        printf(title,
                    205:                            efile1 - file1 - 1, file1,
                    206:                            efile2 - file2 - 1, file2);
                    207:                        printf(":\n");
                    208:                        titled = 1;
                    209:                }
                    210:                printf("\t%s\n", dp->d_entry);
                    211:        }
                    212: }
                    213:
1.3       tedu      214: void
1.4       tedu      215: only(struct dir *dp, int which)
1.1       deraadt   216: {
                    217:        char *file = which == 1 ? file1 : file2;
                    218:        char *efile = which == 1 ? efile1 : efile2;
                    219:
1.3       tedu      220:        printf("Only in %.*s: %s\n", (int)(efile - file - 1), file, dp->d_entry);
1.1       deraadt   221: }
                    222:
                    223: struct dir *
1.3       tedu      224: setupdir(char *cp)
1.1       deraadt   225: {
1.3       tedu      226:        struct dir *dp, *ep;
                    227:        struct direct *rp;
                    228:        int nitems;
1.1       deraadt   229:        DIR *dirp;
                    230:
                    231:        dirp = opendir(cp);
                    232:        if (dirp == NULL) {
                    233:                fprintf(stderr, "diff: ");
                    234:                perror(cp);
1.7       deraadt   235:                done(0);
1.1       deraadt   236:        }
                    237:        nitems = 0;
1.3       tedu      238:        dp = talloc(sizeof(struct dir));
                    239:        while ((rp = readdir(dirp))) {
1.1       deraadt   240:                ep = &dp[nitems++];
                    241:                ep->d_reclen = rp->d_reclen;
                    242:                ep->d_namlen = rp->d_namlen;
                    243:                ep->d_entry = 0;
                    244:                ep->d_flags = 0;
                    245:                if (ep->d_namlen > 0) {
1.3       tedu      246:                        ep->d_entry = talloc(ep->d_namlen + 1);
1.1       deraadt   247:                        strcpy(ep->d_entry, rp->d_name);
                    248:                }
1.4       tedu      249:                dp = ralloc(dp, (nitems + 1) * sizeof(struct dir));
1.1       deraadt   250:        }
1.3       tedu      251:        dp[nitems].d_entry = 0; /* delimiter */
1.1       deraadt   252:        closedir(dirp);
1.3       tedu      253:        qsort(dp, nitems, sizeof(struct dir), entcmp);
1.1       deraadt   254:        return (dp);
                    255: }
                    256:
1.8       tedu      257: static int
                    258: entcmp(const void *v1, const void *v2)
1.1       deraadt   259: {
1.8       tedu      260:        const struct dir *d1, *d2;
                    261:
                    262:        d1 = v1;
                    263:        d2 = v2;
1.1       deraadt   264:        return (strcmp(d1->d_entry, d2->d_entry));
                    265: }
                    266:
1.3       tedu      267: static void
1.4       tedu      268: compare(struct dir *dp)
1.1       deraadt   269: {
1.9       deraadt   270:        char buf1[BUFSIZ], buf2[BUFSIZ];
                    271:        int i, j, f1, f2, fmt1, fmt2;
1.1       deraadt   272:        struct stat stb1, stb2;
                    273:
                    274:        strcpy(efile1, dp->d_entry);
                    275:        strcpy(efile2, dp->d_entry);
                    276:        f1 = open(file1, 0);
                    277:        if (f1 < 0) {
                    278:                perror(file1);
                    279:                return;
                    280:        }
                    281:        f2 = open(file2, 0);
                    282:        if (f2 < 0) {
                    283:                perror(file2);
                    284:                close(f1);
                    285:                return;
                    286:        }
1.3       tedu      287:        fstat(f1, &stb1);
                    288:        fstat(f2, &stb2);
1.1       deraadt   289:        fmt1 = stb1.st_mode & S_IFMT;
                    290:        fmt2 = stb2.st_mode & S_IFMT;
                    291:        if (fmt1 != S_IFREG || fmt2 != S_IFREG) {
                    292:                if (fmt1 == fmt2) {
                    293:                        if (fmt1 != S_IFDIR && stb1.st_rdev == stb2.st_rdev)
                    294:                                goto same;
                    295:                        if (fmt1 == S_IFDIR) {
                    296:                                dp->d_flags = DIRECT;
                    297:                                if (lflag || opt == D_EDIT)
                    298:                                        goto closem;
                    299:                                printf("Common subdirectories: %s and %s\n",
                    300:                                    file1, file2);
                    301:                                goto closem;
                    302:                        }
                    303:                }
                    304:                goto notsame;
                    305:        }
                    306:        if (stb1.st_size != stb2.st_size)
                    307:                goto notsame;
                    308:        for (;;) {
                    309:                i = read(f1, buf1, BUFSIZ);
                    310:                j = read(f2, buf2, BUFSIZ);
                    311:                if (i < 0 || j < 0 || i != j)
                    312:                        goto notsame;
                    313:                if (i == 0 && j == 0)
                    314:                        goto same;
                    315:                for (j = 0; j < i; j++)
                    316:                        if (buf1[j] != buf2[j])
                    317:                                goto notsame;
                    318:        }
                    319: same:
                    320:        if (sflag == 0)
                    321:                goto closem;
                    322:        if (lflag)
                    323:                dp->d_flags = SAME;
                    324:        else
                    325:                printf("Files %s and %s are identical\n", file1, file2);
                    326:        goto closem;
                    327: notsame:
                    328:        dirstatus |= 1;
                    329:        if (!ascii(f1) || !ascii(f2)) {
                    330:                if (lflag)
                    331:                        dp->d_flags |= DIFFER;
                    332:                else if (opt == D_NORMAL || opt == D_CONTEXT)
                    333:                        printf("Binary files %s and %s differ\n",
                    334:                            file1, file2);
                    335:                goto closem;
                    336:        }
1.3       tedu      337:        close(f1);
                    338:        close(f2);
1.1       deraadt   339:        anychange = 1;
                    340:        if (lflag)
                    341:                calldiff(title);
                    342:        else {
                    343:                if (opt == D_EDIT) {
                    344:                        printf("ed - %s << '-*-END-*-'\n", dp->d_entry);
                    345:                        calldiff(0);
                    346:                } else {
                    347:                        printf("%s%s %s\n", title, file1, file2);
                    348:                        calldiff(0);
                    349:                }
                    350:                if (opt == D_EDIT)
                    351:                        printf("w\nq\n-*-END-*-\n");
                    352:        }
                    353:        return;
                    354: closem:
1.3       tedu      355:        close(f1);
                    356:        close(f2);
1.1       deraadt   357: }
                    358:
1.3       tedu      359: static void
                    360: calldiff(char *wantpr)
1.1       deraadt   361: {
1.9       deraadt   362:        int lstatus, lstatus2, pv[2];
                    363:        pid_t pid;
1.1       deraadt   364:
                    365:        prargs[2] = wantpr;
                    366:        fflush(stdout);
                    367:        if (wantpr) {
1.5       deraadt   368:                snprintf(etitle, title + sizeof title - etitle,
                    369:                    "%s %s", file1, file2);
1.1       deraadt   370:                pipe(pv);
                    371:                pid = fork();
                    372:                if (pid == -1) {
                    373:                        fprintf(stderr, "No more processes");
1.7       deraadt   374:                        done(0);
1.1       deraadt   375:                }
                    376:                if (pid == 0) {
                    377:                        close(0);
                    378:                        dup(pv[0]);
                    379:                        close(pv[0]);
                    380:                        close(pv[1]);
1.3       tedu      381:                        execv(pr + 4, prargs);
1.1       deraadt   382:                        execv(pr, prargs);
                    383:                        perror(pr);
1.7       deraadt   384:                        done(0);
1.1       deraadt   385:                }
                    386:        }
                    387:        pid = fork();
                    388:        if (pid == -1) {
                    389:                fprintf(stderr, "diff: No more processes\n");
1.7       deraadt   390:                done(0);
1.1       deraadt   391:        }
                    392:        if (pid == 0) {
                    393:                if (wantpr) {
                    394:                        close(1);
                    395:                        dup(pv[1]);
                    396:                        close(pv[0]);
                    397:                        close(pv[1]);
                    398:                }
1.3       tedu      399:                execv(diff + 4, diffargv);
1.1       deraadt   400:                execv(diff, diffargv);
                    401:                perror(diff);
1.7       deraadt   402:                done(0);
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.1       deraadt   424:
1.6       deraadt   425:        lseek(f, (off_t)0, SEEK_SET);
1.1       deraadt   426:        cnt = read(f, buf, BUFSIZ);
                    427:        cp = buf;
                    428:        while (--cnt >= 0)
                    429:                if (*cp++ & 0200)
                    430:                        return (0);
                    431:        return (1);
                    432: }
                    433:
                    434: /*
                    435:  * THIS IS CRUDE.
                    436:  */
1.3       tedu      437: int
                    438: useless(char *cp)
1.1       deraadt   439: {
                    440:        if (cp[0] == '.') {
                    441:                if (cp[1] == '\0')
                    442:                        return (1);     /* directory "." */
                    443:                if (cp[1] == '.' && cp[2] == '\0')
                    444:                        return (1);     /* directory ".." */
                    445:        }
                    446:        if (start && strcmp(start, cp) > 0)
                    447:                return (1);
                    448:        return (0);
                    449: }