readdir.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #define __need_NULL
  9. #include <stddef.h>
  10. #include "dirstream.h"
  11. #ifndef __READDIR
  12. # define __READDIR readdir
  13. # define __DIRENT_TYPE struct dirent
  14. # define __GETDENTS __getdents
  15. #endif
  16. __DIRENT_TYPE *__READDIR(DIR * dir)
  17. {
  18. ssize_t bytes;
  19. __DIRENT_TYPE *de;
  20. if (!dir) {
  21. __set_errno(EBADF);
  22. return NULL;
  23. }
  24. __UCLIBC_MUTEX_LOCK(dir->dd_lock);
  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. de = NULL;
  30. goto all_done;
  31. }
  32. dir->dd_size = bytes;
  33. dir->dd_nextloc = 0;
  34. }
  35. de = (__DIRENT_TYPE *) (((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. all_done:
  41. __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
  42. return de;
  43. }
  44. libc_hidden_def(__READDIR)
  45. #if __WORDSIZE == 64
  46. strong_alias_untyped(readdir,readdir64)
  47. libc_hidden_def(readdir64)
  48. #endif