xref: /inferno-os/libbio/bwrite.c (revision 79660b141275316d358c0523ba43eef4f25fa89c)
1 #include	"lib9.h"
2 #include	<bio.h>
3 
4 long
5 Bwrite(Biobuf *bp, void *ap, long count)
6 {
7 	long c;
8 	uchar *p;
9 	int i, n, oc;
10 	char errbuf[ERRMAX];
11 
12 	p = ap;
13 	c = count;
14 	oc = bp->ocount;
15 
16 	while(c > 0) {
17 		n = -oc;
18 		if(n > c)
19 			n = c;
20 		if(n == 0) {
21 			if(bp->state != Bwactive)
22 				return Beof;
23 			i = write(bp->fid, bp->bbuf, bp->bsize);
24 			if(i != bp->bsize) {
25 				errstr(errbuf, sizeof errbuf);
26 				if(strstr(errbuf, "interrupt") == nil)
27 					bp->state = Binactive;
28 				errstr(errbuf, sizeof errbuf);
29 				return Beof;
30 			}
31 			bp->offset += i;
32 			oc = -bp->bsize;
33 			continue;
34 		}
35 		memmove(bp->ebuf+oc, p, n);
36 		oc += n;
37 		c -= n;
38 		p += n;
39 	}
40 	bp->ocount = oc;
41 	return count-c;
42 }
43