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