readdir.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. extern int getdents __P ((unsigned int fd, struct dirent *dirp, unsigned int count));
  8. struct dirent *readdir(DIR * dir)
  9. {
  10. ssize_t bytes;
  11. struct dirent *de;
  12. if (!dir) {
  13. __set_errno(EBADF);
  14. return NULL;
  15. }
  16. #ifdef _POSIX_THREADS
  17. pthread_mutex_lock(dir->dd_lock);
  18. #endif
  19. do {
  20. if (dir->dd_size <= dir->dd_nextloc) {
  21. /* read dir->dd_max bytes of directory entries. */
  22. bytes = getdents(dir->dd_fd, dir->dd_buf, dir->dd_max);
  23. if (bytes <= 0) {
  24. de = NULL;
  25. goto all_done;
  26. }
  27. dir->dd_size = bytes;
  28. dir->dd_nextloc = 0;
  29. }
  30. de = (struct dirent *) (((char *) dir->dd_buf) + dir->dd_nextloc);
  31. /* Am I right? H.J. */
  32. dir->dd_nextloc += de->d_reclen;
  33. /* We have to save the next offset here. */
  34. dir->dd_nextoff = de->d_off;
  35. /* Skip deleted files. */
  36. } while (de->d_ino == 0);
  37. all_done:
  38. #ifdef _POSIX_THREADS
  39. pthread_mutex_unlock(dir->dd_lock);
  40. #endif
  41. return de;
  42. }