1 fd04aace 2003-11-23 devnull #include <u.h>
2 f7012583 2003-11-25 devnull #define NOPLAN9DEFINES
3 fd04aace 2003-11-23 devnull #include <libc.h>
4 fd04aace 2003-11-23 devnull #include <sys/stat.h>
5 fd04aace 2003-11-23 devnull #include <dirent.h>
7 2acd6fa6 2005-02-08 devnull extern int _p9dir(struct stat*, struct stat*, char*, Dir*, char**, char*);
9 64bcfff3 2003-11-25 devnull #if defined(__linux__)
10 fd04aace 2003-11-23 devnull static int
11 64bcfff3 2003-11-25 devnull mygetdents(int fd, struct dirent *buf, int n)
13 64bcfff3 2003-11-25 devnull off_t off;
16 03480d73 2004-01-09 devnull /* This doesn't match the man page, but it works in Debian with a 2.2 kernel */
17 64bcfff3 2003-11-25 devnull off = p9seek(fd, 0, 1);
18 64bcfff3 2003-11-25 devnull nn = getdirentries(fd, (void*)buf, n, &off);
19 64bcfff3 2003-11-25 devnull return nn;
21 b7d8e86a 2006-06-26 devnull #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
22 64bcfff3 2003-11-25 devnull static int
23 64bcfff3 2003-11-25 devnull mygetdents(int fd, struct dirent *buf, int n)
25 912fba95 2003-11-24 devnull long off;
26 669250d1 2003-12-03 devnull return getdirentries(fd, (void*)buf, n, &off);
28 b7d8e86a 2006-06-26 devnull #elif defined(__sun__) || defined(__NetBSD__)
29 64bcfff3 2003-11-25 devnull static int
30 64bcfff3 2003-11-25 devnull mygetdents(int fd, struct dirent *buf, int n)
32 64bcfff3 2003-11-25 devnull return getdents(fd, (void*)buf, n);
34 e6c837d6 2009-07-15 rsc #elif defined(__AIX__)
36 e6c837d6 2009-07-15 rsc mygetdents(int fd, struct dirent *buf, int n)
38 e6c837d6 2009-07-15 rsc return getdirent(fd, (void*)buf, n);
42 d2c4ee9e 2003-11-24 devnull static int
43 fd04aace 2003-11-23 devnull countde(char *p, int n)
47 fd04aace 2003-11-23 devnull struct dirent *de;
51 fd04aace 2003-11-23 devnull while(p < e){
52 fd04aace 2003-11-23 devnull de = (struct dirent*)p;
53 fd04aace 2003-11-23 devnull if(de->d_reclen <= 4+2+2+1 || p+de->d_reclen > e)
55 1c253ceb 2003-11-23 devnull if(de->d_name[0]=='.' && de->d_name[1]==0)
56 1c253ceb 2003-11-23 devnull de->d_name[0] = 0;
57 1c253ceb 2003-11-23 devnull else if(de->d_name[0]=='.' && de->d_name[1]=='.' && de->d_name[2]==0)
58 1c253ceb 2003-11-23 devnull de->d_name[0] = 0;
60 fd04aace 2003-11-23 devnull p += de->d_reclen;
62 fd04aace 2003-11-23 devnull return m;
65 fd04aace 2003-11-23 devnull static int
66 fd04aace 2003-11-23 devnull dirpackage(int fd, char *buf, int n, Dir **dp)
68 fd04aace 2003-11-23 devnull int oldwd;
69 fd04aace 2003-11-23 devnull char *p, *str, *estr;
70 fd04aace 2003-11-23 devnull int i, nstr, m;
71 fd04aace 2003-11-23 devnull struct dirent *de;
72 2acd6fa6 2005-02-08 devnull struct stat st, lst;
75 fd04aace 2003-11-23 devnull n = countde(buf, n);
76 fd04aace 2003-11-23 devnull if(n <= 0)
77 fd04aace 2003-11-23 devnull return n;
79 fd04aace 2003-11-23 devnull if((oldwd = open(".", O_RDONLY)) < 0)
80 fd04aace 2003-11-23 devnull return -1;
81 fd04aace 2003-11-23 devnull if(fchdir(fd) < 0)
82 fd04aace 2003-11-23 devnull return -1;
85 fd04aace 2003-11-23 devnull nstr = 0;
87 fd04aace 2003-11-23 devnull for(i=0; i<n; i++){
88 fd04aace 2003-11-23 devnull de = (struct dirent*)p;
89 46199d11 2005-02-08 devnull memset(&lst, 0, sizeof lst);
90 1c8efa18 2003-12-04 devnull if(de->d_name[0] == 0)
91 1c8efa18 2003-12-04 devnull /* nothing */ {}
92 0a229052 2005-02-08 devnull else if(lstat(de->d_name, &lst) < 0)
93 1c253ceb 2003-11-23 devnull de->d_name[0] = 0;
95 0a229052 2005-02-08 devnull st = lst;
96 46199d11 2005-02-08 devnull if(S_ISLNK(lst.st_mode))
97 0a229052 2005-02-08 devnull stat(de->d_name, &st);
98 0a229052 2005-02-08 devnull nstr += _p9dir(&lst, &st, de->d_name, nil, nil, nil);
100 fd04aace 2003-11-23 devnull p += de->d_reclen;
103 fd04aace 2003-11-23 devnull d = malloc(sizeof(Dir)*n+nstr);
104 fd04aace 2003-11-23 devnull if(d == nil){
105 fd04aace 2003-11-23 devnull fchdir(oldwd);
106 fd04aace 2003-11-23 devnull close(oldwd);
107 fd04aace 2003-11-23 devnull return -1;
109 fd04aace 2003-11-23 devnull str = (char*)&d[n];
110 fd04aace 2003-11-23 devnull estr = str+nstr;
112 fd04aace 2003-11-23 devnull p = buf;
114 fd04aace 2003-11-23 devnull for(i=0; i<n; i++){
115 fd04aace 2003-11-23 devnull de = (struct dirent*)p;
116 46199d11 2005-02-08 devnull if(de->d_name[0] != 0 && lstat(de->d_name, &lst) >= 0){
117 2acd6fa6 2005-02-08 devnull st = lst;
118 2acd6fa6 2005-02-08 devnull if((lst.st_mode&S_IFMT) == S_IFLNK)
119 2acd6fa6 2005-02-08 devnull stat(de->d_name, &st);
120 2acd6fa6 2005-02-08 devnull _p9dir(&lst, &st, de->d_name, &d[m++], &str, estr);
122 fd04aace 2003-11-23 devnull p += de->d_reclen;
125 fd04aace 2003-11-23 devnull fchdir(oldwd);
126 fd04aace 2003-11-23 devnull close(oldwd);
127 fd04aace 2003-11-23 devnull *dp = d;
128 fd04aace 2003-11-23 devnull return m;
132 fd04aace 2003-11-23 devnull dirread(int fd, Dir **dp)
134 fd04aace 2003-11-23 devnull char *buf;
135 fd04aace 2003-11-23 devnull struct stat st;
138 fd04aace 2003-11-23 devnull *dp = 0;
140 fd04aace 2003-11-23 devnull if(fstat(fd, &st) < 0)
141 fd04aace 2003-11-23 devnull return -1;
143 fd04aace 2003-11-23 devnull if(st.st_blksize < 8192)
144 fd04aace 2003-11-23 devnull st.st_blksize = 8192;
146 fd04aace 2003-11-23 devnull buf = malloc(st.st_blksize);
147 fd04aace 2003-11-23 devnull if(buf == nil)
148 fd04aace 2003-11-23 devnull return -1;
150 912fba95 2003-11-24 devnull n = mygetdents(fd, (void*)buf, st.st_blksize);
151 fd04aace 2003-11-23 devnull if(n < 0){
152 fd04aace 2003-11-23 devnull free(buf);
153 fd04aace 2003-11-23 devnull return -1;
155 fd04aace 2003-11-23 devnull n = dirpackage(fd, buf, n, dp);
156 fd04aace 2003-11-23 devnull free(buf);
157 fd04aace 2003-11-23 devnull return n;
162 fd04aace 2003-11-23 devnull dirreadall(int fd, Dir **d)
164 fd04aace 2003-11-23 devnull uchar *buf, *nbuf;
165 fd04aace 2003-11-23 devnull long n, ts;
166 fd04aace 2003-11-23 devnull struct stat st;
168 fd04aace 2003-11-23 devnull if(fstat(fd, &st) < 0)
169 fd04aace 2003-11-23 devnull return -1;
171 fd04aace 2003-11-23 devnull if(st.st_blksize < 8192)
172 fd04aace 2003-11-23 devnull st.st_blksize = 8192;
174 fd04aace 2003-11-23 devnull buf = nil;
176 fd04aace 2003-11-23 devnull for(;;){
177 fd04aace 2003-11-23 devnull nbuf = realloc(buf, ts+st.st_blksize);
178 fd04aace 2003-11-23 devnull if(nbuf == nil){
179 fd04aace 2003-11-23 devnull free(buf);
180 fd04aace 2003-11-23 devnull return -1;
182 fd04aace 2003-11-23 devnull buf = nbuf;
183 912fba95 2003-11-24 devnull n = mygetdents(fd, (void*)(buf+ts), st.st_blksize);
184 fd04aace 2003-11-23 devnull if(n <= 0)
186 fd04aace 2003-11-23 devnull ts += n;
188 fd04aace 2003-11-23 devnull if(ts >= 0)
189 8ad51794 2004-03-25 devnull ts = dirpackage(fd, (char*)buf, ts, d);
190 fd04aace 2003-11-23 devnull free(buf);
191 fd04aace 2003-11-23 devnull if(ts == 0 && n < 0)
192 fd04aace 2003-11-23 devnull return -1;
193 fd04aace 2003-11-23 devnull return ts;