readdir.c 996 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <errno.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <dirent.h>
  6. #include "dirstream.h"
  7. struct dirent *readdir(DIR * dir)
  8. {
  9. ssize_t bytes;
  10. struct dirent *de;
  11. if (!dir) {
  12. __set_errno(EBADF);
  13. return NULL;
  14. }
  15. #ifdef __UCLIBC_HAS_THREADS__
  16. __pthread_mutex_lock(&(dir->dd_lock));
  17. #endif
  18. do {
  19. if (dir->dd_size <= dir->dd_nextloc) {
  20. /* read dir->dd_max bytes of directory entries. */
  21. bytes = __getdents(dir->dd_fd, dir->dd_buf, dir->dd_max);
  22. if (bytes <= 0) {
  23. de = NULL;
  24. goto all_done;
  25. }
  26. dir->dd_size = bytes;
  27. dir->dd_nextloc = 0;
  28. }
  29. de = (struct dirent *) (((char *) dir->dd_buf) + dir->dd_nextloc);
  30. /* Am I right? H.J. */
  31. dir->dd_nextloc += de->d_reclen;
  32. /* We have to save the next offset here. */
  33. dir->dd_nextoff = de->d_off;
  34. /* Skip deleted files. */
  35. } while (de->d_ino == 0);
  36. all_done:
  37. #ifdef __UCLIBC_HAS_THREADS__
  38. __pthread_mutex_unlock(&(dir->dd_lock));
  39. #endif
  40. return de;
  41. }