readdir_r.c 1.3 KB

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