1 /*
2 * pANS stdio -- fflush
3 */
4 #include "iolib.h"
5 /*
6 * pANS stdio -- data (put here, since loader won't load a separate file)
7 */
8 FILE _IO_stream[]={
9 { .fd 0, .state OPEN, },
10 { .fd 1, .state OPEN, },
11 { .fd 2, .state OPEN, },
12 };
13
_fflush(FILE * f)14 int _fflush(FILE *f){
15 int error, cnt;
16
17 if(f==NULL){
18 error=0;
19 for(f=_IO_stream;f!=&_IO_stream[FOPEN_MAX];f++)
20 if(f->state==WR && _fflush(f)==EOF)
21 error=EOF;
22 return error;
23 }
24 if(f->flags&STRING) return EOF;
25 switch(f->state){
26 default: /* OPEN RDWR EOF RD */
27 return 0;
28 case CLOSED:
29 case ERR:
30 return EOF;
31 case WR:
32 cnt=(f->flags&LINEBUF?f->lp:f->wp)-f->buf;
33 if(cnt && write(f->fd, f->buf, cnt)!=cnt){
34 f->state=ERR;
35 return EOF;
36 }
37 f->rp=f->wp=f->buf;
38 f->state=RDWR;
39 return 0;
40 }
41 }
42
fflush(FILE * f)43 int fflush(FILE *f)
44 {
45 int r;
46
47 qlock(&_stdiolk);
48 r = _fflush(f);
49 qunlock(&_stdiolk);
50 return r;
51 }
52