github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/src/libbio/binit.c (about) 1 /* 2 http://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 7 Permission is hereby granted, free of charge, to any person obtaining a copy 8 of this software and associated documentation files (the "Software"), to deal 9 in the Software without restriction, including without limitation the rights 10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 copies of the Software, and to permit persons to whom the Software is 12 furnished to do so, subject to the following conditions: 13 14 The above copyright notice and this permission notice shall be included in 15 all copies or substantial portions of the Software. 16 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 THE SOFTWARE. 24 */ 25 26 #include <u.h> 27 #include <libc.h> 28 #include <bio.h> 29 30 enum 31 { 32 MAXBUFS = 20 33 }; 34 35 static Biobuf* wbufs[MAXBUFS]; 36 static int atexitflag; 37 38 static 39 void 40 batexit(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 54 static 55 void 56 deinstall(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 65 static 66 void 67 install(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 83 int 84 Binits(Biobuf *bp, int f, int mode, unsigned char *p, int size) 85 { 86 87 p += Bungetsize; /* make room for Bungets */ 88 size -= Bungetsize; 89 90 switch(mode&~(ORCLOSE|OTRUNC)) { 91 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 120 int 121 Binit(Biobuf *bp, int f, int mode) 122 { 123 return Binits(bp, f, mode, bp->b, sizeof(bp->b)); 124 } 125 126 Biobuf* 127 Bfdopen(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 139 Biobuf* 140 Bopen(char *name, int mode) 141 { 142 Biobuf *bp; 143 int f; 144 145 switch(mode&~(ORCLOSE|OTRUNC)) { 146 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: 157 f = create(name, OWRITE|OTRUNC, 0666); 158 if(f < 0) 159 return 0; 160 } 161 bp = Bfdopen(f, mode); 162 if(bp == 0) 163 close(f); 164 return bp; 165 } 166 167 int 168 Bterm(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 }