.TH BIO 2
.SH NAME
-Bopen, 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
+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
.SH SYNOPSIS
.ta \w'Biobuf* 'u
.B #include <u.h>
Biobuf* Bopen(char *file, int mode)
.PP
.B
+Biobuf* Bfdopen(int fd, int mode)
+.PP
+.B
int Binit(Biobuf *bp, int fd, int mode)
.PP
.B
.B
int Bbuffered(Biobufhdr *bp)
.PP
+.B
+void Blethal(Biobufhdr *bp, void (*errorf)(char *))
+.PP
+.B
+void Biofn(Biobufhdr *bp, int (*iof)(Biobufhdr *, void *, long))
+.PP
.SH DESCRIPTION
These routines implement fast buffered I/O.
I/O on different file descriptors is independent.
.IR malloc (2)
to allocate a buffer.
.PP
+.I Bfdopen
+allocates a buffer for the already-open file descriptor
+.I fd
+for mode
+.B OREAD
+or
+.BR OWRITE .
+It calls
+.IR malloc (2)
+to allocate a buffer.
+.PP
.I Binit
initializes a standard size buffer, type
.IR Biobuf ,
.PP
.I Bterm
flushes the buffer for
-.IR bp
+.I bp
and returns
.IR Bflush 's
return value.
If the buffer was allocated by
-.IR Bopen ,
+.I Bopen
+or
+.IR Bfdopen ,
the buffer is
.I freed
and the file is closed.
on
.IR bp .
If this causes a
-.IR write
+.I write
to occur and there is an error,
a negative value is returned.
Otherwise, a zero is returned.
is a buffered interface to
.IR print (2).
If this causes a
-.IR write
+.I write
to occur and there is an error,
a negative value
.RB ( Beof )
to
.IR bp .
If this causes a
-.IR write
+.I write
to occur and there is an error,
a negative value is returned.
Otherwise, the number of bytes written is returned.
When reading, this is the number of bytes still available from the last
read on the file; when writing, it is the number of bytes ready to be
written.
+.PP
+.I Blethal
+arranges
+.I errorf
+to be called in case of an error happening on read/write.
+An argument of
+.B nil
+will have the program terminated in case of error.
+.PP
+If
+.I Biofn
+is called with a non-nil
+.I iof
+function, then that function is called for I/O in lieu of
+.IR read (2)
+and
+.IR write .
+A nil argument for
+.I iof
+restores normal behaviour.
.SH SOURCE
.B /sys/src/libbio
.SH SEE ALSO
.IR open (2),
+.IR read (2),
.IR print (2),
.IR exits (2),
.IR utf (6),
All routines set
.I errstr
on error.
+.PP
+An error during read or write will call an error handler specified by
+.IR Blethal ,
+if any.
.SH BUGS
.I Brdline
returns an error on strings longer than the buffer associated