readdir.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <errno.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <dirent.h>
  6. #include "dirstream.h"
  7. extern int getdents __P ((unsigned int fd, struct dirent *dirp, unsigned int count));
  8. struct dirent *readdir(DIR * dir)
  9. {
  10. int result;
  11. struct dirent *de;
  12. if (!dir) {
  13. __set_errno(EBADF);
  14. return NULL;
  15. }
  16. /* Are we running an old kernel? */
  17. if (dir->dd_getdents == no_getdents) {
  18. abort();
  19. }
  20. if (dir->dd_size <= dir->dd_nextloc) {
  21. /* read dir->dd_max bytes of directory entries. */
  22. result = getdents(dir->dd_fd, dir->dd_buf, dir->dd_max);
  23. /* We assume we have getdents (). */
  24. dir->dd_getdents = have_getdents;
  25. if (result <= 0) {
  26. result = -result;
  27. if (result > 0) {
  28. /* Are we right? */
  29. if (result == ENOSYS) {
  30. dir->dd_getdents = no_getdents;
  31. abort();
  32. }
  33. __set_errno(result);
  34. }
  35. return NULL;
  36. }
  37. dir->dd_size = result;
  38. dir->dd_nextloc = 0;
  39. }
  40. de = (struct dirent *) (((char *) dir->dd_buf) + dir->dd_nextloc);
  41. /* Am I right? H.J. */
  42. dir->dd_nextloc += de->d_reclen;
  43. /* We have to save the next offset here. */
  44. dir->dd_nextoff = de->d_off;
  45. return de;
  46. }