readdir64_r.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 <_lfs_64.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. int readdir64_r(DIR *dir, struct dirent64 *entry, struct dirent64 **result)
  14. {
  15. int ret;
  16. ssize_t bytes;
  17. struct dirent64 *de;
  18. if (!dir) {
  19. __set_errno(EBADF);
  20. return(EBADF);
  21. }
  22. de = NULL;
  23. __UCLIBC_MUTEX_LOCK(dir->dd_lock);
  24. do {
  25. if (dir->dd_size <= dir->dd_nextloc) {
  26. /* read dir->dd_max bytes of directory entries. */
  27. bytes = __getdents64(dir->dd_fd, dir->dd_buf, dir->dd_max);
  28. if (bytes <= 0) {
  29. *result = NULL;
  30. ret = (bytes==0)? 0 : errno;
  31. goto all_done;
  32. }
  33. dir->dd_size = bytes;
  34. dir->dd_nextloc = 0;
  35. }
  36. de = (struct dirent64 *) (((char *) dir->dd_buf) + dir->dd_nextloc);
  37. /* Am I right? H.J. */
  38. dir->dd_nextloc += de->d_reclen;
  39. /* We have to save the next offset here. */
  40. dir->dd_nextoff = de->d_off;
  41. /* Skip deleted files. */
  42. } while (de->d_ino == 0);
  43. if (de == NULL) {
  44. *result = NULL;
  45. } else {
  46. *result = memcpy (entry, de, de->d_reclen);
  47. }
  48. ret = 0;
  49. all_done:
  50. __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
  51. return((de != NULL)? 0 : ret);
  52. }
  53. libc_hidden_def(readdir64_r)