From f5fa1833ec0f86c62f519a6307e65197ba7597a9 Mon Sep 17 00:00:00 2001 From: sbosse Date: Mon, 14 Oct 2024 23:09:57 +0200 Subject: [PATCH] Mon 14 Oct 23:09:15 CEST 2024 --- user/test/write.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 user/test/write.c diff --git a/user/test/write.c b/user/test/write.c new file mode 100644 index 0000000..69b39d5 --- /dev/null +++ b/user/test/write.c @@ -0,0 +1,51 @@ +/* +Copyright (C) 2016-2019 The University of Notre Dame +This software is distributed under the GNU General Public License. +See the file LICENSE for details. +*/ + +/* +A trivial user level program to try out basic system calls. +*/ + +#include "library/syscalls.h" +#include "library/string.h" +#include "library/user-io.h" + +int main(int argc, char *argv[]) +{ + uint32_t j = 0; + syscall_chdir("/"); + printf("got root\n"); + int fd = syscall_open_file("kevin", 2, 0); + printf("got fd %d\n", fd); + printf("writing to file...\n"); + for(;;) { + j++; + char buffer[100] = "Hello, world! I can write to "; + char num[5]; + char newline[2] = "\n"; + int n; + uint_to_string(j, num); + strcat(buffer, num); + strcat(buffer, newline); + n = syscall_object_write(fd, buffer, strlen(buffer)); + if(n < 0) + break; + printf("wrote %d chars: %s\n", n, buffer); + } + syscall_object_close(fd); + fd = syscall_open_file("kevin", 1, 0); + char buffer[1000]; + int n; + printf("reading file...\n"); + while((n = syscall_object_read(fd, buffer, 999)) > 0) { + buffer[n] = 0; + printf("%s"); + flush(); + } + syscall_object_close(fd); + syscall_process_exit(0); + + return 0; +}