rework langinfo code for ABI compat and for use by time code
[musl] / misc / nftw.c
1 #include <ftw.h>
2 #include <dirent.h>
3 #include <sys/stat.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <limits.h>
8 #include <pthread.h>
9 #include "libc.h"
10
11 struct history
12 {
13         struct history *chain;
14         dev_t dev;
15         ino_t ino;
16         int level;
17         int base;
18 };
19
20 #undef dirfd
21 #define dirfd(d) (*(int *)d)
22
23 static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags, struct history *h)
24 {
25         size_t l = strlen(path), j = l && path[l-1]=='/' ? l-1 : l;
26         struct stat st;
27         struct history new;
28         int type;
29         int r;
30         struct FTW lev;
31         char *name;
32
33         if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) {
34                 if (!(flags & FTW_PHYS) && errno==ENOENT && !lstat(path, &st))
35                         type = FTW_SLN;
36                 else if (errno != EACCES) return -1;
37                 else type = FTW_NS;
38         } else if (S_ISDIR(st.st_mode)) {
39                 if (access(path, R_OK) < 0) type = FTW_DNR;
40                 else if (flags & FTW_DEPTH) type = FTW_DP;
41                 else type = FTW_D;
42         } else if (S_ISLNK(st.st_mode)) {
43                 if (flags & FTW_PHYS) type = FTW_SL;
44                 else type = FTW_SLN;
45         } else {
46                 type = FTW_F;
47         }
48
49         if ((flags & FTW_MOUNT) && h
50          && (st.st_dev != h->dev || st.st_ino != h->ino))
51                 return 0;
52         
53         new.chain = h;
54         new.dev = st.st_dev;
55         new.ino = st.st_ino;
56         new.level = h ? h->level+1 : 0;
57         new.base = l+1;
58         
59         lev.level = new.level;
60         lev.base = h ? h->base : (name=strrchr(path, '/')) ? name-path : 0;
61
62         if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
63                 return r;
64
65         for (; h; h = h->chain)
66                 if (h->dev == st.st_dev && h->ino == st.st_ino)
67                         return 0;
68
69         if ((type == FTW_D || type == FTW_DP) && fd_limit) {
70                 DIR *d = opendir(path);
71                 if (d) {
72                         struct dirent *de;
73                         while ((de = readdir(d))) {
74                                 if (de->d_name[0] == '.'
75                                  && (!de->d_name[1]
76                                   || (de->d_name[1]=='.'
77                                    && !de->d_name[2]))) continue;
78                                 if (strlen(de->d_name) >= PATH_MAX-l) {
79                                         errno = ENAMETOOLONG;
80                                         closedir(d);
81                                         return -1;
82                                 }
83                                 path[j]='/';
84                                 strcpy(path+j+1, de->d_name);
85                                 if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
86                                         closedir(d);
87                                         return r;
88                                 }
89                         }
90                         closedir(d);
91                 } else if (errno != EACCES) {
92                         return -1;
93                 }
94         }
95
96         path[l] = 0;
97         if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
98                 return r;
99
100         return 0;
101 }
102
103 int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags)
104 {
105         int r, cs;
106         size_t l;
107         char pathbuf[PATH_MAX+1];
108
109         if (fd_limit <= 0) return 0;
110
111         l = strlen(path);
112         if (l > PATH_MAX) {
113                 errno = ENAMETOOLONG;
114                 return -1;
115         }
116         memcpy(pathbuf, path, l+1);
117         
118         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
119         r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
120         pthread_setcancelstate(cs, 0);
121         return r;
122 }
123
124 LFS64(nftw);