1989-05-30 13:34:25 +00:00
|
|
|
/*
|
|
|
|
* fseek.c - perform an fseek
|
|
|
|
*/
|
1994-06-24 14:02:31 +00:00
|
|
|
/* $Id$ */
|
1989-05-30 13:34:25 +00:00
|
|
|
|
2007-04-21 23:18:14 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "loc_incl.h"
|
1989-05-30 13:34:25 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
int fseek(FILE* stream, long int offset, int whence)
|
1989-05-30 13:34:25 +00:00
|
|
|
{
|
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 */
|
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (io_testflag(stream, _IOREADING))
|
|
|
|
{
|
1989-06-26 10:37:05 +00:00
|
|
|
if (whence == SEEK_CUR
|
|
|
|
&& stream->_buf
|
2018-06-21 20:33:47 +00:00
|
|
|
&& !io_testflag(stream, _IONBF))
|
1989-06-26 10:37:05 +00:00
|
|
|
adjust = stream->_count;
|
1989-05-30 13:34:25 +00:00
|
|
|
stream->_count = 0;
|
2018-06-21 20:33:47 +00:00
|
|
|
}
|
|
|
|
else if (io_testflag(stream, _IOWRITING))
|
|
|
|
{
|
1989-05-30 13:34:25 +00:00
|
|
|
fflush(stream);
|
2018-06-21 20:33:47 +00:00
|
|
|
}
|
|
|
|
else /* neither reading nor writing. The buffer must be empty */
|
|
|
|
/* EMPTY */;
|
1989-06-26 10:37:05 +00:00
|
|
|
|
2007-04-21 23:18:14 +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
|
|
|
}
|