fseek.c 825 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* $Id$ */
  2. #include <stdio.h>
  3. fseek(iop, offset, where)
  4. FILE *iop;
  5. long offset;
  6. {
  7. int count;
  8. long lseek();
  9. long pos;
  10. iop->_flags &= ~(IO_EOF | IO_ERR);
  11. /* Clear both the end of file and error flags */
  12. if ( io_testflag(iop,IO_READMODE) ) {
  13. if ( where < 2 && iop->_buf && !io_testflag(iop,IO_UNBUFF) ) {
  14. count = iop->_count;
  15. pos = offset;
  16. if ( where == 0 )
  17. pos += count - lseek(fileno(iop), 0L,1);
  18. else
  19. offset -= count;
  20. if ( count > 0 && pos <= count
  21. && pos >= iop->_buf - iop->_ptr ) {
  22. iop->_ptr += (int) pos;
  23. iop->_count -= (int) pos;
  24. return(0);
  25. }
  26. }
  27. pos = lseek(fileno(iop), offset, where);
  28. iop->_count = 0;
  29. } else if ( io_testflag(iop,IO_WRITEMODE) ) {
  30. fflush(iop);
  31. pos = lseek(fileno(iop), offset, where);
  32. }
  33. return((pos == -1) ? -1 : 0 );
  34. }