Blob


1 .TH BIO 3
2 .SH NAME
3 Bopen, Binit, Binits, Brdline, Brdstr, Bgetc, Bgetd, Bungetc, Bread, Bseek, Boffset, Bfildes, Blinelen, Bputc, Bprint, Bvprint, Bwrite, Bflush, Bterm, Bbuffered \- buffered input/output
4 .SH SYNOPSIS
5 .ta \w'Biobuf* 'u
6 .B #include <fmt.h>
7 .B #include <bio.h>
8 .PP
9 .B
10 Biobuf* Bopen(char *file, int mode)
11 .PP
12 .B
13 int Binit(Biobuf *bp, int fd, int mode)
14 .PP
15 .B
16 int Bterm(Biobuf *bp)
17 .PP
18 .B
19 int Bprint(Biobuf *bp, char *format, ...)
20 .PP
21 .B
22 int Bvprint(Biobuf *bp, char *format, va_list arglist);
23 .PP
24 .B
25 void* Brdline(Biobuf *bp, int delim)
26 .PP
27 .B
28 char* Brdstr(Biobuf *bp, int delim, int nulldelim)
29 .PP
30 .B
31 int Blinelen(Biobuf *bp)
32 .PP
33 .B
34 off_t Boffset(Biobuf *bp)
35 .PP
36 .B
37 int Bfildes(Biobuf *bp)
38 .PP
39 .B
40 int Bgetc(Biobuf *bp)
41 .PP
42 .B
43 long Bgetrune(Biobufhdr *bp)
44 .PP
45 .B
46 int Bgetd(Biobuf *bp, double *d)
47 .PP
48 .B
49 int Bungetc(Biobuf *bp)
50 .PP
51 .B
52 int Bungetrune(Biobufhdr *bp)
53 .PP
54 .B
55 off_t Bseek(Biobuf *bp, off_t n, int type)
56 .PP
57 .B
58 int Bputc(Biobuf *bp, int c)
59 .PP
60 .B
61 int Bputrune(Biobufhdr *bp, long c)
62 .PP
63 .B
64 long Bread(Biobuf *bp, void *addr, long nbytes)
65 .PP
66 .B
67 long Bwrite(Biobuf *bp, void *addr, long nbytes)
68 .PP
69 .B
70 int Bflush(Biobuf *bp)
71 .PP
72 .B
73 int Bbuffered(Biobuf *bp)
74 .PP
75 .SH DESCRIPTION
76 These routines implement fast buffered I/O.
77 I/O on different file descriptors is independent.
78 .PP
79 .I Bopen
80 opens
81 .I file
82 for mode
83 .B O_RDONLY
84 or creates for mode
85 .BR O_WRONLY .
86 It calls
87 .IR malloc (3)
88 to allocate a buffer.
89 .PP
90 .I Binit
91 initializes a buffer
92 with the open file descriptor passed in
93 by the user.
94 .PP
95 Arguments
96 of types pointer to Biobuf and pointer to Biobuf
97 can be used interchangeably in the following routines.
98 .PP
99 .IR Bopen ,
100 .IR Binit ,
101 or
102 .I Binits
103 should be called before any of the
104 other routines on that buffer.
105 .I Bfildes
106 returns the integer file descriptor of the associated open file.
107 .PP
108 .I Bterm
109 flushes the buffer for
110 .IR bp .
111 If the buffer was allocated by
112 .IR Bopen ,
113 the buffer is
114 .I freed
115 and the file is closed.
116 .PP
117 .I Brdline
118 reads a string from the file associated with
119 .I bp
120 up to and including the first
121 .I delim
122 character.
123 The delimiter character at the end of the line is
124 not altered.
125 .I Brdline
126 returns a pointer to the start of the line or
127 .L 0
128 on end-of-file or read error.
129 .I Blinelen
130 returns the length (including the delimiter)
131 of the most recent string returned by
132 .IR Brdline .
133 .PP
134 .I Brdstr
135 returns a
136 .IR malloc (3)-allocated
137 buffer containing the next line of input delimited by
138 .IR delim ,
139 terminated by a NUL (0) byte.
140 Unlike
141 .IR Brdline ,
142 which returns when its buffer is full even if no delimiter has been found,
143 .I Brdstr
144 will return an arbitrarily long line in a single call.
145 If
146 .I nulldelim
147 is set, the terminal delimiter will be overwritten with a NUL.
148 After a successful call to
149 .IR Brdstr ,
150 the return value of
151 .I Blinelen
152 will be the length of the returned buffer, excluding the NUL.
153 .PP
154 .I Bgetc
155 returns the next byte from
156 .IR bp ,
157 or a negative value
158 at end of file.
159 .I Bungetc
160 may be called immediately after
161 .I Bgetc
162 to allow the same byte to be reread.
163 .PP
164 .I Bgetrune
165 calls
166 .I Bgetc
167 to read the bytes of the next
168 .SM UTF
169 sequence in the input stream and returns the value of the rune
170 represented by the sequence.
171 It returns a negative value
172 at end of file.
173 .I Bungetrune
174 may be called immediately after
175 .I Bgetrune
176 to allow the same
177 .SM UTF
178 sequence to be reread as either bytes or a rune.
179 .I Bungetc
180 and
181 .I Bungetrune
182 may back up a maximum of five bytes.
183 .PP
184 .I Bgetd
185 uses
186 .I fmtcharstod
187 (undocumented)
188 and
189 .I Bgetc
190 to read the formatted
191 floating-point number in the input stream,
192 skipping initial blanks and tabs.
193 The value is stored in
194 .BR *d.
195 .PP
196 .I Bread
197 reads
198 .I nbytes
199 of data from
200 .I bp
201 into memory starting at
202 .IR addr .
203 The number of bytes read is returned on success
204 and a negative value is returned if a read error occurred.
205 .PP
206 .I Bseek
207 applies
208 .IR lseek (2)
209 to
210 .IR bp .
211 It returns the new file offset.
212 .I Boffset
213 returns the file offset of the next character to be processed.
214 .PP
215 .I Bputc
216 outputs the low order 8 bits of
217 .I c
218 on
219 .IR bp .
220 If this causes a
221 .IR write
222 to occur and there is an error,
223 a negative value is returned.
224 Otherwise, a zero is returned.
225 .PP
226 .I Bputrune
227 calls
228 .I Bputc
229 to output the low order
230 16 bits of
231 .I c
232 as a rune
233 in
234 .SM UTF
235 format
236 on the output stream.
237 .PP
238 .I Bprint
239 is a buffered interface to
240 .IR print (2).
241 If this causes a
242 .IR write
243 to occur and there is an error,
244 a negative value
245 .RB ( Beof )
246 is returned.
247 Otherwise, the number of bytes output is returned.
248 .I Bvprint
249 does the same except it takes as argument a
250 .B va_list
251 parameter, so it can be called within a variadic function.
252 .PP
253 .I Bwrite
254 outputs
255 .I nbytes
256 of data starting at
257 .I addr
258 to
259 .IR bp .
260 If this causes a
261 .IR write
262 to occur and there is an error,
263 a negative value is returned.
264 Otherwise, the number of bytes written is returned.
265 .PP
266 .I Bflush
267 causes any buffered output associated with
268 .I bp
269 to be written.
270 The return is as for
271 .IR Bputc .
272 .I Bflush
273 is called on
274 exit for every buffer still open
275 for writing.
276 .PP
277 .I Bbuffered
278 returns the number of bytes in the buffer.
279 When reading, this is the number of bytes still available from the last
280 read on the file; when writing, it is the number of bytes ready to be
281 written.
282 .PP
283 This library uses
284 .IR fmt (3)
285 for diagnostic messages about internal errors,
286 as well as for the implementation of
287 .I Bprint
288 and
289 .IR Bvprint .
290 It uses
291 .IR utf (3)
292 for the implementation of
293 .I Bgetrune
294 and
295 .IR Bputrune .
296 .SH SEE ALSO
297 .IR atexit (3).
298 .IR open (2),
299 .IR print (3),
300 .IR utf (7)
301 .SH DIAGNOSTICS
302 .I Bio
303 routines that return integers yield
304 .B Beof
305 if
306 .I bp
307 is not the descriptor of an open file.
308 .I Bopen
309 returns zero if the file cannot be opened in the given mode.
310 .SH HISTORY
311 The
312 .IR bio (3)
313 library originally appeared in Plan 9.
314 This is a port of the Plan 9 bio library.
315 .SH BUGS
316 .I Brdline
317 returns an error on strings longer than the buffer associated
318 with the file
319 and also if the end-of-file is encountered
320 before a delimiter.
321 .I Blinelen
322 will tell how many characters are available
323 in these cases.
324 In the case of a true end-of-file,
325 .I Blinelen
326 will return zero.
327 At the cost of allocating a buffer,
328 .I Brdstr
329 sidesteps these issues.
330 .PP
331 The data returned by
332 .I Brdline
333 may be overwritten by calls to any other
334 .I bio
335 routine on the same
336 .IR bp.