readdir_r.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 <errno.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <dirent.h>
  11. #include "dirstream.h"
  12. int readdir_r(DIR *dir, struct dirent *entry, struct dirent **result)
  13. {
  14. int ret;
  15. ssize_t bytes;
  16. struct dirent *de;
  17. if (!dir) {
  18. __set_errno(EBADF);
  19. return(EBADF);
  20. }
  21. de = NULL;
  22. __UCLIBC_MUTEX_LOCK(dir->dd_lock);
  23. do {
  24. if (dir->dd_size <= dir->dd_nextloc) {
  25. /* read dir->dd_max bytes of directory entries. */
  26. bytes = __getdents(dir->dd_fd, dir->dd_buf, dir->dd_max);
  27. if (bytes <= 0) {
  28. *result = NULL;
  29. ret = (bytes==0)? 0 : errno;
  30. goto all_done;
  31. }
  32. dir->dd_size = bytes;
  33. dir->dd_nextloc = 0;
  34. }
  35. de = (struct dirent *) (((char *) dir->dd_buf) + dir->dd_nextloc);
  36. /* Am I right? H.J. */
  37. dir->dd_nextloc += de->d_reclen;
  38. /* We have to save the next offset here. */
  39. dir->dd_nextoff = de->d_off;
  40. /* Skip deleted files. */
  41. } while (de->d_ino == 0);
  42. if (de == NULL) {
  43. *result = NULL;
  44. } else {
  45. *result = memcpy (entry, de, de->d_reclen);
  46. }
  47. ret = 0;
  48. all_done:
  49. __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
  50. return((de != NULL)? 0 : ret);
  51. }
  52. libc_hidden_def(readdir_r)