Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / libcc.ansi / stdio / fseek.c
1 /*
2  * fseek.c - perform an fseek
3  */
4 /* $Id: fseek.c,v 1.6 1994/06/24 11:50:01 ceriel Exp $ */
5
6 #include        <stdio.h>
7
8 #if     (SEEK_CUR != 1) || (SEEK_END != 2) || (SEEK_SET != 0)
9 #error SEEK_* values are wrong
10 #endif
11
12 #include        "loc_incl.h"
13
14 #include        <sys/types.h>
15
16 off_t _lseek(int fildes, off_t offset, int whence);
17
18 int
19 fseek(FILE *stream, long int offset, int whence)
20 {
21         int adjust = 0;
22         long pos;
23
24         stream->_flags &= ~(_IOEOF | _IOERR);
25         /* Clear both the end of file and error flags */
26
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 */ ;
37
38         pos = _lseek(fileno(stream), offset - adjust, whence);
39         if (io_testflag(stream, _IOREAD) && io_testflag(stream, _IOWRITE))
40                 stream->_flags &= ~(_IOREADING | _IOWRITING);
41
42         stream->_ptr = stream->_buf;
43         return ((pos == -1) ? -1 : 0);
44 }