14 struct history *chain;
22 #define dirfd(d) (*(int *)d)
24 static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags, struct history *h)
26 size_t l = strlen(path), j = l && path[l-1]=='/' ? l-1 : l;
34 if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) {
35 if (!(flags & FTW_PHYS) && errno==ENOENT && !lstat(path, &st))
37 else if (errno != EACCES) return -1;
39 } else if (S_ISDIR(st.st_mode)) {
40 if (access(path, R_OK) < 0) type = FTW_DNR;
41 else if (flags & FTW_DEPTH) type = FTW_DP;
43 } else if (S_ISLNK(st.st_mode)) {
44 if (flags & FTW_PHYS) type = FTW_SL;
50 if ((flags & FTW_MOUNT) && h
51 && (st.st_dev != h->dev || st.st_ino != h->ino))
57 new.level = h ? h->level+1 : 0;
60 lev.level = new.level;
61 lev.base = h ? h->base : (name=strrchr(path, '/')) ? name-path : 0;
63 if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
66 for (; h; h = h->chain)
67 if (h->dev == st.st_dev && h->ino == st.st_ino)
70 if ((type == FTW_D || type == FTW_DP) && fd_limit) {
71 DIR *d = opendir(path);
74 while ((de = readdir(d))) {
75 if (de->d_name[0] == '.'
77 || (de->d_name[1]=='.'
78 && !de->d_name[2]))) continue;
79 if (strlen(de->d_name) >= PATH_MAX-l) {
85 strcpy(path+j+1, de->d_name);
86 if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
92 } else if (errno != EACCES) {
98 if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
104 int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags)
107 char pathbuf[PATH_MAX+1];
109 if (fd_limit <= 0) return 0;
113 errno = ENAMETOOLONG;
116 memcpy(pathbuf, path, l+1);
118 return do_nftw(pathbuf, fn, fd_limit, flags, NULL);