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