[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.3

1.3     ! tedu        1: /*     $OpenBSD: diffdir.c,v 1.2 2003/06/25 01:23:38 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:
        !            40: #include <stdlib.h>
        !            41: #include <fcntl.h>
        !            42: #include <unistd.h>
        !            43: #include <string.h>
1.1       deraadt    44:
                     45: #include "diff.h"
1.3     ! tedu       46:
        !            47: #if 0
        !            48: static const char sccsid[] = "@(#)diffdir.c    4.12 (Berkeley) 4/30/89";
        !            49: #endif
        !            50:
1.1       deraadt    51: /*
                     52:  * diff - directory comparison
                     53:  */
                     54: #define        d_flags d_ino
                     55:
                     56: #define        ONLY    1               /* Only in this directory */
                     57: #define        SAME    2               /* Both places and same */
                     58: #define        DIFFER  4               /* Both places and different */
                     59: #define        DIRECT  8               /* Directory */
                     60:
                     61: struct dir {
1.3     ! tedu       62:        u_long d_ino;
        !            63:        short d_reclen;
        !            64:        short d_namlen;
        !            65:        char *d_entry;
1.1       deraadt    66: };
                     67:
1.3     ! tedu       68: int header;
        !            69: static int dirstatus;          /* exit status from diffdir */
        !            70: extern int status;
        !            71: char title[2 * BUFSIZ];
        !            72: char *etitle;
        !            73: char *prargs[] = {"pr", "-h", 0, "-f", 0, 0};
        !            74:
        !            75:
        !            76: static struct dir *setupdir(char *);
        !            77: static int ascii(int);
        !            78: static void compare(struct dir *);
        !            79: static void calldiff(char *);
        !            80: static void setfile(char **fpp, char **epp, char *file);
        !            81: static int useless(char *);
        !            82: static void only(struct dir * dp, int which);
        !            83: static void scanpr(struct dir *, int, char *, char *, char *, char *, char *);
1.1       deraadt    84:
1.3     ! tedu       85: void
        !            86: diffdir(char **argv)
1.1       deraadt    87: {
1.3     ! tedu       88:        struct dir *d1, *d2;
1.1       deraadt    89:        struct dir *dir1, *dir2;
1.3     ! tedu       90:        int i;
1.1       deraadt    91:        int cmp;
                     92:
                     93:        if (opt == D_IFDEF) {
                     94:                fprintf(stderr, "diff: can't specify -I with directories\n");
                     95:                done();
                     96:        }
                     97:        if (opt == D_EDIT && (sflag || lflag))
                     98:                fprintf(stderr,
                     99:                    "diff: warning: shouldn't give -s or -l with -e\n");
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.3     ! tedu      107:        for (etitle = title; *etitle; etitle++);
1.1       deraadt   108:        setfile(&file1, &efile1, file1);
                    109:        setfile(&file2, &efile2, file2);
                    110:        argv[0] = file1;
                    111:        argv[1] = file2;
                    112:        dir1 = setupdir(file1);
                    113:        dir2 = setupdir(file2);
1.3     ! tedu      114:        d1 = dir1;
        !           115:        d2 = dir2;
1.1       deraadt   116:        while (d1->d_entry != 0 || d2->d_entry != 0) {
                    117:                if (d1->d_entry && useless(d1->d_entry)) {
                    118:                        d1++;
                    119:                        continue;
                    120:                }
                    121:                if (d2->d_entry && useless(d2->d_entry)) {
                    122:                        d2++;
                    123:                        continue;
                    124:                }
                    125:                if (d1->d_entry == 0)
                    126:                        cmp = 1;
                    127:                else if (d2->d_entry == 0)
                    128:                        cmp = -1;
                    129:                else
                    130:                        cmp = strcmp(d1->d_entry, d2->d_entry);
                    131:                if (cmp < 0) {
                    132:                        if (lflag)
                    133:                                d1->d_flags |= ONLY;
                    134:                        else if (opt == 0 || opt == 2)
                    135:                                only(d1, 1);
                    136:                        d1++;
                    137:                        dirstatus |= 1;
                    138:                } else if (cmp == 0) {
                    139:                        compare(d1);
                    140:                        d1++;
                    141:                        d2++;
                    142:                } else {
                    143:                        if (lflag)
                    144:                                d2->d_flags |= ONLY;
                    145:                        else if (opt == 0 || opt == 2)
                    146:                                only(d2, 2);
                    147:                        d2++;
                    148:                        dirstatus |= 1;
                    149:                }
                    150:        }
                    151:        if (lflag) {
                    152:                scanpr(dir1, ONLY, "Only in %.*s", file1, efile1, 0, 0);
                    153:                scanpr(dir2, ONLY, "Only in %.*s", file2, efile2, 0, 0);
                    154:                scanpr(dir1, SAME, "Common identical files in %.*s and %.*s",
                    155:                    file1, efile1, file2, efile2);
                    156:                scanpr(dir1, DIFFER, "Binary files which differ in %.*s and %.*s",
                    157:                    file1, efile1, file2, efile2);
                    158:                scanpr(dir1, DIRECT, "Common subdirectories of %.*s and %.*s",
                    159:                    file1, efile1, file2, efile2);
                    160:        }
                    161:        if (rflag) {
                    162:                if (header && lflag)
                    163:                        printf("\f");
1.3     ! tedu      164:                for (d1 = dir1; d1->d_entry; d1++) {
1.1       deraadt   165:                        if ((d1->d_flags & DIRECT) == 0)
                    166:                                continue;
                    167:                        strcpy(efile1, d1->d_entry);
                    168:                        strcpy(efile2, d1->d_entry);
                    169:                        calldiff(0);
                    170:                }
                    171:        }
                    172:        status = dirstatus;
                    173: }
                    174:
1.3     ! tedu      175: void
        !           176: setfile(char **fpp, char **epp, char *file)
1.1       deraadt   177: {
1.3     ! tedu      178:        char *cp;
1.1       deraadt   179:
1.3     ! tedu      180:        *fpp = talloc(BUFSIZ);
1.1       deraadt   181:        strcpy(*fpp, file);
                    182:        for (cp = *fpp; *cp; cp++)
                    183:                continue;
                    184:        *cp++ = '/';
                    185:        *epp = cp;
                    186: }
                    187:
1.3     ! tedu      188: static void
        !           189: scanpr(struct dir * dp, int test, char *title, char *file1, char *efile1,
        !           190:     char *file2, char *efile2)
1.1       deraadt   191: {
                    192:        int titled = 0;
                    193:
                    194:        for (; dp->d_entry; dp++) {
                    195:                if ((dp->d_flags & test) == 0)
                    196:                        continue;
                    197:                if (titled == 0) {
                    198:                        if (header == 0)
                    199:                                header = 1;
                    200:                        else
                    201:                                printf("\n");
                    202:                        printf(title,
                    203:                            efile1 - file1 - 1, file1,
                    204:                            efile2 - file2 - 1, file2);
                    205:                        printf(":\n");
                    206:                        titled = 1;
                    207:                }
                    208:                printf("\t%s\n", dp->d_entry);
                    209:        }
                    210: }
                    211:
1.3     ! tedu      212: void
        !           213: only(struct dir * dp, int which)
1.1       deraadt   214: {
                    215:        char *file = which == 1 ? file1 : file2;
                    216:        char *efile = which == 1 ? efile1 : efile2;
                    217:
1.3     ! tedu      218:        printf("Only in %.*s: %s\n", (int)(efile - file - 1), file, dp->d_entry);
1.1       deraadt   219: }
                    220:
1.3     ! tedu      221: int entcmp();
1.1       deraadt   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);
                    235:                done();
                    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.3     ! tedu      249:                dp = realloc((char *) dp,
        !           250:                    (nitems + 1) * sizeof(struct dir));
1.1       deraadt   251:                if (dp == 0) {
                    252:                        fprintf(stderr, "diff: ran out of memory\n");
                    253:                        done();
                    254:                }
                    255:        }
1.3     ! tedu      256:        dp[nitems].d_entry = 0; /* delimiter */
1.1       deraadt   257:        closedir(dirp);
1.3     ! tedu      258:        qsort(dp, nitems, sizeof(struct dir), entcmp);
1.1       deraadt   259:        return (dp);
                    260: }
                    261:
1.3     ! tedu      262: int
        !           263: entcmp(struct dir * d1, struct dir * d2)
1.1       deraadt   264: {
                    265:        return (strcmp(d1->d_entry, d2->d_entry));
                    266: }
                    267:
1.3     ! tedu      268: static void
        !           269: compare(struct dir * dp)
1.1       deraadt   270: {
1.3     ! tedu      271:        int i, j;
1.1       deraadt   272:        int f1, f2, fmt1, fmt2;
                    273:        struct stat stb1, stb2;
                    274:        char buf1[BUFSIZ], buf2[BUFSIZ];
                    275:
                    276:        strcpy(efile1, dp->d_entry);
                    277:        strcpy(efile2, dp->d_entry);
                    278:        f1 = open(file1, 0);
                    279:        if (f1 < 0) {
                    280:                perror(file1);
                    281:                return;
                    282:        }
                    283:        f2 = open(file2, 0);
                    284:        if (f2 < 0) {
                    285:                perror(file2);
                    286:                close(f1);
                    287:                return;
                    288:        }
1.3     ! tedu      289:        fstat(f1, &stb1);
        !           290:        fstat(f2, &stb2);
1.1       deraadt   291:        fmt1 = stb1.st_mode & S_IFMT;
                    292:        fmt2 = stb2.st_mode & S_IFMT;
                    293:        if (fmt1 != S_IFREG || fmt2 != S_IFREG) {
                    294:                if (fmt1 == fmt2) {
                    295:                        if (fmt1 != S_IFDIR && stb1.st_rdev == stb2.st_rdev)
                    296:                                goto same;
                    297:                        if (fmt1 == S_IFDIR) {
                    298:                                dp->d_flags = DIRECT;
                    299:                                if (lflag || opt == D_EDIT)
                    300:                                        goto closem;
                    301:                                printf("Common subdirectories: %s and %s\n",
                    302:                                    file1, file2);
                    303:                                goto closem;
                    304:                        }
                    305:                }
                    306:                goto notsame;
                    307:        }
                    308:        if (stb1.st_size != stb2.st_size)
                    309:                goto notsame;
                    310:        for (;;) {
                    311:                i = read(f1, buf1, BUFSIZ);
                    312:                j = read(f2, buf2, BUFSIZ);
                    313:                if (i < 0 || j < 0 || i != j)
                    314:                        goto notsame;
                    315:                if (i == 0 && j == 0)
                    316:                        goto same;
                    317:                for (j = 0; j < i; j++)
                    318:                        if (buf1[j] != buf2[j])
                    319:                                goto notsame;
                    320:        }
                    321: same:
                    322:        if (sflag == 0)
                    323:                goto closem;
                    324:        if (lflag)
                    325:                dp->d_flags = SAME;
                    326:        else
                    327:                printf("Files %s and %s are identical\n", file1, file2);
                    328:        goto closem;
                    329: notsame:
                    330:        dirstatus |= 1;
                    331:        if (!ascii(f1) || !ascii(f2)) {
                    332:                if (lflag)
                    333:                        dp->d_flags |= DIFFER;
                    334:                else if (opt == D_NORMAL || opt == D_CONTEXT)
                    335:                        printf("Binary files %s and %s differ\n",
                    336:                            file1, file2);
                    337:                goto closem;
                    338:        }
1.3     ! tedu      339:        close(f1);
        !           340:        close(f2);
1.1       deraadt   341:        anychange = 1;
                    342:        if (lflag)
                    343:                calldiff(title);
                    344:        else {
                    345:                if (opt == D_EDIT) {
                    346:                        printf("ed - %s << '-*-END-*-'\n", dp->d_entry);
                    347:                        calldiff(0);
                    348:                } else {
                    349:                        printf("%s%s %s\n", title, file1, file2);
                    350:                        calldiff(0);
                    351:                }
                    352:                if (opt == D_EDIT)
                    353:                        printf("w\nq\n-*-END-*-\n");
                    354:        }
                    355:        return;
                    356: closem:
1.3     ! tedu      357:        close(f1);
        !           358:        close(f2);
1.1       deraadt   359: }
                    360:
1.3     ! tedu      361: static void
        !           362: calldiff(char *wantpr)
1.1       deraadt   363: {
                    364:        int pid, lstatus, lstatus2, pv[2];
                    365:
                    366:        prargs[2] = wantpr;
                    367:        fflush(stdout);
                    368:        if (wantpr) {
1.3     ! tedu      369:                sprintf(etitle, "%s %s", file1, file2);
1.1       deraadt   370:                pipe(pv);
                    371:                pid = fork();
                    372:                if (pid == -1) {
                    373:                        fprintf(stderr, "No more processes");
                    374:                        done();
                    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);
                    384:                        done();
                    385:                }
                    386:        }
                    387:        pid = fork();
                    388:        if (pid == -1) {
                    389:                fprintf(stderr, "diff: No more processes\n");
                    390:                done();
                    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);
                    402:                done();
                    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)
        !           414:                        done();
        !           415:        */
1.1       deraadt   416:        dirstatus |= lstatus >> 8;
                    417: }
                    418:
1.3     ! tedu      419: int
        !           420: ascii(int f)
1.1       deraadt   421: {
                    422:        char buf[BUFSIZ];
1.3     ! tedu      423:        int cnt;
        !           424:        char *cp;
1.1       deraadt   425:
1.3     ! tedu      426:        lseek(f, 0, 0);
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: }