scc

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

__getc.c (621B)


      1 #include <errno.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 
      5 #include "../libc.h"
      6 #include "../syscall.h"
      7 
      8 int
      9 __getc(FILE *fp)
     10 {
     11 	int cnt;
     12 
     13 	if (fp->flags & (_IOEOF | _IOERR))
     14 		return EOF;
     15 
     16 	if ((fp->flags & (_IOREAD | _IORW)) == 0) {
     17 		fp->flags |= _IOERR;
     18 		errno = EBADF;
     19 		return EOF;
     20 	}
     21 
     22 	if (fp->flags & _IOSTRG) {
     23 		fp->flags |= _IOEOF;
     24 		return EOF;
     25 	}
     26 
     27 	if (fp->buf == NULL && _allocbuf(fp))
     28 		return EOF;
     29 
     30 	if ((cnt = _read(fp->fd, fp->buf, fp->len)) <= 0) {
     31 		fp->flags |= (cnt == 0) ? _IOEOF : _IOERR;
     32 		return EOF;
     33 	}
     34 
     35 	fp->flags |= _IOREAD;
     36 	fp->rp = fp->buf;
     37 	fp->wp = fp->buf + cnt;
     38 
     39 	return *fp->rp++;
     40 }