fseek.c 865 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * fseek.c - perform an fseek
  3. */
  4. /* $Id$ */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include "loc_incl.h"
  9. int
  10. fseek(FILE *stream, long int offset, int whence)
  11. {
  12. int adjust = 0;
  13. long pos;
  14. stream->_flags &= ~(_IOEOF | _IOERR);
  15. /* Clear both the end of file and error flags */
  16. if (io_testflag(stream, _IOREADING)) {
  17. if (whence == SEEK_CUR
  18. && stream->_buf
  19. && !io_testflag(stream,_IONBF))
  20. adjust = stream->_count;
  21. stream->_count = 0;
  22. } else if (io_testflag(stream,_IOWRITING)) {
  23. fflush(stream);
  24. } else /* neither reading nor writing. The buffer must be empty */
  25. /* EMPTY */ ;
  26. pos = lseek(fileno(stream), offset - adjust, whence);
  27. if (io_testflag(stream, _IOREAD) && io_testflag(stream, _IOWRITE))
  28. stream->_flags &= ~(_IOREADING | _IOWRITING);
  29. stream->_ptr = stream->_buf;
  30. return ((pos == -1) ? -1 : 0);
  31. }