BIO(3)
π BIO(3) β Library Functions Manual π
πΈ NAME πΈ
Buffered input/output β the whole squad is: Bopen, Bfdopen, Binit, Binits, Brdline, Brdstr, Bgetc, Bgetrune, Bgetd, Bungetc, Bungetrune, Bread, Bseek, Boffset, Bfildes, Blinelen, Bputc, Bputrune, Bprint, Bvprint, Bwrite, Bflush, Bterm, Bbuffered.
β¨ SYNOPSIS β¨
#include <u.h>
#include <libc.h>
#include <bio.h>
Biobuf* Bopen(char *file, int mode)
Biobuf* Bfdopen(int fd, int mode)
int Binit(Biobuf *bp, int fd, int mode)
int Binits(Biobufhdr *bp, int fd, int mode, uchar *buf, int size)
int Bterm(Biobufhdr *bp)
int Bprint(Biobufhdr *bp, char *format, ...)
int Bvprint(Biobufhdr *bp, char *format, va_list arglist);
void* Brdline(Biobufhdr *bp, int delim)
char* Brdstr(Biobufhdr *bp, int delim, int nulldelim)
int Blinelen(Biobufhdr *bp)
vlong Boffset(Biobufhdr *bp)
int Bfildes(Biobufhdr *bp)
int Bgetc(Biobufhdr *bp)
long Bgetrune(Biobufhdr *bp)
int Bgetd(Biobufhdr *bp, double *d)
int Bungetc(Biobufhdr *bp)
int Bungetrune(Biobufhdr *bp)
vlong Bseek(Biobufhdr *bp, vlong n, int type)
int Bputc(Biobufhdr *bp, int c)
int Bputrune(Biobufhdr *bp, long c)
long Bread(Biobufhdr *bp, void *addr, long nbytes)
long Bwrite(Biobufhdr *bp, void *addr, long nbytes)
int Bflush(Biobufhdr *bp)
int Bbuffered(Biobufhdr *bp)
π¦ DESCRIPTION π¦
Okay so these routines are literally fast buffered I/O, and I/O on different file descriptors is independent. No cap.
Bopenβ opens file for modeOREADor creates it for modeOWRITE. It callsmalloc(3)to allocate a buffer. πBfdopenβ allocates a buffer for the already-open file descriptor fd for modeOREADorOWRITE. It callsmalloc(3)to allocate a buffer. π«ΆBinitβ initializes a standard size buffer, typeBiobuf, with the open file descriptor passed in by the user. πΈBinitsβ initializes a non-standard size buffer, typeBiobufhdr, with the open file descriptor, buffer area, and buffer size passed in by the user. π¦
Biobuf and Biobufhdr are related by the declaration:
typedef struct Biobuf Biobuf;
struct Biobuf
{
Biobufhdr;
uchar b[Bungetsize+Bsize];
};
Arguments of types pointer to Biobuf and pointer to Biobufhdr can be used interchangeably in the following routines.
Bopen, Binit, or Binits should be called before any of the other routines on that buffer.
Bfildesβ returns the integer file descriptor of the associated open file. π₯Btermβ flushes the buffer for bp. If the buffer was allocated byBopen, the buffer is freed and the file is closed. π§Brdlineβ reads a string from the file associated with bp up to and including the first delim character. The delimiter character at the end of the line is not altered. It returns a pointer to the start of the line, or on end-of-file or read error.Blinelenreturns the length (including the delimiter) of the most recent string returned byBrdline. πBrdstrβ returns amalloc(3)-allocated buffer containing the next line of input delimited by delim, terminated by a NUL (0) byte. UnlikeBrdline, which returns when its buffer is full even if no delimiter has been found,Brdstrwill return an arbitrarily long line in a single call. If nulldelim is set, the terminal delimiter will be overwritten with a NUL. After a successful call toBrdstr, the return value ofBlinelenwill be the length of the returned buffer, excluding the NUL. πBgetcβ returns the next character from bp, or a negative value at end of file.Bungetcmay be called immediately afterBgetcto allow the same character to be reread. βBgetruneβ callsBgetcto read the bytes of the next UTF sequence in the input stream and returns the value of the rune represented by the sequence. It returns a negative value at end of file.Bungetrunemay be called immediately afterBgetruneto allow the same UTF sequence to be reread as either bytes or a rune.BungetcandBungetrunemay back up a maximum of five bytes. πBgetdβ usescharstod(seeatof(3)) andBgetcto read the formatted floating-point number in the input stream, skipping initial blanks and tabs. The value is stored in*d. π₯Breadβ reads nbytes of data from bp into memory starting at addr. The number of bytes read is returned on success and a negative value is returned if a read error occurred. π«ΆBseekβ appliesseek(3)to bp. It returns the new file offset.Boffsetreturns the file offset of the next character to be processed. βBputcβ outputs the low order 8 bits of c on bp. If this causes a write to occur and there is an error, a negative value is returned. Otherwise, a zero is returned. πBputruneβ callsBputcto output the low order 16 bits of c as a rune in UTF format on the output stream. π¦Bprintβ is a buffered interface toprint(3). If this causes a write to occur and there is an error, a negative value (Beof) is returned. Otherwise,Bprintreturns the number of bytes written.Bvprintdoes the same except it takes as argument ava_listparameter, so it can be called within a variadic function. πBwriteβ outputs nbytes of data starting at addr to bp. If this causes a write to occur and there is an error, a negative value is returned. Otherwise, the number of bytes written is returned. πBflushβ causes any buffered output associated with bp to be written. The return is as forBputc.Bflushis called on exit for every buffer still open for writing. πBbufferedβ returns the number of bytes in the buffer. 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. π
π SOURCE π
/src/libbio β that's where this lives, bestie.
π¦ SEE ALSO π¦
open(3), print(3), exits(3), utf(7),
π DIAGNOSTICS π
- Bio routines that return integers yield
Beofif bp is not the descriptor of an open file. Bopenreturns zero if the file cannot be opened in the given mode.- All routines set
errstron error.
π BUGS π
Brdlinereturns an error on strings longer than the buffer associated with the file and also if the end-of-file is encountered before a delimiter.Blinelenwill tell how many characters are available in these cases. In the case of a true end-of-file,Blinelenwill return zero. At the cost of allocating a buffer,Brdstrsidesteps these issues. π- The data returned by
Brdlinemay be overwritten by calls to any other bio routine on the same bp. π«
π okay that's the whole BIO(3) page from the Library Functions Manual β footer vibes only, no extra date in sight. Catch you in the next one, bestie π
π¦ where this page comes from
- alpine 3.24: plan9port-doc 0_git20260208-r0 Β· LPL-1.02
- arch rolling: plan9port 20261021-2 Β· custom
- nixos 26.05: plan9port 0-unstable-2025-11-10
- nixos 26.05: plan9port-wayland 0-unstable-2025-11-26
- nixpkgs unstable: plan9port 0-unstable-2025-11-10
- nixpkgs unstable: plan9port-wayland 0-unstable-2025-11-26
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms