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