scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

fseek.c (483B)


      1 #include <stdio.h>
      2 
      3 #include "../syscall.h"
      4 #include "../libc.h"
      5 
      6 #undef fseek
      7 
      8 int
      9 fseek(FILE *fp, long off, int whence)
     10 {
     11 	if (fp->flags & _IOERR)
     12 		return EOF;
     13 
     14 	if ((fp->flags & _IOWRITE) && _flsbuf(fp))
     15 		return -1;
     16 	else if (whence == SEEK_CUR && (fp->flags & _IOREAD))
     17 		off -= fp->wp - fp->rp;
     18 
     19 	if (_lseek(fp->fd, off, whence) < 0) {
     20 		fp->flags |= _IOERR;
     21 		return EOF;
     22 	}
     23 
     24 	if (fp->flags & _IORW)
     25 		fp->flags &= ~(_IOREAD | _IOWRITE);
     26 	fp->flags &= ~_IOEOF;
     27 
     28 	return 0;
     29 }