readdir_r.c 1.4 KB

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