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

diff --git a/sys/io/write.c b/sys/io/write.c
new file mode 100644 (file)
index 0000000..ca7962e
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * 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_write(const pid_t pid, struct cxio_write *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_write(filedesc, msg->data, msg->datalen);
+
+                       if(ret_val >= 0) {
+                               ioresp->retval = ret_val;
+                               ioresp->error = 0;
+                       }
+               }
+       }
+
+       if(ret_val < 0) {
+               ioresp->retval = -1;
+               ioresp->error = -ret_val;
+       }
+
+       ioresp->fd = msg->fd;
+       resp.cm_type = CXIO_RESPONSE;
+       resp.cm_len = sizeof(*ioresp);
+
+       ret_val = cxsend(pid, &resp);
+
+       return(ret_val);
+}