unix/fiss

lib/libbio/bwrite.c in master
Repositories | Summary | Log | Files | LICENSE

bwrite.c (586B) download


 1#include "lib9.h"
 2
 3#include <bio.h>
 4
 5long Bwrite(Biobuf* bp, void* ap, long count) {
 6	long           c;
 7	unsigned char* p;
 8	int            i, n, oc;
 9
10	p  = ap;
11	c  = count;
12	oc = bp->ocount;
13
14	while (c > 0) {
15		n = -oc;
16		if (n > c)
17			n = c;
18		if (n == 0) {
19			if (bp->state != Bwactive)
20				return Beof;
21			i = write(bp->fid, bp->bbuf, bp->bsize);
22			if (i != bp->bsize) {
23				bp->state = Binactive;
24				return Beof;
25			}
26			bp->offset += i;
27			oc = -bp->bsize;
28			continue;
29		}
30		memmove(bp->ebuf + oc, p, n);
31		oc += n;
32		c -= n;
33		p += n;
34	}
35	bp->ocount = oc;
36	return count - c;
37}