9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

bwrite.c (553B)


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