scc

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

ftell.c (426B)


      1 #include <stdio.h>
      2 
      3 #include "../syscall.h"
      4 
      5 #undef ftell
      6 
      7 long
      8 ftell(FILE *fp)
      9 {
     10 	long off;
     11 	unsigned char *p;
     12 
     13 	if (fp->flags & _IOERR)
     14 		return EOF;
     15 
     16 	if ((off = _lseek(fp->fd, 0, SEEK_CUR)) < 0) {
     17 		fp->flags |= _IOERR;
     18 		return EOF;
     19 	}
     20 
     21 	if (fp->flags & _IOREAD)
     22 		return off - (fp->wp - fp->rp);
     23 
     24 	if (fp->flags & _IOWRITE) {
     25 		p = (fp->flags & _IOLBF) ? fp->lp : fp->wp;
     26 		return off + (p - fp->buf);
     27 	}
     28 	return off;
     29 }