361 lines · 6.6 KB · Text
RawDownload
.TH BIO 2.SH NAMEBopen, 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.SH SYNOPSIS.ta \w'Biobuf* 'u.B #include <u.h>.br.B #include <libc.h>.br.B #include <bio.h>.PP.BBiobuf* Bopen(char *file, int mode).PP.Bint	Binit(Biobuf *bp, int fd, int mode).PP.Bint	Binits(Biobufhdr *bp, int fd, int mode, uchar *buf, int size).PP.Bint	Bterm(Biobufhdr *bp).PP.Bint	Bprint(Biobufhdr *bp, char *format, ...).PP.Bint	Bvprint(Biobufhdr *bp, char *format, va_list arglist);.PP.Bvoid*	Brdline(Biobufhdr *bp, int delim).PP.Bchar*	Brdstr(Biobufhdr *bp, int delim, int nulldelim).PP.Bint	Blinelen(Biobufhdr *bp).PP.Bvlong	Boffset(Biobufhdr *bp).PP.Bint	Bfildes(Biobufhdr *bp).PP.Bint	Bgetc(Biobufhdr *bp).PP.Blong	Bgetrune(Biobufhdr *bp).PP.Bint	Bgetd(Biobufhdr *bp, double *d).PP.Bint	Bungetc(Biobufhdr *bp).PP.Bint	Bungetrune(Biobufhdr *bp).PP.Bvlong	Bseek(Biobufhdr *bp, vlong n, int type).PP.Bint	Bputc(Biobufhdr *bp, int c).PP.Bint	Bputrune(Biobufhdr *bp, long c).PP.Blong	Bread(Biobufhdr *bp, void *addr, long nbytes).PP.Blong	Bwrite(Biobufhdr *bp, void *addr, long nbytes).PP.Bint	Bflush(Biobufhdr *bp).PP.Bint	Bbuffered(Biobufhdr *bp).PP.SH DESCRIPTIONThese routines implement fast buffered I/O.I/O on different file descriptors is independent..PP.I Bopenopens.I filefor mode.B OREADor creates for mode.BR OWRITE .It calls.IR malloc (2)to allocate a buffer..PP.I Binitinitializes a standard size buffer, type.IR Biobuf ,with the open file descriptor passed inby the user..I Binitsinitializes a non-standard size buffer, type.IR Biobufhdr ,with the open file descriptor,buffer area, and buffer size passed inby the user..I Biobufand.I Biobufhdrare related by the declaration:.IP.EXtypedef struct Biobuf Biobuf;struct Biobuf{	Biobufhdr;	uchar b[Bungetsize+Bsize];};.EE.PPArgumentsof types pointer to Biobuf and pointer to Biobufhdrcan be used interchangeably in the following routines..PP.IR Bopen ,.IR Binit ,or.I Binitsshould be called before any of theother routines on that buffer..I Bfildesreturns the integer file descriptor of the associated open file..PP.I Btermflushes the buffer for.IR bpand returns.IR Bflush 'sreturn value.If the buffer was allocated by.IR Bopen ,the buffer is.I freedand the file is closed..PP.I Brdlinereads a string from the file associated with.I bpup to and including the first.I delimcharacter.The delimiter character at the end of the line isnot altered, thus the returned string probably won't be NUL-terminated..I Brdlinereturns a pointer to the start of the line or.L 0on end-of-file or read error..I Blinelenreturns the length (including the delimiter)of the most recent string returned by.IR Brdline ..PP.I Brdstrreturns a.IR malloc (2)-allocatedbuffer containing the next line of input delimited by.IR delim ,terminated by a NUL (0) byte.Unlike.IR Brdline ,which returns when its buffer is full even if no delimiter has been found,.I Brdstrwill return an arbitrarily long line in a single call.If.I nulldelimis set, the terminal delimiter will be overwritten with a NUL.After a successful call to.IR Brdstr ,the return value of.I Blinelenwill be the length of the returned buffer, excluding the NUL..PP.I Bgetcreturns the next character from.IR bp ,or a negative valueat end of file..I Bungetcmay be called immediately after.I Bgetcto allow the same character to be reread..PP.I Bgetrunecalls.I Bgetcto read the bytes of the next.SM UTFsequence in the input stream and returns the value of the runerepresented by the sequence.It returns a negative valueat end of file..I Bungetrunemay be called immediately after.I Bgetruneto allow the same.SM UTFsequence to be reread as either bytes or a rune..I Bungetcand.I Bungetrunemay back up a maximum of five bytes..PP.I Bgetduses.I charstod(see.IR atof (2))and.I Bgetcto read the formattedfloating-point number in the input stream,skipping initial blanks and tabs.The value is stored in.BR *d..PP.I Breadreads.I nbytesof data from.I bpinto memory starting at.IR addr .The number of bytes read is returned on successand a negative value is returned if a read error occurred..PP.I Bseekapplies.IR seek (2)to.IR bp .It returns the new file offset..I Boffsetreturns the file offset of the next character to be processed..PP.I Bputcoutputs the low order 8 bits of.I con.IR bp .If this causes a.IR writeto occur and there is an error,a negative value is returned.Otherwise, a zero is returned..PP.I Bputrunecalls.I Bputcto output the low order16 bits of.I cas a runein.SM UTFformaton the output stream..PP.I Bprintis a buffered interface to.IR print (2).If this causes a.IR writeto occur and there is an error,a negative value.RB ( Beof )is returned.Otherwise, .I Bprintreturns the number of bytes written..I Bvprintdoes the same except it takes as argument a.B va_listparameter, so it can be called within a variadic function..PP.I Bwriteoutputs.I nbytesof data starting at.I addrto.IR bp .If this causes a.IR writeto occur and there is an error,a negative value is returned.Otherwise, the number of bytes written is returned..PP.I Bflushcauses any buffered output associated with.I bpto be written.The return is as for.IR Bputc ..I Bflushis called onexit for every buffer still openfor writing..PP.I Bbufferedreturns the number of bytes in the buffer.When reading, this is the number of bytes still available from the lastread on the file; when writing, it is the number of bytes ready to bewritten..SH SOURCE.B /sys/src/libbio.SH SEE ALSO.IR open (2),.IR print (2),.IR exits (2),.IR utf (6),.SH DIAGNOSTICS.I Bioroutines that return integers yield.B Beofif .I bpis not the descriptor of an open file..I Bopenreturns zero if the file cannot be opened in the given mode.All routines set.I errstron error..SH BUGS.I Brdlinereturns an error on strings longer than the buffer associatedwith the fileand also if the end-of-file is encounteredbefore a delimiter..I Blinelenwill tell how many characters are availablein these cases.In the case of a true end-of-file,.I Blinelenwill return zero.At the cost of allocating a buffer,.I Brdstrsidesteps these issues..PPOnly the low byte of.IR Brdstr 's.I delimis examined, so.I delimcannot be an arbitrary rune..PPThe data returned by.I Brdlinemay be overwritten by calls to any other.I bioroutine on the same.IR bp.