wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

binit.c (2003B)


      1 #include	<stdlib.h>
      2 #include	<plan9.h>
      3 #include	<bio.h>
      4 
      5 enum
      6 {
      7 	MAXBUFS	= 20
      8 };
      9 
     10 static	Biobuf*	wbufs[MAXBUFS];
     11 static	int		atexitflag;
     12 
     13 static
     14 void
     15 batexit(void)
     16 {
     17 	Biobuf *bp;
     18 	int i;
     19 
     20 	for(i=0; i<MAXBUFS; i++) {
     21 		bp = wbufs[i];
     22 		if(bp != 0) {
     23 			wbufs[i] = 0;
     24 			Bflush(bp);
     25 		}
     26 	}
     27 }
     28 
     29 static
     30 void
     31 deinstall(Biobuf *bp)
     32 {
     33 	int i;
     34 
     35 	for(i=0; i<MAXBUFS; i++)
     36 		if(wbufs[i] == bp)
     37 			wbufs[i] = 0;
     38 }
     39 
     40 static
     41 void
     42 install(Biobuf *bp)
     43 {
     44 	int i;
     45 
     46 	deinstall(bp);
     47 	for(i=0; i<MAXBUFS; i++)
     48 		if(wbufs[i] == 0) {
     49 			wbufs[i] = bp;
     50 			break;
     51 		}
     52 	if(atexitflag == 0) {
     53 		atexitflag = 1;
     54 		atexit(batexit);
     55 	}
     56 }
     57 
     58 int
     59 Binits(Biobuf *bp, int f, int mode, unsigned char *p, int size)
     60 {
     61 
     62 	p += Bungetsize;	/* make room for Bungets */
     63 	size -= Bungetsize;
     64 
     65 	switch(mode&~(OCEXEC|ORCLOSE|OTRUNC)) {
     66 	default:
     67 		fprint(2, "Bopen: unknown mode %d\n", mode);
     68 		return Beof;
     69 
     70 	case OREAD:
     71 		bp->state = Bractive;
     72 		bp->ocount = 0;
     73 		break;
     74 
     75 	case OWRITE:
     76 		install(bp);
     77 		bp->state = Bwactive;
     78 		bp->ocount = -size;
     79 		break;
     80 	}
     81 	bp->bbuf = p;
     82 	bp->ebuf = p+size;
     83 	bp->bsize = size;
     84 	bp->icount = 0;
     85 	bp->gbuf = bp->ebuf;
     86 	bp->fid = f;
     87 	bp->flag = 0;
     88 	bp->rdline = 0;
     89 	bp->offset = 0;
     90 	bp->runesize = 0;
     91 	return 0;
     92 }
     93 
     94 
     95 int
     96 Binit(Biobuf *bp, int f, int mode)
     97 {
     98 	return Binits(bp, f, mode, bp->b, sizeof(bp->b));
     99 }
    100 
    101 Biobuf*
    102 Bfdopen(int f, int mode)
    103 {
    104 	Biobuf *bp;
    105 
    106 	bp = malloc(sizeof(Biobuf));
    107 	if(bp == 0)
    108 		return 0;
    109 	Binits(bp, f, mode, bp->b, sizeof(bp->b));
    110 	bp->flag = Bmagic;
    111 	return bp;
    112 }
    113 
    114 Biobuf*
    115 Bopen(const char *name, int mode)
    116 {
    117 	Biobuf *bp;
    118 	int f;
    119 
    120 	switch(mode&~(OCEXEC|ORCLOSE|OTRUNC)) {
    121 	default:
    122 		fprint(2, "Bopen: unknown mode %d\n", mode);
    123 		return 0;
    124 
    125 	case OREAD:
    126 		f = open(name, mode);
    127 		if(f < 0)
    128 			return 0;
    129 		break;
    130 
    131 	case OWRITE:
    132 		f = creat(name, mode);
    133 		if(f < 0)
    134 			return 0;
    135 	}
    136 	bp = Bfdopen(f, mode);
    137 	if(bp == 0)
    138 		close(f);
    139 	return bp;
    140 }
    141 
    142 int
    143 Bterm(Biobuf *bp)
    144 {
    145 
    146 	deinstall(bp);
    147 	Bflush(bp);
    148 	if(bp->flag == Bmagic) {
    149 		bp->flag = 0;
    150 		close(bp->fid);
    151 		free(bp);
    152 	}
    153 	return 0;
    154 }