#ifdef BSD4_2
-#include "/usr/include/sys/dir.h"
+#define MAXNAMLEN 255
#else
-#define DIRBLKSIZ 512
#define MAXNAMLEN 14
+#endif
+#define DIRBLKSIZ 512
#undef DIRSIZ
#define DIRSIZ(dp) \
((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1+3)&~3))
int dd_fd;
long dd_loc;
long dd_size;
- char dd_buf[DIRBLKSIZ];
+ char *dd_buf;
+ int dd_bsize;
};
typedef struct _dirdesc DIR;
#ifndef NULL
#define NULL 0
#endif
-extern DIR *opendir();
+extern DIR *opendir();
extern struct direct *readdir();
-extern long telldir();
-extern seekdir();
+extern long telldir();
+extern seekdir();
#define rewinddir(dirp) seekdir((dirp), 0L)
-extern closedir();
-#endif
+extern closedir();
close(dirp->dd_fd);
dirp->dd_fd = -1;
dirp->dd_loc = 0;
- free(dirp);
+ free(dirp->dd_buf);
+ free((char *)dirp);
}
close (fd);
return NULL;
}
+ if ((int) stbuf.st_size == stbuf.st_size &&
+ (dirp->dd_buf = malloc((unsigned) stbuf.st_size))) {
+ dirp->dd_bsize = stbuf.st_size;
+ }
+ else if (dirp->dd_buf = malloc(8*DIRBLKSIZ)) {
+ dirp->dd_bsize = 8 * DIRBLKSIZ;
+ }
+ else if (dirp->dd_buf = malloc(DIRBLKSIZ)) {
+ dirp->dd_bsize = DIRBLKSIZ;
+ }
+ else {
+ close(fd);
+ free((char *) dirp);
+ return NULL;
+ }
dirp->dd_fd = fd;
dirp->dd_loc = 0;
return dirp;
for (;;) {
if (dirp->dd_loc == 0) {
dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
- DIRBLKSIZ);
+ dirp->dd_bsize);
if (dirp->dd_size <= 0) {
dirp->dd_size = 0;
return NULL;
curloc = telldir(dirp);
if (loc == curloc)
return;
- base = loc & ~(DIRBLKSIZ - 1);
- offset = loc & (DIRBLKSIZ - 1);
+ offset = loc % dirp->dd_bsize;
+ base = loc - offset;
if (dirp->dd_loc != 0 && offset != 0 &&
- (curloc & ~(DIRBLKSIZ-1)) == base) {
+ (curloc - (curloc % dirp->dd_bsize)) == base) {
dirp->dd_loc = offset;
return;
}
(void) lseek(dirp->dd_fd, base, 0);
dirp->dd_loc = 0;
dirp->dd_size = 0;
- if (offset == 0)
- (void) readdir(dirp);
- else {
- while (dirp->dd_loc < offset) {
- if (readdir(dirp) == (struct direct *) 0)
- return;
- }
+ while (dirp->dd_loc < offset) {
+ if (readdir(dirp) == (struct direct *) 0)
+ return;
}
}