xref: /inferno-os/libbio/bflush.c (revision 7ef44d652ae9e5e1f5b3465d73684e4a54de73c0)
1 #include	"lib9.h"
2 #include	<bio.h>
3 
4 int
5 Bflush(Biobuf *bp)
6 {
7 	int n, c;
8 
9 	switch(bp->state) {
10 	case Bwactive:
11 		n = bp->bsize+bp->ocount;
12 		if(n == 0)
13 			return 0;
14 		c = write(bp->fid, bp->bbuf, n);
15 		if(n == c) {
16 			bp->offset += n;
17 			bp->ocount = -bp->bsize;
18 			return 0;
19 		}
20 		bp->state = Binactive;
21 		bp->ocount = 0;
22 		break;
23 
24 	case Bracteof:
25 		bp->state = Bractive;
26 
27 	case Bractive:
28 		bp->icount = 0;
29 		bp->gbuf = bp->ebuf;
30 		return 0;
31 	}
32 	return Beof;
33 }
34