readdir64_r.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. extern int getdents64 __P ((unsigned int fd, struct dirent64 *dirp, unsigned int count));
  22. int readdir64_r(DIR *dir, struct dirent64 *entry, struct dirent64 **result)
  23. {
  24. int ret;
  25. ssize_t bytes;
  26. struct dirent64 *de;
  27. if (!dir) {
  28. __set_errno(EBADF);
  29. return(EBADF);
  30. }
  31. de = NULL;
  32. #ifdef __UCLIBC_HAS_THREADS__
  33. pthread_mutex_lock(&(dir->dd_lock));
  34. #endif
  35. do {
  36. if (dir->dd_size <= dir->dd_nextloc) {
  37. /* read dir->dd_max bytes of directory entries. */
  38. bytes = getdents64(dir->dd_fd, dir->dd_buf, dir->dd_max);
  39. if (bytes <= 0) {
  40. *result = NULL;
  41. ret = errno;
  42. goto all_done;
  43. }
  44. dir->dd_size = bytes;
  45. dir->dd_nextloc = 0;
  46. }
  47. de = (struct dirent64 *) (((char *) dir->dd_buf) + dir->dd_nextloc);
  48. /* Am I right? H.J. */
  49. dir->dd_nextloc += de->d_reclen;
  50. /* We have to save the next offset here. */
  51. dir->dd_nextoff = de->d_off;
  52. /* Skip deleted files. */
  53. } while (de->d_ino == 0);
  54. if (de == NULL) {
  55. *result = NULL;
  56. } else {
  57. *result = memcpy (entry, de, de->d_reclen);
  58. }
  59. ret = 0;
  60. all_done:
  61. #ifdef __UCLIBC_HAS_THREADS__
  62. pthread_mutex_unlock(&(dir->dd_lock));
  63. #endif
  64. return((de != NULL)? 0 : ret);
  65. }
  66. #endif /* __UCLIBC_HAS_LFS__ */