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