summaryrefslogtreecommitdiff
path: root/src/sys
diff options
context:
space:
mode:
Diffstat (limited to 'src/sys')
-rw-r--r--src/sys/include/os/filedesc.h11
-rw-r--r--src/sys/os/os_filedes.c45
2 files changed, 55 insertions, 1 deletions
diff --git a/src/sys/include/os/filedesc.h b/src/sys/include/os/filedesc.h
index 87bf242..c339ed6 100644
--- a/src/sys/include/os/filedesc.h
+++ b/src/sys/include/os/filedesc.h
@@ -60,6 +60,17 @@ struct filedesc {
int fdtab_init(struct proc *procp);
/*
+ * Open a file
+ *
+ * @path: Path to file in which we wish to open
+ * @mode: Mode of our desired file
+ *
+ * Returns the file descriptor on success,
+ * otherwise a less than zero value on failure.
+ */
+int fd_open(const char *path, mode_t mode);
+
+/*
* Write to a file descriptor
*
* @fd: File descriptor to write to
diff --git a/src/sys/os/os_filedes.c b/src/sys/os/os_filedes.c
index e7cc83a..dde7f22 100644
--- a/src/sys/os/os_filedes.c
+++ b/src/sys/os/os_filedes.c
@@ -31,10 +31,12 @@
#include <sys/syslog.h>
#include <sys/errno.h>
#include <sys/limits.h>
+#include <sys/proc.h>
+#include <sys/namei.h>
#include <os/filedesc.h>
#include <os/kalloc.h>
#include <io/cons/cons.h>
-#include <sys/proc.h>
+#include <compat/unix/syscall.h>
#include <string.h>
#define STDOUT_FILENO 1
@@ -86,6 +88,47 @@ fd_alloc(struct proc *procp, struct filedesc **fd_res)
return -EMFILE;
}
+int
+fd_open(const char *path, mode_t mode)
+{
+ struct filedesc *fd;
+ struct nameidata nd;
+ struct proc *self = proc_self();
+ struct vnode *vp;
+ int error;
+
+ /* We need the current proc */
+ if (self == NULL) {
+ return -ESRCH;
+ }
+
+ if (path == NULL) {
+ return -ESRCH;
+ }
+
+ /* Allocate a new file descriptor */
+ error = fd_alloc(self, &fd);
+ if (error < 0) {
+ return error;
+ }
+
+ /*
+ * Now we try to do the lookup, we'll need
+ * the vnode for file references to be
+ * useful
+ */
+ nd.path = path;
+ nd.flags = 0;
+ nd.vp_res = &vp;
+ error = namei(&nd);
+ if (error < 0) {
+ return error;
+ }
+
+ fd->vp = vp;
+ return fd->fdno;
+}
+
/*
* Initialize file descriptor table
*/