blob: a7ade5081fe76e9abab24d00fa1963a84ab2a456 [file] [log] [blame]
Rob Pike69b74c32008-06-12 13:26:16 -07001/*
2http://code.google.com/p/inferno-os/source/browse/libbio/binit.c
3
4 Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
5 Revisions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com). All rights reserved.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in
15all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23THE SOFTWARE.
24*/
25
26#include <u.h>
27#include <libc.h>
28#include <bio.h>
29
30enum
31{
32 MAXBUFS = 20
33};
34
35static Biobuf* wbufs[MAXBUFS];
36static int atexitflag;
37
38static
39void
40batexit(void)
41{
42 Biobuf *bp;
43 int i;
44
45 for(i=0; i<MAXBUFS; i++) {
46 bp = wbufs[i];
47 if(bp != 0) {
48 wbufs[i] = 0;
49 Bflush(bp);
50 }
51 }
52}
53
54static
55void
56deinstall(Biobuf *bp)
57{
58 int i;
59
60 for(i=0; i<MAXBUFS; i++)
61 if(wbufs[i] == bp)
62 wbufs[i] = 0;
63}
64
65static
66void
67install(Biobuf *bp)
68{
69 int i;
70
71 deinstall(bp);
72 for(i=0; i<MAXBUFS; i++)
73 if(wbufs[i] == 0) {
74 wbufs[i] = bp;
75 break;
76 }
77 if(atexitflag == 0) {
78 atexitflag = 1;
79 atexit(batexit);
80 }
81}
82
83int
84Binits(Biobuf *bp, int f, int mode, unsigned char *p, int size)
85{
86
87 p += Bungetsize; /* make room for Bungets */
88 size -= Bungetsize;
89
Hector Chucd9d72b2009-11-30 11:53:11 -080090 switch(mode&~(ORCLOSE|OTRUNC)) {
Rob Pike69b74c32008-06-12 13:26:16 -070091 default:
92 fprint(2, "Bopen: unknown mode %d\n", mode);
93 return Beof;
94
95 case OREAD:
96 bp->state = Bractive;
97 bp->ocount = 0;
98 break;
99
100 case OWRITE:
101 install(bp);
102 bp->state = Bwactive;
103 bp->ocount = -size;
104 break;
105 }
106 bp->bbuf = p;
107 bp->ebuf = p+size;
108 bp->bsize = size;
109 bp->icount = 0;
110 bp->gbuf = bp->ebuf;
111 bp->fid = f;
112 bp->flag = 0;
113 bp->rdline = 0;
114 bp->offset = 0;
115 bp->runesize = 0;
116 return 0;
117}
118
119
120int
121Binit(Biobuf *bp, int f, int mode)
122{
123 return Binits(bp, f, mode, bp->b, sizeof(bp->b));
124}
125
126Biobuf*
127Bfdopen(int f, int mode)
128{
129 Biobuf *bp;
130
131 bp = malloc(sizeof(Biobuf));
132 if(bp == 0)
133 return 0;
134 Binits(bp, f, mode, bp->b, sizeof(bp->b));
135 bp->flag = Bmagic;
136 return bp;
137}
138
139Biobuf*
140Bopen(char *name, int mode)
141{
142 Biobuf *bp;
143 int f;
144
Hector Chucd9d72b2009-11-30 11:53:11 -0800145 switch(mode&~(ORCLOSE|OTRUNC)) {
Rob Pike69b74c32008-06-12 13:26:16 -0700146 default:
147 fprint(2, "Bopen: unknown mode %d\n", mode);
148 return 0;
149
150 case OREAD:
151 f = open(name, OREAD);
152 if(f < 0)
153 return 0;
154 break;
155
156 case OWRITE:
Hector Chucd9d72b2009-11-30 11:53:11 -0800157 f = create(name, OWRITE|OTRUNC, 0666);
Rob Pike69b74c32008-06-12 13:26:16 -0700158 if(f < 0)
159 return 0;
160 }
161 bp = Bfdopen(f, mode);
162 if(bp == 0)
163 close(f);
164 return bp;
165}
166
167int
168Bterm(Biobuf *bp)
169{
170
171 deinstall(bp);
172 Bflush(bp);
173 if(bp->flag == Bmagic) {
174 bp->flag = 0;
175 close(bp->fid);
176 free(bp);
177 }
178 return 0;
179}