isblank() implementation.
[minix.git] / lib / libc / stdio / fseek.c
blob4e0d70948a3460d6adade5b8d640bee31d1e704a
1 /*
2 * fseek.c - perform an fseek
3 */
4 /* $Header$ */
6 #include <stdio.h>
8 #if (SEEK_CUR != 1) || (SEEK_END != 2) || (SEEK_SET != 0)
9 #error SEEK_* values are wrong
10 #endif
12 #include "loc_incl.h"
14 #include <sys/types.h>
16 off_t _lseek(int fildes, off_t offset, int whence);
18 int
19 fseek(FILE *stream, long int offset, int whence)
21 int adjust = 0;
22 long pos;
24 stream->_flags &= ~(_IOEOF | _IOERR);
25 /* Clear both the end of file and error flags */
27 if (io_testflag(stream, _IOREADING)) {
28 if (whence == SEEK_CUR
29 && stream->_buf
30 && !io_testflag(stream,_IONBF))
31 adjust = stream->_count;
32 stream->_count = 0;
33 } else if (io_testflag(stream,_IOWRITING)) {
34 fflush(stream);
35 } else /* neither reading nor writing. The buffer must be empty */
36 /* EMPTY */ ;
38 pos = _lseek(fileno(stream), offset - adjust, whence);
39 if (io_testflag(stream, _IOREAD) && io_testflag(stream, _IOWRITE))
40 stream->_flags &= ~(_IOREADING | _IOWRITING);
42 stream->_ptr = stream->_buf;
43 return ((pos == -1) ? -1 : 0);