xref: /plan9/sys/src/ape/lib/ap/stdio/_IO_getc.c (revision d9306527b4a7229dcf0cf3c58aed36bb9da82854)
1 /*
2  * pANS stdio -- _IO_getc
3  */
4 #include "iolib.h"
_IO_getc(FILE * f)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