9os

Experimental kernel using plan9 ideas for embedded device
git clone git://git.simple-cc.org/9os
Log | Files | Refs | README | LICENSE

__getc.c (620B)


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