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