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