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