num_files.c 599 B

123456789101112131415161718192021222324252627282930313233
  1. /* See LICENSE file for copyright and license details. */
  2. #include <err.h>
  3. #include <stdio.h>
  4. #include <dirent.h>
  5. #include <string.h>
  6. #include <inttypes.h>
  7. #include "../lib/util.h"
  8. void
  9. num_files(char *out, const char *path, uint32_t __unused _i, void __unused *_p)
  10. {
  11. DIR *fd;
  12. size_t num;
  13. struct dirent *dp;
  14. if (!(fd = opendir(path))) {
  15. warn("opendir '%s'", path);
  16. ERRRET(out);
  17. }
  18. num = 0;
  19. while ((dp = readdir(fd))) {
  20. if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
  21. continue; /* skip self and parent */
  22. num++;
  23. }
  24. closedir(fd);
  25. bprintf(out, "%" PRIu64, num);
  26. }