#include #include #include #include #include #include void recurse(const char *path) { DIR *d = opendir(path); if (!d) { warn("couldn't open %s", path); return; } for (;;) { struct dirent *dent; errno = 0; dent = readdir(d); if (!dent) { if (errno) { warn("when reading %s", path); } break; } printf("%s%s\n", path, dent->d_name); /* if the string ends with '/' */ if (strchr(dent->d_name, '\0')[-1] == '/') { char *next; if (asprintf(&next, "%s%s", path, dent->d_name) >= 0) { recurse(next); free(next); } } } closedir(d); } int main(int argc, char **argv) { if (argc < 2) { recurse("/"); } else { for (int i = 1; i < argc; i++) recurse(argv[i]); } return 0; }