readdir.c 997 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <features.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <dirent.h>
  7. #include "dirstream.h"
  8. libc_hidden_proto(readdir)
  9. struct dirent *readdir(DIR * dir)
  10. {
  11. ssize_t bytes;
  12. struct dirent *de;
  13. if (!dir) {
  14. __set_errno(EBADF);
  15. return NULL;
  16. }
  17. __pthread_mutex_lock(&(dir->dd_lock));
  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. __pthread_mutex_unlock(&(dir->dd_lock));
  38. return de;
  39. }
  40. libc_hidden_def(readdir)