]> git.lizzy.rs Git - plan9front.git/blob - sys/man/2/bio
merge
[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 or
160 .IR Bfdopen ,
161 the buffer is
162 .I freed
163 and the file is closed.
164 .PP
165 .I Brdline
166 reads a string from the file associated with
167 .I bp
168 up to and including the first
169 .I delim
170 character.
171 The delimiter character at the end of the line is
172 not altered, thus the returned string probably won't be NUL-terminated.
173 .I Brdline
174 returns a pointer to the start of the line or
175 .L 0
176 on end-of-file or read error.
177 .I Blinelen
178 returns the length (including the delimiter)
179 of the most recent string returned by
180 .IR Brdline .
181 .PP
182 .I Brdstr
183 returns a
184 .IR malloc (2)-allocated
185 buffer containing the next line of input delimited by
186 .IR delim ,
187 terminated by a NUL (0) byte.
188 Unlike
189 .IR Brdline ,
190 which returns when its buffer is full even if no delimiter has been found,
191 .I Brdstr
192 will return an arbitrarily long line in a single call.
193 If
194 .I nulldelim
195 is set, the terminal delimiter will be overwritten with a NUL.
196 After a successful call to
197 .IR Brdstr ,
198 the return value of
199 .I Blinelen
200 will be the length of the returned buffer, excluding the NUL.
201 .PP
202 .I Bgetc
203 returns the next character from
204 .IR bp ,
205 or a negative value
206 at end of file.
207 .I Bungetc
208 may be called immediately after
209 .I Bgetc
210 to allow the same character to be reread.
211 .PP
212 .I Bgetrune
213 calls
214 .I Bgetc
215 to read the bytes of the next
216 .SM UTF
217 sequence in the input stream and returns the value of the rune
218 represented by the sequence.
219 It returns a negative value
220 at end of file.
221 .I Bungetrune
222 may be called immediately after
223 .I Bgetrune
224 to allow the same
225 .SM UTF
226 sequence to be reread as either bytes or a rune.
227 .I Bungetc
228 and
229 .I Bungetrune
230 may back up a maximum of five bytes.
231 .PP
232 .I Bgetd
233 uses
234 .I charstod
235 (see
236 .IR atof (2))
237 and
238 .I Bgetc
239 to read the formatted
240 floating-point number in the input stream,
241 skipping initial blanks and tabs.
242 The value is stored in
243 .BR *d.
244 .PP
245 .I Bread
246 reads
247 .I nbytes
248 of data from
249 .I bp
250 into memory starting at
251 .IR addr .
252 The number of bytes read is returned on success
253 and a negative value is returned if a read error occurred.
254 .PP
255 .I Bseek
256 applies
257 .IR seek (2)
258 to
259 .IR bp .
260 It returns the new file offset.
261 .I Boffset
262 returns the file offset of the next character to be processed.
263 .PP
264 .I Bputc
265 outputs the low order 8 bits of
266 .I c
267 on
268 .IR bp .
269 If this causes a
270 .IR write
271 to occur and there is an error,
272 a negative value is returned.
273 Otherwise, a zero is returned.
274 .PP
275 .I Bputrune
276 calls
277 .I Bputc
278 to output the low order
279 16 bits of
280 .I c
281 as a rune
282 in
283 .SM UTF
284 format
285 on the output stream.
286 .PP
287 .I Bprint
288 is a buffered interface to
289 .IR print (2).
290 If this causes a
291 .IR write
292 to occur and there is an error,
293 a negative value
294 .RB ( Beof )
295 is returned.
296 Otherwise, 
297 .I Bprint
298 returns the number of bytes written.
299 .I Bvprint
300 does the same except it takes as argument a
301 .B va_list
302 parameter, so it can be called within a variadic function.
303 .PP
304 .I Bwrite
305 outputs
306 .I nbytes
307 of data starting at
308 .I addr
309 to
310 .IR bp .
311 If this causes a
312 .IR write
313 to occur and there is an error,
314 a negative value is returned.
315 Otherwise, the number of bytes written is returned.
316 .PP
317 .I Bflush
318 causes any buffered output associated with
319 .I bp
320 to be written.
321 The return is as for
322 .IR Bputc .
323 .I Bflush
324 is called on
325 exit for every buffer still open
326 for writing.
327 .PP
328 .I Bbuffered
329 returns the number of bytes in the buffer.
330 When reading, this is the number of bytes still available from the last
331 read on the file; when writing, it is the number of bytes ready to be
332 written.
333 .PP
334 .I Blethal
335 arranges
336 .I errorf
337 to be called in case of an error happening on read/write.
338 An argument of
339 .B nil
340 will have the program terminated in case of error.
341 .SH SOURCE
342 .B /sys/src/libbio
343 .SH SEE ALSO
344 .IR open (2),
345 .IR print (2),
346 .IR exits (2),
347 .IR utf (6),
348 .SH DIAGNOSTICS
349 .I Bio
350 routines that return integers yield
351 .B Beof
352 if 
353 .I bp
354 is not the descriptor of an open file.
355 .I Bopen
356 returns zero if the file cannot be opened in the given mode.
357 All routines set
358 .I errstr
359 on error.
360 .PP
361 An error during read or write will call an error handler specified by
362 .IR Blethal ,
363 if any.
364 .SH BUGS
365 .I Brdline
366 returns an error on strings longer than the buffer associated
367 with the file
368 and also if the end-of-file is encountered
369 before a delimiter.
370 .I Blinelen
371 will tell how many characters are available
372 in these cases.
373 In the case of a true end-of-file,
374 .I Blinelen
375 will return zero.
376 At the cost of allocating a buffer,
377 .I Brdstr
378 sidesteps these issues.
379 .PP
380 Only the low byte of
381 .IR Brdstr 's
382 .I delim
383 is examined, so
384 .I delim
385 cannot be an arbitrary rune.
386 .PP
387 The data returned by
388 .I Brdline
389 may be overwritten by calls to any other
390 .I bio
391 routine on the same
392 .IR bp.