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
|
|
|
|
|
|
|
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 */
|
1990-01-22 11:13:26 +00:00
|
|
|
/* 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
|
|
|
}
|