#include #include #include #include "diff.h" static int itemcmp(void const *d1, void const *d2) { return strcmp((char *) d1, (char *)d2); } static char ** scandir(char *name) { char **cp; Dir db[32]; int nitems; int fd, n; if ((fd = open(name, OREAD)) < 0) panic(2, "can't open %s\n", name); cp = 0; nitems = 0; while((n = dirread(fd, db, sizeof db)) > 0){ n /= sizeof(Dir); while (n--) { cp = REALLOC(cp, char *, (nitems+1)); cp[nitems] = MALLOC(char, strlen(db[n].name)+1); strcpy(cp[nitems], db[n].name); nitems++; } } cp = REALLOC(cp, char*, (nitems+1)); cp[nitems] = 0; close(fd); qsort((char *)cp, nitems, sizeof(char*), itemcmp); return cp; } static int isdotordotdot(char *p) { if (*p == '.') { if (!p[1]) return 1; if (p[1] == '.' && !p[2]) return 1; } return 0; } void diffdir(char *f, char *t, int level) { char **df, **dt, **dirf, **dirt; char *from, *to; int res; char fb[MAXPATHLEN+1], tb[MAXPATHLEN+1]; df = dirf = scandir(f); dt = dirt = scandir(t); while (*df || *dt) { from = *df++; to = *dt++; if (from && isdotordotdot(from)) continue; if (to && isdotordotdot(to)) continue; if (!from) res = 1; else if (!to) res = -1; else res = strcmp(from, to); if (res < 0) { if (mode == 0) Bprint(&stdout, "Only in %s: %s\n", f, from); continue; } if (res > 0) { if (mode == 0) Bprint(&stdout, "Only in %s: %s\n", t, to); continue; } if (mkpathname(fb, f, from)) continue; if (mkpathname(tb, t, to)) continue; diff(fb, tb, level+1); } for (df = dirf; *df; df++) FREE(*df); for (dt = dirt; *dt; dt++) FREE(*dt); FREE(dirf); FREE(dirt); }