diff options
author | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
---|---|---|
committer | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
commit | e5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch) | |
tree | d8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/libbio/bread.c |
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/libbio/bread.c')
-rwxr-xr-x | sys/src/libbio/bread.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/sys/src/libbio/bread.c b/sys/src/libbio/bread.c new file mode 100755 index 000000000..9780ffac1 --- /dev/null +++ b/sys/src/libbio/bread.c @@ -0,0 +1,48 @@ +#include <u.h> +#include <libc.h> +#include <bio.h> + +long +Bread(Biobufhdr *bp, void *ap, long count) +{ + long c; + uchar *p; + int i, n, ic; + + p = ap; + c = count; + ic = bp->icount; + + while(c > 0) { + n = -ic; + if(n > c) + n = c; + if(n == 0) { + if(bp->state != Bractive) + break; + i = read(bp->fid, bp->bbuf, bp->bsize); + if(i <= 0) { + bp->state = Bracteof; + if(i < 0) + bp->state = Binactive; + break; + } + bp->gbuf = bp->bbuf; + bp->offset += i; + if(i < bp->bsize) { + memmove(bp->ebuf-i, bp->bbuf, i); + bp->gbuf = bp->ebuf-i; + } + ic = -i; + continue; + } + memmove(p, bp->ebuf+ic, n); + c -= n; + ic += n; + p += n; + } + bp->icount = ic; + if(count == c && bp->state == Binactive) + return -1; + return count-c; +} |