ftell.c 536 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include "stdio_impl.h"
  2. #include <limits.h>
  3. #include <errno.h>
  4. off_t __ftello_unlocked(FILE *f)
  5. {
  6. off_t pos = f->seek(f, 0, SEEK_CUR);
  7. if (pos < 0) return pos;
  8. /* Adjust for data in buffer. */
  9. return pos - (f->rend - f->rpos) + (f->wpos - f->wbase);
  10. }
  11. off_t __ftello(FILE *f)
  12. {
  13. off_t pos;
  14. FLOCK(f);
  15. pos = __ftello_unlocked(f);
  16. FUNLOCK(f);
  17. return pos;
  18. }
  19. long ftell(FILE *f)
  20. {
  21. off_t pos = __ftello(f);
  22. if (pos > LONG_MAX) {
  23. errno = EOVERFLOW;
  24. return -1;
  25. }
  26. return pos;
  27. }
  28. weak_alias(__ftello, ftello);
  29. LFS64(ftello);