readdir_r.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 <dirent.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #define __need_NULL
  10. #include <stddef.h>
  11. #include "dirstream.h"
  12. #ifndef __READDIR_R
  13. # define __READDIR_R readdir_r
  14. # define __DIRENT_TYPE struct dirent
  15. # define __GETDENTS __getdents
  16. #endif
  17. int __READDIR_R(DIR *dir, __DIRENT_TYPE *entry, __DIRENT_TYPE **result)
  18. {
  19. int ret;
  20. ssize_t bytes;
  21. __DIRENT_TYPE *de;
  22. if (!dir) {
  23. __set_errno(EBADF);
  24. return(EBADF);
  25. }
  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 = __GETDENTS(dir->dd_fd, dir->dd_buf, dir->dd_max);
  31. if (bytes <= 0) {
  32. de = NULL;
  33. *result = NULL;
  34. ret = (bytes==0)? 0 : errno;
  35. goto all_done;
  36. }
  37. dir->dd_size = bytes;
  38. dir->dd_nextloc = 0;
  39. }
  40. de = (__DIRENT_TYPE *) (((char *) dir->dd_buf) + dir->dd_nextloc);
  41. /* Am I right? H.J. */
  42. dir->dd_nextloc += de->d_reclen;
  43. /* We have to save the next offset here. */
  44. dir->dd_nextoff = de->d_off;
  45. /* Skip deleted files. */
  46. } while (de->d_ino == 0);
  47. if (de == NULL) {
  48. *result = NULL;
  49. } else {
  50. *result = memcpy (entry, de, de->d_reclen);
  51. }
  52. ret = 0;
  53. all_done:
  54. __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
  55. return((de != NULL)? 0 : ret);
  56. }
  57. libc_hidden_def(__READDIR_R)
  58. #if __WORDSIZE == 64
  59. strong_alias_untyped(readdir_r,readdir64_r)
  60. libc_hidden_def(readdir64_r)
  61. #endif