readdir64_r.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <features.h>
  2. #ifdef __UCLIBC_HAS_LFS__
  3. #if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS != 64
  4. #undef _FILE_OFFSET_BITS
  5. #define _FILE_OFFSET_BITS 64
  6. #endif
  7. #ifndef __USE_LARGEFILE64
  8. # define __USE_LARGEFILE64 1
  9. #endif
  10. /* We absolutely do _NOT_ want interfaces silently
  11. * renamed under us or very bad things will happen... */
  12. #ifdef __USE_FILE_OFFSET64
  13. # undef __USE_FILE_OFFSET64
  14. #endif
  15. #include <errno.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <dirent.h>
  20. #include "dirstream.h"
  21. int readdir64_r(DIR *dir, struct dirent64 *entry, struct dirent64 **result)
  22. {
  23. int ret;
  24. ssize_t bytes;
  25. struct dirent64 *de;
  26. if (!dir) {
  27. __set_errno(EBADF);
  28. return(EBADF);
  29. }
  30. de = NULL;
  31. __pthread_mutex_lock(&(dir->dd_lock));
  32. do {
  33. if (dir->dd_size <= dir->dd_nextloc) {
  34. /* read dir->dd_max bytes of directory entries. */
  35. bytes = __getdents64(dir->dd_fd, dir->dd_buf, dir->dd_max);
  36. if (bytes <= 0) {
  37. *result = NULL;
  38. ret = errno;
  39. goto all_done;
  40. }
  41. dir->dd_size = bytes;
  42. dir->dd_nextloc = 0;
  43. }
  44. de = (struct dirent64 *) (((char *) dir->dd_buf) + dir->dd_nextloc);
  45. /* Am I right? H.J. */
  46. dir->dd_nextloc += de->d_reclen;
  47. /* We have to save the next offset here. */
  48. dir->dd_nextoff = de->d_off;
  49. /* Skip deleted files. */
  50. } while (de->d_ino == 0);
  51. if (de == NULL) {
  52. *result = NULL;
  53. } else {
  54. *result = __memcpy (entry, de, de->d_reclen);
  55. }
  56. ret = 0;
  57. all_done:
  58. __pthread_mutex_unlock(&(dir->dd_lock));
  59. return((de != NULL)? 0 : ret);
  60. }
  61. #endif /* __UCLIBC_HAS_LFS__ */