Learning by doing: Reading books and trying to understand the (code) examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.1 KiB

  1. #include "process_dir.h"
  2. #include <dirent.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. int process_dir_r(filestruct level) {
  6. if (!level.fullname) {
  7. if (level.name) {
  8. level.fullname = level.name;
  9. } else {
  10. level.fullname = ".";
  11. }
  12. }
  13. int errct = 0;
  14. DIR *current = opendir(level.fullname);
  15. if (!current) {
  16. return 1;
  17. }
  18. struct dirent *entry;
  19. while ((entry = readdir(current))) {
  20. if (entry->d_name[0] == '.') {
  21. continue;
  22. }
  23. filestruct next_level = level;
  24. next_level.name = entry->d_name;
  25. (void) asprintf(&next_level.fullname, "%s/%s", level.fullname, entry->d_name);
  26. if (next_level.fullname == NULL) {
  27. return 1;
  28. }
  29. if (entry->d_type == DT_DIR) {
  30. next_level.depth++;
  31. if (level.directory_action) {
  32. level.directory_action(next_level);
  33. }
  34. errct += process_dir_r(next_level);
  35. } else if (entry->d_type == DT_REG && level.file_action) {
  36. level.file_action(next_level);
  37. errct += next_level.error;
  38. }
  39. if (next_level.fullname) {
  40. free(next_level.fullname);
  41. }
  42. }
  43. closedir(current);
  44. return errct;
  45. }