iterate_on_dir.c 579 B

12345678910111213141516171819202122232425262728
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * See README for additional information
  4. *
  5. * Licensed under GPLv2, see file LICENSE in this source tree.
  6. */
  7. //kbuild:lib-y += iterate_on_dir.o
  8. #include "libbb.h"
  9. /* Iterate a function on each entry of a directory */
  10. int FAST_FUNC iterate_on_dir(const char *dir_name,
  11. int FAST_FUNC (*func)(const char *, struct dirent *, void *),
  12. void *private)
  13. {
  14. DIR *dir;
  15. struct dirent *de;
  16. dir = opendir(dir_name);
  17. if (dir == NULL) {
  18. return -1;
  19. }
  20. while ((de = readdir(dir)) != NULL) {
  21. func(dir_name, de, private);
  22. }
  23. closedir(dir);
  24. return 0;
  25. }