fseek.c 1001 B

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