aboutsummaryrefslogtreecommitdiff
path: root/dirs.c
diff options
context:
space:
mode:
authorOmar Polo <op@omarpolo.com>2021-04-25 12:45:17 +0000
committerOmar Polo <op@omarpolo.com>2021-04-25 12:45:17 +0000
commit5aba63956a04590ade71cffa0cef0a5d2356f42b (patch)
treeea3b029f043f533109302633b548fbe586b93549 /dirs.c
parente76f2c74b8a27db8a3d05aee6293b94ecc8452ce (diff)
d->d_namelen doesn't seem to be available on linux
Diffstat (limited to 'dirs.c')
-rw-r--r--dirs.c13
1 files changed, 6 insertions, 7 deletions
diff --git a/dirs.c b/dirs.c
index 7965f8f..66e38d4 100644
--- a/dirs.c
+++ b/dirs.c
@@ -61,9 +61,9 @@
* (dp->d_namlen + 1), rounded up to a 4 byte boundary.
*/
#undef DIRSIZ
-#define DIRSIZ(dp) \
+#define DIRSIZ(dp, namlen) \
((sizeof(struct dirent) - sizeof(dp)->d_name) + \
- (((dp)->d_namlen + 1 + 3) &~ 3))
+ ((namlen + 1 + 3) &~ 3))
int
scandir_fd(int fd, struct dirent ***namelist,
@@ -71,8 +71,7 @@ scandir_fd(int fd, struct dirent ***namelist,
int (*dcomp)(const struct dirent **, const struct dirent **))
{
struct dirent *d, *p, **names = NULL;
- size_t /* arraysz, */ nitems = 0;
- long arraysz;
+ size_t arraysz, namlen, nitems = 0;
struct stat stb;
DIR *dirp;
@@ -122,15 +121,15 @@ scandir_fd(int fd, struct dirent ***namelist,
/*
* Make a minimum size copy of the data
*/
- p = malloc(DIRSIZ(d));
+ namlen = strlen(d->d_name);
+ p = malloc(DIRSIZ(d, namlen));
if (p == NULL)
goto fail;
p->d_ino = d->d_ino;
p->d_type = d->d_type;
p->d_reclen = d->d_reclen;
- p->d_namlen = d->d_namlen;
- bcopy(d->d_name, p->d_name, p->d_namlen + 1);
+ bcopy(d->d_name, p->d_name, namlen + 1);
names[nitems++] = p;
}
closedir(dirp);