forked from mirrors/kore
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:
parent
d6ab1d7445
commit
1fae259603
13
src/cli.c
13
src/cli.c
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue