Use stat(2) to figure out type of directory entry.

readdir(2) might not be able to give back the correct
type in the d_type member and we should properly handle
the DT_UNKNOWN value anyway.

Fixes #39.
This commit is contained in:
Joris Vink 2015-04-07 09:27:58 +02:00
parent d6ab1d7445
commit 1fae259603
1 changed files with 11 additions and 2 deletions

View File

@ -727,6 +727,7 @@ static void
cli_find_files(const char *path, void (*cb)(char *, struct dirent *))
{
DIR *d;
struct stat st;
struct dirent *dp;
char *fpath;
@ -739,12 +740,20 @@ cli_find_files(const char *path, void (*cb)(char *, struct dirent *))
continue;
(void)cli_vasprintf(&fpath, "%s/%s", path, dp->d_name);
if (stat(fpath, &st) == -1) {
fprintf(stderr, "stat(%s): %s\n", fpath, errno_s);
free(fpath);
continue;
}
if (dp->d_type == DT_DIR) {
if (S_ISDIR(st.st_mode)) {
cli_find_files(fpath, cb);
free(fpath);
} else {
} else if (S_ISREG(st.st_mode)) {
cb(fpath, dp);
} else {
fprintf(stderr, "ignoring %s\n", fpath);
free(fpath);
}
}