ack/lang/cem/libcc.ansi/stdio/fseek.c

45 lines
1,005 B
C
Raw Normal View History

1989-05-30 13:34:25 +00:00
/*
* fseek.c - perform an fseek
*/
/* $Header$ */
#include <stdio.h>
1989-12-18 15:04:14 +00:00
#if (SEEK_CUR != 1) || (SEEK_END != 2) || (SEEK_SET != 0)
#error SEEK_* values are wrong
#endif
1989-05-30 13:34:25 +00:00
#include "loc_incl.h"
1989-12-18 15:04:14 +00:00
#include <sys/types.h>
off_t _lseek(int fildes, off_t offset, int whence);
1989-05-30 13:34:25 +00:00
int
fseek(FILE *stream, long int offset, int whence)
{
1991-09-30 16:24:45 +00:00
int adjust = 0;
1989-05-30 13:34:25 +00:00
long pos;
stream->_flags &= ~(_IOEOF | _IOERR);
/* Clear both the end of file and error flags */
1989-06-26 10:37:05 +00:00
if (io_testflag(stream, _IOREADING)) {
if (whence == SEEK_CUR
&& stream->_buf
&& !io_testflag(stream,_IONBF))
adjust = stream->_count;
1989-05-30 13:34:25 +00:00
stream->_count = 0;
1989-12-18 15:04:14 +00:00
} else if (io_testflag(stream,_IOWRITING)) {
1989-05-30 13:34:25 +00:00
fflush(stream);
1989-12-18 15:04:14 +00:00
} else /* neither reading nor writing. The buffer must be empty */
/* EMPTY */ ;
1989-06-26 10:37:05 +00:00
pos = _lseek(fileno(stream), offset - adjust, whence);
1989-06-26 10:37:05 +00:00
if (io_testflag(stream, _IOREAD) && io_testflag(stream, _IOWRITE))
stream->_flags &= ~(_IOREADING | _IOWRITING);
stream->_ptr = stream->_buf;
return ((pos == -1) ? -1 : 0);
1989-05-30 13:34:25 +00:00
}