1
0

bio 6.6 KB

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