shithub: scc

ref: 3f605cf71d9fa11b487c97dee9900d19ad8ae69b
dir: /lib/c/fseek.c/

View raw version

#include <stdio.h>
#include "syscall.h"
#undef fseek

extern int _flsbuf(FILE *fp);

int
fseek(FILE *fp, long off, int whence)
{
	if (fp->flags & _IOERR)
		return EOF;

	if ((fp->flags & _IOWRITE) && _flsbuf(fp))
		return -1;
	else if (whence == SEEK_CUR && (fp->flags & _IOREAD))
		off -= fp->wp - fp->rd;

	if (_seek(fp->fd, off, type) < 0) {
		fp->flags |= _IOERR;
		return EOF;
	}

	if (fp->flags & _IORW)
		fp->flags &= ~(_IOREAD | _IOWRITE);
	fp->flags &= ~_IOEOF;

	return 0;
}