]> git.corax.cc Git - corax/commitdiff
sys/io: Add sys_read() implementation
authorMatthias Kruk <m@m10k.eu>
Sat, 15 Aug 2020 06:20:42 +0000 (15:20 +0900)
committerMatthias Kruk <m@m10k.eu>
Sat, 15 Aug 2020 06:20:42 +0000 (15:20 +0900)
sys/io/read.c [new file with mode: 0644]

diff --git a/sys/io/read.c b/sys/io/read.c
new file mode 100644 (file)
index 0000000..75ed74a
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * This file is part of the Corax operating system.
+ * Copyright (C) 2020 Matthias Kruk
+ *
+ * Corax is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Corax is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Corax.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <crxstd.h>
+#include <corax/ipc/io.h>
+#include "proc.h"
+#include "filedesc.h"
+
+ssize_t sys_read(const pid_t pid, struct cxio_read *msg)
+{
+       struct proc *proc;
+       struct filedesc *filedesc;
+       struct cxmsg resp;
+       struct cxio_response *ioresp;
+       ssize_t ret_val;
+
+       ret_val = proc_lookup(pid, &proc);
+
+       ioresp = (struct cxio_response*)resp.cm_data;
+
+       memset(&resp, 0, sizeof(resp));
+
+       if(!ret_val) {
+               ret_val = proc_get_fd(proc, msg->fd, &filedesc);
+
+               if(!ret_val) {
+                       ret_val = fd_read(filedesc, ioresp->extradata, msg->n);
+
+                       if(ret_val >= 0) {
+                               ioresp->retval = ret_val;
+                               ioresp->error = 0;
+                               ioresp->extralen = ret_val;
+                       }
+               }
+       }
+
+       if(ret_val < 0) {
+               ioresp->retval = -1;
+               ioresp->error = -ret_val;
+       }
+
+       ioresp->fd = msg->fd;
+       resp.cm_type = CXIO_RESPONSE;
+       resp.cm_len = ioresp->extralen + sizeof(*ioresp);
+
+       ret_val = cxsend(pid, &resp);
+
+       return(ret_val);
+}