Introduction
Introduction Statistics Contact Development Disclaimer Help
binit.c - 9base - revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log
Files
Refs
README
LICENSE
---
binit.c (1983B)
---
1 #include "lib9.h"
2 #include <bio.h>
3
4 enum
5 {
6 MAXBUFS = 20
7 };
8
9 static Biobuf* wbufs[MAXBUFS];
10 static int atexitflag;
11
12 static
13 void
14 batexit(void)
15 {
16 Biobuf *bp;
17 int i;
18
19 for(i=0; i<MAXBUFS; i++) {
20 bp = wbufs[i];
21 if(bp != 0) {
22 wbufs[i] = 0;
23 Bflush(bp);
24 }
25 }
26 }
27
28 static
29 void
30 deinstall(Biobuf *bp)
31 {
32 int i;
33
34 for(i=0; i<MAXBUFS; i++)
35 if(wbufs[i] == bp)
36 wbufs[i] = 0;
37 }
38
39 static
40 void
41 install(Biobuf *bp)
42 {
43 int i;
44
45 deinstall(bp);
46 for(i=0; i<MAXBUFS; i++)
47 if(wbufs[i] == 0) {
48 wbufs[i] = bp;
49 break;
50 }
51 if(atexitflag == 0) {
52 atexitflag = 1;
53 atexit(batexit);
54 }
55 }
56
57 int
58 Binits(Biobuf *bp, int f, int mode, unsigned char *p, int size)
59 {
60
61 p += Bungetsize; /* make room for Bungets */
62 size -= Bungetsize;
63
64 switch(mode&~(OCEXEC|ORCLOSE|OTRUNC)) {
65 default:
66 fprint(2, "Bopen: unknown mode %d\n", mode);
67 return Beof;
68
69 case OREAD:
70 bp->state = Bractive;
71 bp->ocount = 0;
72 break;
73
74 case OWRITE:
75 install(bp);
76 bp->state = Bwactive;
77 bp->ocount = -size;
78 break;
79 }
80 bp->bbuf = p;
81 bp->ebuf = p+size;
82 bp->bsize = size;
83 bp->icount = 0;
84 bp->gbuf = bp->ebuf;
85 bp->fid = f;
86 bp->flag = 0;
87 bp->rdline = 0;
88 bp->offset = 0;
89 bp->runesize = 0;
90 return 0;
91 }
92
93
94 int
95 Binit(Biobuf *bp, int f, int mode)
96 {
97 return Binits(bp, f, mode, bp->b, sizeof(bp->b));
98 }
99
100 Biobuf*
101 Bfdopen(int f, int mode)
102 {
103 Biobuf *bp;
104
105 bp = malloc(sizeof(Biobuf));
106 if(bp == 0)
107 return 0;
108 Binits(bp, f, mode, bp->b, sizeof(bp->b));
109 bp->flag = Bmagic;
110 return bp;
111 }
112
113 Biobuf*
114 Bopen(char *name, int mode)
115 {
116 Biobuf *bp;
117 int f;
118
119 switch(mode&~(OCEXEC|ORCLOSE|OTRUNC)) {
120 default:
121 fprint(2, "Bopen: unknown mode %d\n", mode);
122 return 0;
123
124 case OREAD:
125 f = open(name, mode);
126 if(f < 0)
127 return 0;
128 break;
129
130 case OWRITE:
131 f = create(name, mode, 0666);
132 if(f < 0)
133 return 0;
134 }
135 bp = Bfdopen(f, mode);
136 if(bp == 0)
137 close(f);
138 return bp;
139 }
140
141 int
142 Bterm(Biobuf *bp)
143 {
144
145 deinstall(bp);
146 Bflush(bp);
147 if(bp->flag == Bmagic) {
148 bp->flag = 0;
149 close(bp->fid);
150 free(bp);
151 }
152 return 0;
153 }
You are viewing proxied material from suckless.org. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.