readdir.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. 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. de = NULL;
  31. goto all_done;
  32. }
  33. dir->dd_size = bytes;
  34. dir->dd_nextloc = 0;
  35. }
  36. de = (__DIRENT_TYPE *) (((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. all_done:
  44. __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
  45. return de;
  46. }
  47. libc_hidden_def(__READDIR)
  48. #if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
  49. strong_alias_untyped(readdir,readdir64)
  50. libc_hidden_def(readdir64)
  51. #endif