readdir.c 922 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. __pthread_mutex_lock(&(dir->dd_lock));
  16. do {
  17. if (dir->dd_size <= dir->dd_nextloc) {
  18. /* read dir->dd_max bytes of directory entries. */
  19. bytes = __getdents(dir->dd_fd, dir->dd_buf, dir->dd_max);
  20. if (bytes <= 0) {
  21. de = NULL;
  22. goto all_done;
  23. }
  24. dir->dd_size = bytes;
  25. dir->dd_nextloc = 0;
  26. }
  27. de = (struct dirent *) (((char *) dir->dd_buf) + dir->dd_nextloc);
  28. /* Am I right? H.J. */
  29. dir->dd_nextloc += de->d_reclen;
  30. /* We have to save the next offset here. */
  31. dir->dd_nextoff = de->d_off;
  32. /* Skip deleted files. */
  33. } while (de->d_ino == 0);
  34. all_done:
  35. __pthread_mutex_unlock(&(dir->dd_lock));
  36. return de;
  37. }