ftell.c 588 B

12345678910111213141516171819202122232425262728293031
  1. /*
  2. * ftell.c - obtain the value of the file-position indicator of a stream
  3. */
  4. /* $Id$ */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include "loc_incl.h"
  9. long ftell(FILE *stream)
  10. {
  11. long result;
  12. int adjust = 0;
  13. if (io_testflag(stream,_IOREADING))
  14. adjust = -stream->_count;
  15. else if (io_testflag(stream,_IOWRITING)
  16. && stream->_buf
  17. && !io_testflag(stream,_IONBF))
  18. adjust = stream->_ptr - stream->_buf;
  19. else adjust = 0;
  20. result = lseek(fileno(stream), 0, SEEK_CUR);
  21. if ( result == -1 )
  22. return result;
  23. result += (long) adjust;
  24. return result;
  25. }