1
0
Fork 0

num_files: opendir() returns a directory stream

opendir() returns a directory stream, not a file descriptor

Co-authored-by: drkhsh <me@drkhsh.at>
Signed-off-by: drkhsh <me@drkhsh.at>
This commit is contained in:
planet36 2021-05-11 22:45:34 -04:00 committed by drkhsh
parent c432c981df
commit 984f45719e

View file

@ -10,23 +10,23 @@ const char *
num_files(const char *path)
{
struct dirent *dp;
DIR *fd;
DIR *dir;
int num;
if (!(fd = opendir(path))) {
if (!(dir = opendir(path))) {
warn("opendir '%s':", path);
return NULL;
}
num = 0;
while ((dp = readdir(fd))) {
while ((dp = readdir(dir))) {
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
continue; /* skip self and parent */
num++;
}
closedir(fd);
closedir(dir);
return bprintf("%d", num);
}