bread.c (720B) download
1#include "lib9.h"
2
3#include <bio.h>
4
5long Bread(Biobuf* bp, void* ap, long count) {
6 long c;
7 unsigned char* p;
8 int i, n, ic;
9
10 p = ap;
11 c = count;
12 ic = bp->icount;
13
14 while (c > 0) {
15 n = -ic;
16 if (n > c)
17 n = c;
18 if (n == 0) {
19 if (bp->state != Bractive)
20 break;
21 i = read(bp->fid, bp->bbuf, bp->bsize);
22 if (i <= 0) {
23 bp->state = Bracteof;
24 if (i < 0)
25 bp->state = Binactive;
26 break;
27 }
28 bp->gbuf = bp->bbuf;
29 bp->offset += i;
30 if (i < bp->bsize) {
31 memmove(bp->ebuf - i, bp->bbuf, i);
32 bp->gbuf = bp->ebuf - i;
33 }
34 ic = -i;
35 continue;
36 }
37 memmove(p, bp->ebuf + ic, n);
38 c -= n;
39 ic += n;
40 p += n;
41 }
42 bp->icount = ic;
43 return count - c;
44}