readdir64_r.c 1.4 KB

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