diff --git a/src/lib9/dirread.c b/src/lib9/dirread.c index 40fbe3c..3b70938 100644 --- a/src/lib9/dirread.c +++ b/src/lib9/dirread.c @@ -6,46 +6,6 @@ extern int _p9dir(struct stat*, struct stat*, char*, Dir*, char**, char*); -#if defined(__linux__) -static int -mygetdents(int fd, struct dirent *buf, int n) -{ - off_t off; - int nn; - - /* This doesn't match the man page, but it works in Debian with a 2.2 kernel */ - off = p9seek(fd, 0, 1); - nn = getdirentries(fd, (void*)buf, n, &off); - return nn; -} -#elif defined(__APPLE__) -static int -mygetdents(int fd, struct dirent *buf, int n) -{ - long off; - return getdirentries(fd, (void*)buf, n, &off); -} -#elif defined(__FreeBSD__) || defined(__DragonFly__) -static int -mygetdents(int fd, struct dirent *buf, int n) -{ - off_t off; - return getdirentries(fd, (void*)buf, n, &off); -} -#elif defined(__sun__) || defined(__NetBSD__) || defined(__OpenBSD__) -static int -mygetdents(int fd, struct dirent *buf, int n) -{ - return getdents(fd, (void*)buf, n); -} -#elif defined(__AIX__) -static int -mygetdents(int fd, struct dirent *buf, int n) -{ - return getdirent(fd, (void*)buf, n); -} -#endif - #if defined(__DragonFly__) static inline int d_reclen(struct dirent *de) { return _DIRENT_DIRSIZ(de); } #else @@ -53,6 +13,31 @@ static inline int d_reclen(struct dirent *de) { return de->d_reclen; } #endif static int +mygetdents(int fd, struct dirent *buf, int n) +{ + int tmpfd; + char *p; + DIR *dirp; + struct dirent *entry; + + if ((tmpfd = dup(fd)) == -1) + return -1; + if ((dirp = fdopendir(tmpfd)) == nil) + return -1; + + p = (char*)buf; + while((entry = readdir(dirp)) != nil) { + if (d_reclen(entry) > n) + break; + + memcpy(p, entry, d_reclen(entry)); + p += d_reclen(entry); + n -= d_reclen(entry); + } + return (int)(p - (char*)buf); +} + +static int countde(char *p, int n) { char *e;