readdir64_r.c 1.5 KB

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