3
0

iod.c 1.1 KB

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