summaryrefslogtreecommitdiff
path: root/sys/src/libbio/bseek.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/libbio/bseek.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/libbio/bseek.c')
-rwxr-xr-xsys/src/libbio/bseek.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/sys/src/libbio/bseek.c b/sys/src/libbio/bseek.c
new file mode 100755
index 000000000..809b09323
--- /dev/null
+++ b/sys/src/libbio/bseek.c
@@ -0,0 +1,58 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+
+vlong
+Bseek(Biobufhdr *bp, vlong offset, int base)
+{
+ vlong n, d;
+
+ switch(bp->state) {
+ default:
+ fprint(2, "Bseek: unknown state %d\n", bp->state);
+ return Beof;
+
+ case Bracteof:
+ bp->state = Bractive;
+ bp->icount = 0;
+ bp->gbuf = bp->ebuf;
+
+ case Bractive:
+ n = offset;
+ if(base == 1) {
+ n += Boffset(bp);
+ base = 0;
+ }
+
+ /*
+ * try to seek within buffer
+ */
+ if(base == 0) {
+ /*
+ * if d is too large for an int, icount may wrap,
+ * so we need to ensure that icount hasn't wrapped
+ * and points within the buffer's valid data.
+ */
+ d = n - Boffset(bp);
+ bp->icount += d;
+ if(d <= bp->bsize && bp->icount <= 0 &&
+ bp->ebuf - bp->gbuf >= -bp->icount)
+ return n;
+ }
+
+ /*
+ * reset the buffer
+ */
+ n = seek(bp->fid, n, base);
+ bp->icount = 0;
+ bp->gbuf = bp->ebuf;
+ break;
+
+ case Bwactive:
+ Bflush(bp);
+ n = seek(bp->fid, offset, base);
+ break;
+ }
+ bp->offset = n;
+ return n;
+}