diff options
author | Ian Moffett <ian@osmora.org> | 2025-10-17 19:57:22 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-10-17 20:03:30 -0400 |
commit | 9dcf1391fe8cf7e09c2e9bb66f7f6c094e6268f5 (patch) | |
tree | d28f76bcdb027f9b783d1e3bd8c622cbdf218c2b /src/sys/os | |
parent | a6ccf59fc56f90c97a82fe851f758decaeea9aef (diff) |
kern: filedesc: Implement lseek() function
The lseek() function allows the call to resposition the file offset
using a specific whence value to govern offsetting semantics
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src/sys/os')
-rw-r--r-- | src/sys/os/os_filedes.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/sys/os/os_filedes.c b/src/sys/os/os_filedes.c index ca56fc3..3cbbc6f 100644 --- a/src/sys/os/os_filedes.c +++ b/src/sys/os/os_filedes.c @@ -359,6 +359,44 @@ read(int fd, void *buf, size_t count) return retval; } +off_t +lseek(int fd, off_t offset, int whence) +{ + struct filedesc *fdp; + struct proc *self = proc_self(); + struct vattr attr; + int error; + + if (fd < 0) { + return -EBADF; + } + + fdp = fd_get(self, fd); + if (fdp == NULL) { + return -EBADF; + } + + /* Could not get vnode attributes */ + error = vop_getattr(fdp->vp, &attr); + if (error < 0) { + return 0; + } + + switch (whence) { + case SEEK_SET: + fdp->off = offset; + break; + case SEEK_CUR: + fdp->off += offset; + break; + case SEEK_END: + fdp->off = attr.size; + break; + } + + return fdp->off; +} + /* * ARG0: Path * ARG1: Mode |