telldir.c 791 B

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. telldir -- report directory stream position
  3. last edit: 25-Apr-1987 D A Gwyn
  4. NOTE: 4.nBSD directory compaction makes seekdir() & telldir()
  5. practically impossible to do right. Avoid using them!
  6. */
  7. #include <errno.h>
  8. #include <sys/errno.h>
  9. #include <sys/types.h>
  10. #include <dirent.h>
  11. extern off_t _lseek(int d, int offset, int whence);
  12. #ifndef SEEK_CUR
  13. #define SEEK_CUR 1
  14. #endif
  15. off_t
  16. telldir(register DIR *dirp) /* return offset of next entry */
  17. {
  18. if ( dirp == NULL || dirp->dd_buf == NULL )
  19. {
  20. errno = EFAULT;
  21. return -1; /* invalid pointer */
  22. }
  23. if ( dirp->dd_loc < dirp->dd_size ) /* valid index */
  24. return ((struct dirent *)&dirp->dd_buf[dirp->dd_loc])->d_off;
  25. else /* beginning of next directory block */
  26. return _lseek( dirp->dd_fd, (off_t)0, SEEK_CUR );
  27. }