Blob


1 #include "lib9.h"
2 #include <bio.h>
4 off_t
5 Bseek(Biobuf *bp, off_t offset, int base)
6 {
7 off_t n, d;
9 switch(bp->state) {
10 default:
11 fprint(2, "Bseek: unknown state %d\n", bp->state);
12 return Beof;
14 case Bracteof:
15 bp->state = Bractive;
16 bp->icount = 0;
17 bp->gbuf = bp->ebuf;
19 case Bractive:
20 n = offset;
21 if(base == 1) {
22 n += Boffset(bp);
23 base = 0;
24 }
26 /*
27 * try to seek within buffer
28 */
29 if(base == 0) {
30 d = n - Boffset(bp);
31 bp->icount += d;
32 if(d >= 0) {
33 if(bp->icount <= 0)
34 return n;
35 } else {
36 if(bp->ebuf - bp->gbuf >= -bp->icount)
37 return n;
38 }
39 }
41 /*
42 * reset the buffer
43 */
44 n = lseek(bp->fid, n, base);
45 bp->icount = 0;
46 bp->gbuf = bp->ebuf;
47 break;
49 case Bwactive:
50 Bflush(bp);
51 n = lseek(bp->fid, offset, base);
52 break;
53 }
54 bp->offset = n;
55 return n;
56 }