num_files.c 534 B

123456789101112131415161718192021222324252627282930313233
  1. /* See LICENSE file for copyright and license details. */
  2. #include <dirent.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "../slstatus.h"
  6. #include "../util.h"
  7. const char *
  8. num_files(const char *path)
  9. {
  10. struct dirent *dp;
  11. DIR *dir;
  12. int num;
  13. if (!(dir = opendir(path))) {
  14. warn("opendir '%s':", path);
  15. return NULL;
  16. }
  17. num = 0;
  18. while ((dp = readdir(dir))) {
  19. if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
  20. continue; /* skip self and parent */
  21. num++;
  22. }
  23. closedir(dir);
  24. return bprintf("%d", num);
  25. }