readdir.c 995 B

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