aboutsummaryrefslogtreecommitdiff
path: root/src/lib9pclient/seek.c
blob: 0785c4b489b91bbe1c9b3fd5f26428a63ca96fdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <9pclient.h>
#include "fsimpl.h"

vlong
fsseek(CFid *fid, vlong n, int whence)
{
	Dir *d;

	switch(whence){
	case 0:
		qlock(&fid->lk);
		fid->offset = n;
		qunlock(&fid->lk);
		break;
	case 1:
		qlock(&fid->lk);
		n += fid->offset;
		if(n < 0){
			qunlock(&fid->lk);
			werrstr("negative offset");
			return -1;
		}
		fid->offset = n;
		qunlock(&fid->lk);
		break;
	case 2:
		if((d = fsdirfstat(fid)) == nil)
			return -1;
		n += d->length;
		if(n < 0){
			werrstr("negative offset");
			return -1;
		}
		qlock(&fid->lk);
		fid->offset = n;
		qunlock(&fid->lk);
		break;
	default:
		werrstr("bad whence in fsseek");
		return -1;
	}
	return n;
}