xref: /plan9/sys/src/ape/lib/ap/stdio/fflush.c (revision 3e12c5d1bb89fc02707907988834ef147769ddaf)
1 /*
2  * pANS stdio -- fflush
3  */
4 #include "iolib.h"
fflush(FILE * f)5 int fflush(FILE *f){
6 	int error, cnt;
7 	if(f==NULL){
8 		error=0;
9 		for(f=_IO_stream;f!=&_IO_stream[FOPEN_MAX];f++)
10 			if(f->state==WR && fflush(f)==EOF)
11 				error=EOF;
12 		return error;
13 	}
14 	if(f->flags&STRING) return EOF;
15 	switch(f->state){
16 	default:	/* OPEN RDWR EOF RD */
17 		return 0;
18 	case CLOSED:
19 	case ERR:
20 		return EOF;
21 	case WR:
22 		cnt=(f->flags&LINEBUF?f->lp:f->wp)-f->buf;
23 		if(cnt && write(f->fd, f->buf, cnt)!=cnt){
24 			f->state=ERR;
25 			return EOF;
26 		}
27 		f->rp=f->wp=f->buf;
28 		f->state=RDWR;
29 		return 0;
30 	}
31 }
32