ftell.c 625 B

123456789101112131415161718192021222324252627282930313233343536373839
  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,
  7. (f->flags & F_APP) && f->wpos != f->wbase
  8. ? SEEK_END : SEEK_CUR);
  9. if (pos < 0) return pos;
  10. /* Adjust for data in buffer. */
  11. if (f->rend)
  12. pos += f->rpos - f->rend;
  13. else if (f->wbase)
  14. pos += f->wpos - f->wbase;
  15. return pos;
  16. }
  17. off_t __ftello(FILE *f)
  18. {
  19. off_t pos;
  20. FLOCK(f);
  21. pos = __ftello_unlocked(f);
  22. FUNLOCK(f);
  23. return pos;
  24. }
  25. long ftell(FILE *f)
  26. {
  27. off_t pos = __ftello(f);
  28. if (pos > LONG_MAX) {
  29. errno = EOVERFLOW;
  30. return -1;
  31. }
  32. return pos;
  33. }
  34. weak_alias(__ftello, ftello);