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

55 lines
1.3 KiB
C
Raw Normal View History

1989-05-30 13:34:25 +00:00
/*
* fseek.c - perform an fseek
*/
/* $Header$ */
#include <stdio.h>
#include <sys/file.h>
#include "loc_incl.h"
int lseek(int d, int offset, int whence);
int
fseek(FILE *stream, long int offset, int whence)
{
1989-06-26 10:37:05 +00:00
int count, adjust = 0;
1989-05-30 13:34:25 +00:00
long pos;
#if (SEEK_CUR != L_INCR) || (SEEK_SET != L_SET) || (SEEK_END != L_XTND)
int swhence; /* the real whence for lseek */
switch(whence) { /* map to UNIX values */
case SEEK_CUR: swhence = L_INCR; break;
case SEEK_SET: swhence = L_SET; break;
case SEEK_END: swhence = L_XTND; break;
default: swhence = whence; break;
}
#else
#define swhence whence
#endif
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;
pos = lseek(fileno(stream), offset - adjust, swhence);
1989-05-30 13:34:25 +00:00
stream->_count = 0;
1989-06-26 10:37:05 +00:00
}
else if (io_testflag(stream,_IOWRITING)) {
1989-05-30 13:34:25 +00:00
fflush(stream);
pos = lseek(fileno(stream), offset, swhence);
}
1989-06-26 10:37:05 +00:00
else /* neither reading nor writing. The buffer must be empty */
pos = lseek(fileno(stream), offset, swhence);
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
}