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