3
0

iod.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * iod.c - Iterate a function on each entry of a directory
  3. *
  4. * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
  5. * Laboratoire MASI, Institut Blaise Pascal
  6. * Universite Pierre et Marie Curie (Paris VI)
  7. *
  8. * This file can be redistributed under the terms of the GNU Library General
  9. * Public License
  10. */
  11. /*
  12. * History:
  13. * 93/10/30 - Creation
  14. */
  15. #include "e2p.h"
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. int iterate_on_dir (const char * dir_name,
  20. int (*func) (const char *, struct dirent *, void *),
  21. void * private)
  22. {
  23. DIR * dir;
  24. struct dirent *de, *dep;
  25. int max_len, len;
  26. max_len = PATH_MAX + sizeof(struct dirent);
  27. de = (struct dirent *)xmalloc(max_len+1);
  28. memset(de, 0, max_len+1);
  29. dir = opendir (dir_name);
  30. if (dir == NULL) {
  31. free(de);
  32. return -1;
  33. }
  34. while ((dep = readdir (dir))) {
  35. len = sizeof(struct dirent);
  36. if (len < dep->d_reclen)
  37. len = dep->d_reclen;
  38. if (len > max_len)
  39. len = max_len;
  40. memcpy(de, dep, len);
  41. (*func) (dir_name, de, private);
  42. }
  43. free(de);
  44. closedir(dir);
  45. return 0;
  46. }