xref: /plan9-contrib/sys/src/ape/lib/ap/stdio/_IO_getc.c (revision 781103c4074deb8af160e8a0da2742ba6b29dc2b)
1 /*
2  * pANS stdio -- _IO_getc
3  */
4 #include "iolib.h"
5 int _IO_getc(FILE *f){
6 	int cnt, n;
7 	switch(f->state){
8 	default:	/* CLOSED, WR, ERR, EOF */
9 		return EOF;
10 	case OPEN:
11 		_IO_setvbuf(f);
12 	case RDWR:
13 	case RD:
14 		if(f->flags&STRING) return EOF;
15 		if(f->buf == f->unbuf)
16 			n = 1;
17 		else
18 			n = f->bufl;
19 		cnt=read(f->fd, f->buf, n);
20 		switch(cnt){
21 		case -1: f->state=ERR; return EOF;
22 		case 0: f->state=END; return EOF;
23 		default:
24 			f->state=RD;
25 			f->rp=f->buf;
26 			f->wp=f->buf+cnt;
27 			return (*f->rp++)&_IO_CHMASK;
28 		}
29 	}
30 }
31