xref: /plan9/sys/src/libstdio/_IO_getc.c (revision 3e12c5d1bb89fc02707907988834ef147769ddaf)
1 /*
2  * pANS stdio -- _IO_getc
3  */
4 #include "iolib.h"
_IO_getc(FILE * f)5 int _IO_getc(FILE *f){
6 	int cnt;
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 		cnt=read(f->fd, f->buf, f->buf==f->unbuf?1:f->bufl);
16 		switch(cnt){
17 		case -1: f->state=ERR; return EOF;
18 		case 0: f->state=END; return EOF;
19 		default:
20 			f->state=RD;
21 			f->rp=f->buf;
22 			f->wp=f->buf+cnt;
23 			return (*f->rp++)&_IO_CHMASK;
24 		}
25 	}
26 }
27