fork(2) download
  1. #define _DEFAULT_SOURCE
  2. #define _BSD_SOURCE
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <dirent.h>
  7. #include <errno.h>
  8.  
  9. void
  10. dir_recurse (DIR *parent, int level)
  11. {
  12. struct dirent *ent;
  13. DIR *child;
  14. int fd;
  15.  
  16. while ((ent = readdir(parent)) != NULL) {
  17. if ((strcmp(ent->d_name, ".") == 0) ||
  18. (strcmp(ent->d_name, "..") == 0)) {
  19. continue;
  20. }
  21. fd = openat(dirfd(parent), ent->d_name, O_RDONLY | O_DIRECTORY);
  22. if (fd != -1) {
  23. printf("%*s%s/\n", level, "", ent->d_name);
  24. child = fdopendir(fd);
  25. dir_recurse(child, level + 1);
  26. closedir(child);
  27. } else if (errno == ENOTDIR) {
  28. printf("%*s%s\n", level, "", ent->d_name);
  29. } else {
  30. perror("open");
  31. }
  32. }
  33. }
  34.  
  35. int
  36. main (int argc, char *argv)
  37. {
  38. DIR *root;
  39.  
  40. root = opendir("..");
  41. dir_recurse(root, 0);
  42. closedir(root);
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0s 5456KB
stdin
Standard input is empty
stdout
0al86f/
phRGOH/
 prog
T3gMUQ/
kJ1psI/
 prog
kQh2Pk/
 prog
PxAEbf/
 prog
QKT9Gv/