#include #include #include #define nelem(x) (sizeof(x)/sizeof(x[0])) static Biobufhdr* wbufs[20]; static int atexitflag; static void batexit(void) { Biobufhdr *bp; int i; for(i=0; istate = Bractive; bp->ocount = 0; break; case OWRITE: install(bp); bp->state = Bwactive; bp->ocount = -size; break; } bp->bbuf = p; bp->ebuf = p+size; bp->bsize = size; bp->icount = 0; bp->fid = f; bp->flag = 0; bp->rdline = 0; bp->offset = 0; bp->runesize = 0; return 0; } int Binit(Biobuf *bp, int f, int mode) { return Binits(bp, f, mode, bp->b, sizeof(bp->b)); } Biobuf* Bopen(char *name, int mode) { Biobuf *bp; int f; switch(mode) { default: fprint(2, "Bopen: unknown mode %d\n", mode); return 0; case OREAD: f = open(name, OREAD); if(f < 0) return 0; break; case OWRITE: f = create(name, OWRITE, 0666); if(f < 0) return 0; } bp = malloc(sizeof(Biobuf)); Binits(bp, f, mode, bp->b, sizeof(bp->b)); bp->flag = Bmagic; return bp; } int Bclose(Biobufhdr *bp) { deinstall(bp); Bflush(bp); if(bp->flag == Bmagic) { bp->flag = 0; close(bp->fid); free(bp); } return 0; }