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