readdir.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /* convert */
  46. dir->dd_buf->d_ino = de->d_ino;
  47. dir->dd_buf->d_off = de->d_off;
  48. dir->dd_buf->d_reclen = de->d_reclen;
  49. dir->dd_buf->d_type = 0;
  50. if (strlen((char *) &de->d_type) > 10)
  51. de->d_name[10] = 0;
  52. strcpy(dir->dd_buf->d_name, (char *) &de->d_name);
  53. __set_errno(0);
  54. return dir->dd_buf;
  55. }