bio 6.8 KB

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