diff options
Diffstat (limited to 'src/sys/os/os_filedes.c')
-rw-r--r-- | src/sys/os/os_filedes.c | 47 |
1 files changed, 44 insertions, 3 deletions
diff --git a/src/sys/os/os_filedes.c b/src/sys/os/os_filedes.c index 9ba0c63..95452af 100644 --- a/src/sys/os/os_filedes.c +++ b/src/sys/os/os_filedes.c @@ -197,6 +197,32 @@ fd_open(const char *path, mode_t mode) return fd->fdno; } +int +fd_close(int fd) +{ + struct filedesc *fdp; + struct proc *proc = proc_self(); + struct vnode *vp; + + if (fd < 0) { + return -EBADF; + } + + if (proc == NULL) { + return -EIO; + } + + if ((fdp = fd_get(proc, fd)) == NULL) { + return -EIO; + } + + vp = fdp->vp; + proc->fdtab[fdp->fdno] = NULL; + + kfree(fdp); + return vop_reclaim(vp, 0); +} + /* * Initialize file descriptor table */ @@ -227,8 +253,9 @@ write(int fd, const void *buf, size_t count) { struct proc *self = proc_self(); struct filedesc *fdp; - int error; + ssize_t retval; char kbuf[1024]; + int error; if (self == NULL) { return -ESRCH; @@ -267,7 +294,13 @@ write(int fd, const void *buf, size_t count) if (fdp->vp == NULL) { return -EIO; } - return vop_write(fdp->vp, kbuf, count); + retval = vop_write(fdp->vp, kbuf, fdp->off, count); + if (retval <= 0) { + return retval; + } + + /* Move away from where we wrote */ + fdp->off += count; } return count; @@ -278,6 +311,7 @@ read(int fd, void *buf, size_t count) { struct proc *self = proc_self(); struct filedesc *fdp; + ssize_t retval; int error; if (buf == NULL) { @@ -304,7 +338,14 @@ read(int fd, void *buf, size_t count) return -EIO; } - return vop_read(fdp->vp, buf, count); + /* Read the file */ + retval = vop_read(fdp->vp, buf, fdp->off, count); + if (retval <= 0) { + return retval; + } + + fdp->off += count; + return retval; } /* |