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

1.19    ! millert     1: /*     $OpenBSD: diffdir.c,v 1.18 2003/07/06 02:11:12 millert Exp $    */
1.2       deraadt     2:
                      3: /*
1.19    ! millert     4:  * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
1.2       deraadt     5:  *
1.19    ! 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.19    ! 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.19    ! millert    23: #ifndef lint
        !            24: static const char rcsid[] = "$OpenBSD: diffdir.c,v 1.18 2003/07/06 02:11:12 millert Exp $";
        !            25: #endif /* not lint */
        !            26:
        !            27: #include <sys/param.h>
        !            28: #include <sys/stat.h>
1.3       tedu       29:
1.12      millert    30: #include <dirent.h>
1.19    ! millert    31: #include <err.h>
1.12      millert    32: #include <errno.h>
                     33: #include <fcntl.h>
1.19    ! millert    34: #include <fnmatch.h>
        !            35: #include <paths.h>
1.8       tedu       36: #include <stdio.h>
1.3       tedu       37: #include <stdlib.h>
1.12      millert    38: #include <string.h>
1.3       tedu       39: #include <unistd.h>
1.1       deraadt    40:
                     41: #include "diff.h"
1.3       tedu       42:
1.19    ! millert    43: static int dircompare(const void *, const void *);
        !            44: static int excluded(const char *);
        !            45: static struct dirent **slurpdir(char *, char **);
        !            46: static void diffit(struct dirent *, char *, size_t, char *, size_t);
1.3       tedu       47:
1.1       deraadt    48: /*
1.19    ! millert    49:  * Diff directory traveral. Will be called recursively if -r was specified.
1.1       deraadt    50:  */
1.19    ! millert    51: void
        !            52: diffdir(char *p1, char *p2)
        !            53: {
        !            54:        struct dirent **dirp1, **dirp2, **dp1, **dp2;
        !            55:        struct dirent *dent1, *dent2;
        !            56:        size_t dirlen1, dirlen2;
        !            57:        char path1[MAXPATHLEN], path2[MAXPATHLEN];
        !            58:        char *dirbuf1, *dirbuf2;
        !            59:        int pos;
        !            60:
        !            61:        dirlen1 = strlcpy(path1, *p1 ? p1 : ".", sizeof(path1));
        !            62:        if (dirlen1 >= sizeof(path1) - 1) {
        !            63:                warnx("%s: %s", p1, strerror(ENAMETOOLONG));
        !            64:                status = 2;
        !            65:                return;
        !            66:        }
        !            67:        if (path1[dirlen1 - 1] != '/') {
        !            68:                path1[dirlen1++] = '/';
        !            69:                path1[dirlen1] = '\0';
        !            70:        }
        !            71:        dirlen2 = strlcpy(path2, *p2 ? p2 : ".", sizeof(path2));
        !            72:        if (dirlen2 >= sizeof(path2) - 1) {
        !            73:                warnx("%s: %s", p2, strerror(ENAMETOOLONG));
        !            74:                status = 2;
        !            75:                return;
        !            76:        }
        !            77:        if (path2[dirlen2 - 1] != '/') {
        !            78:                path2[dirlen2++] = '/';
        !            79:                path2[dirlen2] = '\0';
        !            80:        }
1.1       deraadt    81:
1.19    ! millert    82:        /* get a list of the entries in each directory */
        !            83:        dp1 = dirp1 = slurpdir(path1, &dirbuf1);
        !            84:        dp2 = dirp2 = slurpdir(path2, &dirbuf2);
        !            85:        if (dirp1 == NULL || dirp2 == NULL)
        !            86:                return;
1.1       deraadt    87:
1.19    ! millert    88:        /*
        !            89:         * If we were given a starting point, find it.
        !            90:         */
        !            91:        if (start != NULL) {
        !            92:                while (*dp1 != NULL && strcmp((*dp1)->d_name, start) < 0)
        !            93:                        dp1++;
        !            94:                while (*dp2 != NULL && strcmp((*dp2)->d_name, start) < 0)
        !            95:                        dp2++;
        !            96:        }
1.8       tedu       97:
1.19    ! millert    98:        /*
        !            99:         * Iterate through the two directory lists, diffing as we go.
        !           100:         */
        !           101:        while (*dp1 != NULL || *dp2 != NULL) {
        !           102:                dent1 = *dp1;
        !           103:                dent2 = *dp2;
        !           104:
        !           105:                pos = dent1 == NULL ? 1 : dent2 == NULL ? -1 :
        !           106:                    strcmp(dent1->d_name, dent2->d_name);
        !           107:                if (pos == 0) {
        !           108:                        /* file exists in both dirs, diff it */
        !           109:                        diffit(dent1, path1, dirlen1, path2, dirlen2);
        !           110:                        dp1++;
        !           111:                        dp2++;
        !           112:                } else if (pos < 0) {
        !           113:                        /* file only in first dir, only diff if -N */
        !           114:                        if (Nflag)
        !           115:                                diffit(dent1, path1, dirlen1, path2, dirlen2);
        !           116:                        else if (format == D_NORMAL || format == D_CONTEXT ||
        !           117:                            format == D_UNIFIED)
        !           118:                                /* XXX GNU diff always prints this XXX */
        !           119:                                printf("Only in %.*s: %s\n", (int)(dirlen1 - 1),
        !           120:                                    path1, dent1->d_name);
        !           121:                        dp1++;
1.1       deraadt   122:                } else {
1.19    ! millert   123:                        /* file only in second dir, only diff if -N */
        !           124:                        if (Nflag)
        !           125:                                diffit(dent2, path1, dirlen1, path2, dirlen2);
        !           126:                        else if (format == D_NORMAL || format == D_CONTEXT ||
        !           127:                            format == D_UNIFIED)
        !           128:                                /* XXX GNU diff always prints this XXX */
        !           129:                                printf("Only in %.*s: %s\n", (int)(dirlen2 - 1),
        !           130:                                    path2, dent2->d_name);
        !           131:                        dp2++;
1.1       deraadt   132:                }
                    133:        }
1.19    ! millert   134:
        !           135:        if (dirbuf1 != NULL) {
        !           136:                free(dirp1);
        !           137:                free(dirbuf1);
        !           138:        }
        !           139:        if (dirbuf2 != NULL) {
        !           140:                free(dirp2);
        !           141:                free(dirbuf2);
1.1       deraadt   142:        }
                    143: }
                    144:
1.19    ! millert   145: /*
        !           146:  * Read in a whole directory's worth of struct dirents, culling
        !           147:  * out the "excluded" ones.
        !           148:  * Returns an array of struct dirent *'s that point into the buffer
        !           149:  * returned via bufp.  Caller is responsible for free()ing both of these.
        !           150:  */
        !           151: static struct dirent **
        !           152: slurpdir(char *path, char **bufp)
1.1       deraadt   153: {
1.19    ! millert   154:        char *buf, *ebuf, *cp;
        !           155:        size_t bufsize;
        !           156:        long base;
        !           157:        int fd, nbytes, entries;
        !           158:        struct stat sb;
        !           159:        struct dirent **dirlist, *dp;
        !           160:
        !           161:        *bufp = NULL;
        !           162:        if ((fd = open(path, O_RDONLY, 0644)) == -1) {
        !           163:                static struct dirent *dummy;
        !           164:
        !           165:                if (!Nflag) {
        !           166:                        warn("%s", path);
        !           167:                        return (NULL);
        !           168:                }
        !           169:                return (&dummy);
        !           170:        }
        !           171:        fstat(fd, &sb);
1.1       deraadt   172:
1.19    ! millert   173:        bufsize = sb.st_size;
        !           174:        if (bufsize < sb.st_blksize)
        !           175:                bufsize = sb.st_blksize;
        !           176:        buf = emalloc(bufsize);
        !           177:
        !           178:        nbytes = getdirentries(fd, buf, bufsize, &base);
        !           179:        if (nbytes <= 0) {
        !           180:                free(buf);
        !           181:                warn("%s", path);
        !           182:                return (NULL);
1.12      millert   183:        }
1.19    ! millert   184:        ebuf = buf + nbytes;
        !           185:        close(fd);
1.1       deraadt   186:
1.19    ! millert   187:        /*
        !           188:         * We now have all the directory entries in our buffer.
        !           189:         * However, in order to easily sort them we need to convert
        !           190:         * the buffer into an array.
        !           191:         */
        !           192:        for (entries = 0, cp = buf; cp < ebuf; ) {
        !           193:                dp = (struct dirent *)cp;
        !           194:                if (dp->d_fileno != 0 && dp->d_type != DT_WHT)
        !           195:                        entries++;
        !           196:                if (dp->d_reclen <= 0)
        !           197:                        break;
        !           198:                cp += dp->d_reclen;
        !           199:        }
        !           200:        dirlist = emalloc(sizeof(struct dirent *) * (entries + 1));
        !           201:        for (entries = 0, cp = buf; cp < ebuf; ) {
        !           202:                dp = (struct dirent *)cp;
        !           203:                if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
        !           204:                    !excluded(dp->d_name))
        !           205:                        dirlist[entries++] = dp;
        !           206:                if (dp->d_reclen <= 0)
        !           207:                        break;
        !           208:                cp += dp->d_reclen;
        !           209:        }
        !           210:        dirlist[entries] = NULL;
1.1       deraadt   211:
1.19    ! millert   212:        qsort(dirlist, entries, sizeof(struct dir *), dircompare);
1.1       deraadt   213:
1.19    ! millert   214:        *bufp = buf;
        !           215:        return (dirlist);
1.1       deraadt   216: }
                    217:
1.19    ! millert   218: /*
        !           219:  * Compare d_name in two dirent structures; for qsort(3).
        !           220:  */
1.8       tedu      221: static int
1.19    ! millert   222: dircompare(const void *vp1, const void *vp2)
1.1       deraadt   223: {
1.19    ! millert   224:        struct dirent *dp1 = *((struct dirent **) vp1);
        !           225:        struct dirent *dp2 = *((struct dirent **) vp2);
1.8       tedu      226:
1.19    ! millert   227:        return (strcmp(dp1->d_name, dp2->d_name));
1.1       deraadt   228: }
                    229:
1.19    ! millert   230: /*
        !           231:  * Do the actual diff by calling either diffreg() or diffdir().
        !           232:  */
1.3       tedu      233: static void
1.19    ! millert   234: diffit(struct dirent *dp, char *path1, size_t plen1, char *path2, size_t plen2)
1.1       deraadt   235: {
1.19    ! millert   236:        int flags = D_HEADER;
        !           237:
        !           238:        strlcpy(path1 + plen1, dp->d_name, MAXPATHLEN - plen1);
        !           239:        if (stat(path1, &stb1) != 0) {
        !           240:                if (!Nflag || errno != ENOENT) {
        !           241:                        warn("%s", path1);
        !           242:                        return;
1.1       deraadt   243:                }
1.19    ! millert   244:                flags |= D_EMPTY1;
        !           245:                memset(&stb1, 0, sizeof(stb1));
1.1       deraadt   246:        }
                    247:
1.19    ! millert   248:        strlcpy(path2 + plen2, dp->d_name, MAXPATHLEN - plen2);
        !           249:        if (stat(path2, &stb2) != 0) {
        !           250:                if (!Nflag || errno != ENOENT) {
        !           251:                        warn("%s", path2);
        !           252:                        return;
        !           253:                }
        !           254:                flags |= D_EMPTY2;
        !           255:                memset(&stb2, 0, sizeof(stb2));
        !           256:                stb2.st_mode = stb1.st_mode;
        !           257:        }
        !           258:        if (stb1.st_mode == 0)
        !           259:                stb1.st_mode = stb2.st_mode;
        !           260:
        !           261:        if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
        !           262:                /* XXX GNU diff always prints this for dirs XXX */
        !           263:                if (format != D_EDIT)
        !           264:                        printf("Common subdirectories: %s and %s\n",
        !           265:                            path1, path2);
        !           266:                if (rflag)
        !           267:                        diffdir(path1, path2);
        !           268:                return;
1.1       deraadt   269:        }
1.19    ! millert   270:        diffreg(path1, path2, flags);
1.1       deraadt   271: }
                    272:
1.19    ! millert   273: /*
        !           274:  * Exclude the given directory entry?
        !           275:  */
        !           276: static int
        !           277: excluded(const char *entry)
1.1       deraadt   278: {
1.19    ! millert   279:        struct excludes *excl;
1.15      tedu      280:
1.19    ! millert   281:        /* always skip "." and ".." */
        !           282:        if (entry[0] == '.' &&
        !           283:            (entry[1] == '\0' || (entry[1] == '.' && entry[2] == '\0')))
1.15      tedu      284:                return (1);
1.1       deraadt   285:
1.19    ! millert   286:        /* check excludes list */
        !           287:        for (excl = excludes_list; excl != NULL; excl = excl->next)
        !           288:                if (fnmatch(excl->pattern, entry, FNM_PATHNAME) == 0)
        !           289:                        return (1);
1.1       deraadt   290:
                    291:        return (0);
                    292: }