readdir64_r.c 1.4 KB

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