readdir.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #include <features.h>
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <dirent.h>
  12. #include "dirstream.h"
  13. libc_hidden_proto(readdir)
  14. struct dirent *readdir(DIR * dir)
  15. {
  16. ssize_t bytes;
  17. struct dirent *de;
  18. if (!dir) {
  19. __set_errno(EBADF);
  20. return NULL;
  21. }
  22. __pthread_mutex_lock(&(dir->dd_lock));
  23. do {
  24. if (dir->dd_size <= dir->dd_nextloc) {
  25. /* read dir->dd_max bytes of directory entries. */
  26. bytes = __getdents(dir->dd_fd, dir->dd_buf, dir->dd_max);
  27. if (bytes <= 0) {
  28. de = NULL;
  29. goto all_done;
  30. }
  31. dir->dd_size = bytes;
  32. dir->dd_nextloc = 0;
  33. }
  34. de = (struct dirent *) (((char *) dir->dd_buf) + dir->dd_nextloc);
  35. /* Am I right? H.J. */
  36. dir->dd_nextloc += de->d_reclen;
  37. /* We have to save the next offset here. */
  38. dir->dd_nextoff = de->d_off;
  39. /* Skip deleted files. */
  40. } while (de->d_ino == 0);
  41. all_done:
  42. __pthread_mutex_unlock(&(dir->dd_lock));
  43. return de;
  44. }
  45. libc_hidden_def(readdir)