Reputation: 2825
I want to read subdirectories and links to subdirectories only. With the following code I read all subdirs and links.
struct dirent* de;
DIR* dir = opendir(c_str());
if (!dir) { /* error handling */ }
while (NULL != (de = readdir(dir))) {
if (de->d_type != DT_DIR && de->d_type != DT_LNK)
continue;
// Do something with subdirectory
}
But how do I examine whether the link points to a subdirectory too? I do not want to read the entire linked directory to do this.
Upvotes: 1
Views: 435
Reputation: 1644
You can use function named stat
from <sys/stat.h>
:
struct dirent* de;
struct stat s;
DIR* dir = opendir(c_str());
if (!dir) { /* error handling */ }
while (NULL != (de = readdir(dir))) {
if (de->d_type != DT_DIR) {
char filename[256];
sprintf(filename, "%s/%s", c_str(), de->d_name);
if (stat(filename, &s) < 0)
continue;
if (!S_ISDIR(s.st_mode))
continue;
}
// Do something with subdirectory
}
Upvotes: 3