readdir_r.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. if (dir->dd_size <= dir->dd_nextloc) {
  28. /* read dir->dd_max bytes of directory entries. */
  29. bytes = __GETDENTS(dir->dd_fd, dir->dd_buf, dir->dd_max);
  30. if (bytes <= 0) {
  31. de = NULL;
  32. *result = NULL;
  33. ret = (bytes==0)? 0 : errno;
  34. goto all_done;
  35. }
  36. dir->dd_size = bytes;
  37. dir->dd_nextloc = 0;
  38. }
  39. de = (__DIRENT_TYPE *) (((char *) dir->dd_buf) + dir->dd_nextloc);
  40. /* Am I right? H.J. */
  41. dir->dd_nextloc += de->d_reclen;
  42. /* We have to save the next offset here. */
  43. dir->dd_nextoff = de->d_off;
  44. if (de == NULL) {
  45. *result = NULL;
  46. } else {
  47. *result = memcpy (entry, de, de->d_reclen);
  48. }
  49. ret = 0;
  50. all_done:
  51. __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
  52. return((de != NULL)? 0 : ret);
  53. }
  54. libc_hidden_def(__READDIR_R)
  55. #if __WORDSIZE == 64
  56. strong_alias_untyped(readdir_r,readdir64_r)
  57. libc_hidden_def(readdir64_r)
  58. #endif