From d996bc9b9bc2f06c99d09071f53ac4870f91cc78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Thu, 11 Feb 2021 11:47:29 +0100 Subject: implement write --- src/userprog/syscall.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/userprog/syscall.c') diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index 3a780c6..2116e5c 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -114,8 +114,26 @@ read (int fd_i, void *buf, unsigned size) static int write (int fd_i, const void *buf, unsigned size) { - printf ("write: %s", (char *)buf); - return 0; + struct thread *thread = thread_current (); + + if (fd_i == 1) { + // stdout + putbuf ((const char *)buf, size); + return size; + } else if (fd_i == 0) { + printf ("[%s] write: tried to write to stdin\n", thread->name); + return -1; + } + + struct file *file = get_file (thread, fd_i); + if (!file) { + return -1; + } + + int n = file_write(file, buf, size); + printf ("%d/%d: %s\n", n, size, (char *)buf); + + return n; } static void -- cgit v1.2.1