ftell.c 724 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. * ftell.c - obtain the value of the file-position indicator of a stream
  3. */
  4. /* $Id$ */
  5. #include <stdio.h>
  6. #if (SEEK_CUR != 1) || (SEEK_SET != 0) || (SEEK_END != 2)
  7. #error SEEK_* values are wrong
  8. #endif
  9. #include "loc_incl.h"
  10. #include <sys/types.h>
  11. off_t _lseek(int fildes, off_t offset, int whence);
  12. long ftell(FILE *stream)
  13. {
  14. long result;
  15. int adjust = 0;
  16. if (io_testflag(stream,_IOREADING))
  17. adjust = -stream->_count;
  18. else if (io_testflag(stream,_IOWRITING)
  19. && stream->_buf
  20. && !io_testflag(stream,_IONBF))
  21. adjust = stream->_ptr - stream->_buf;
  22. else adjust = 0;
  23. result = _lseek(fileno(stream), 0, SEEK_CUR);
  24. if ( result == -1 )
  25. return result;
  26. result += (long) adjust;
  27. return result;
  28. }