diff --git a/07_trees/c/01_filesystem_dfs.c b/07_trees/c/01_filesystem_dfs.c new file mode 100644 index 00000000..800383f9 --- /dev/null +++ b/07_trees/c/01_filesystem_dfs.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include + +int filter(const struct dirent *entry) { + return strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0; +} + +void printnames(const char *dir) { + struct dirent **entries; + int count = scandir(dir, &entries, filter, alphasort); + + if (count < 0) { + perror("scandir"); + return; + } + + for (int i = 0; i < count; i++) { + char path[PATH_MAX]; + snprintf(path, sizeof(path), "%s/%s", dir, entries[i]->d_name); + + struct stat sb; + if (stat(path, &sb)) { + perror("stat"); + free(entries[i]); + continue; + } + + if (S_ISREG(sb.st_mode)) { + printf("%s\n", entries[i]->d_name); + } else if (S_ISDIR(sb.st_mode)) { + printnames(path); + } + + free(entries[i]); + } + free(entries); +} + +int main() { + printnames("pics"); + return 0; +}