From 4764846cc2afe0e56f4490e3973b7322c2129e29 Mon Sep 17 00:00:00 2001
From: Anton Kling <anton@kling.gg>
Date: Fri, 24 Nov 2023 21:47:02 +0100
Subject: Refactor write() and pwrite()

---
 kernel/Makefile                |  2 +-
 kernel/cpu/syscall.c           | 47 +++++++++++++++---------------------------
 kernel/includes/syscalls.h     |  2 ++
 kernel/syscalls/pwrite.c       |  6 ++++++
 kernel/syscalls/write.c        | 12 +++++++++++
 userland/libc/Makefile         |  2 +-
 userland/libc/include/stdio.h  |  2 --
 userland/libc/include/unistd.h |  2 ++
 userland/libc/libc.c           | 14 -------------
 userland/libc/unistd/pwrite.c  |  6 ++++++
 userland/libc/unistd/write.c   |  6 ++++++
 11 files changed, 53 insertions(+), 48 deletions(-)
 create mode 100644 kernel/syscalls/pwrite.c
 create mode 100644 kernel/syscalls/write.c
 create mode 100644 userland/libc/unistd/pwrite.c
 create mode 100644 userland/libc/unistd/write.c

diff --git a/kernel/Makefile b/kernel/Makefile
index 1b7eea2..0778cb3 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -1,6 +1,6 @@
 CC="i686-sb-gcc"
 AS="i686-sb-as"
-OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o  read_eip.o libc/exit/assert.o process.o cpu/int_syscall.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o halts.o syscalls/ppoll.o syscalls/ftruncate.o kubsan.o syscalls/mmap.o drivers/serial.o syscalls/accept.o syscalls/bind.o syscalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o syscalls/shm.o elf.o ksbrk.o sched/scheduler.o syscalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o syscalls/msleep.o syscalls/uptime.o syscalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o syscalls/recvfrom.o math.o syscalls/sendto.o signal.o syscalls/kill.o syscalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o syscalls/chdir.o syscalls/getcwd.o syscalls/isatty.o syscalls/randomfill.o syscalls/open.o
+OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o  read_eip.o libc/exit/assert.o process.o cpu/int_syscall.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o halts.o syscalls/ppoll.o syscalls/ftruncate.o kubsan.o syscalls/mmap.o drivers/serial.o syscalls/accept.o syscalls/bind.o syscalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o syscalls/shm.o elf.o ksbrk.o sched/scheduler.o syscalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o syscalls/msleep.o syscalls/uptime.o syscalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o syscalls/recvfrom.o math.o syscalls/sendto.o signal.o syscalls/kill.o syscalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o syscalls/chdir.o syscalls/getcwd.o syscalls/isatty.o syscalls/randomfill.o syscalls/open.o syscalls/write.o syscalls/pwrite.o
 CFLAGS = -O3 -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign -DKERNEL
 INCLUDE=-I./includes/ -I../include/ -I./libc/include/
 
diff --git a/kernel/cpu/syscall.c b/kernel/cpu/syscall.c
index cb2b52e..9aecc42 100644
--- a/kernel/cpu/syscall.c
+++ b/kernel/cpu/syscall.c
@@ -53,19 +53,6 @@ int syscall_read(SYS_READ_PARAMS *args) {
   return rc;
 }
 
-int syscall_pwrite(SYS_PWRITE_PARAMS *args) {
-  return vfs_pwrite(args->fd, args->buf, args->count, args->offset);
-}
-
-int syscall_write(int fd, const char *buf, size_t count) {
-  vfs_fd_t *fd_ptr = get_vfs_fd(fd);
-  if (!fd_ptr)
-    return -EBADF;
-  int rc = vfs_pwrite(fd, (char *)buf, count, fd_ptr->offset);
-  fd_ptr->offset += rc;
-  return rc;
-}
-
 int syscall_dup2(SYS_DUP2_PARAMS *args) {
   return vfs_dup2(args->org_fd, args->new_fd);
 }
@@ -127,23 +114,23 @@ int syscall_openpty(SYS_OPENPTY_PARAMS *args) {
 }
 
 void (*syscall_functions[])() = {
-    (void(*))syscall_open,      (void(*))syscall_read,
-    (void(*))syscall_write,     (void(*))syscall_pread,
-    (void(*))syscall_pwrite,    (void(*))syscall_fork,
-    (void(*))syscall_exec,      (void(*))syscall_getpid,
-    (void(*))syscall_exit,      (void(*))syscall_wait,
-    (void(*))syscall_brk,       (void(*))syscall_sbrk,
-    (void(*))syscall_pipe,      (void(*))syscall_dup2,
-    (void(*))syscall_close,     (void(*))syscall_openpty,
-    (void(*))syscall_poll,      (void(*))syscall_mmap,
-    (void(*))syscall_accept,    (void(*))syscall_bind,
-    (void(*))syscall_socket,    (void(*))syscall_shm_open,
-    (void(*))syscall_ftruncate, (void(*))syscall_stat,
-    (void(*))syscall_msleep,    (void(*))syscall_uptime,
-    (void(*))syscall_mkdir,     (void(*))syscall_recvfrom,
-    (void(*))syscall_sendto,    (void(*))syscall_kill,
-    (void(*))syscall_sigaction, (void(*))syscall_chdir,
-    (void(*))syscall_getcwd,    (void(*))syscall_isatty,
+    (void(*))syscall_open,       (void(*))syscall_read,
+    (void(*))syscall_write,      (void(*))syscall_pread,
+    (void(*))syscall_pwrite,     (void(*))syscall_fork,
+    (void(*))syscall_exec,       (void(*))syscall_getpid,
+    (void(*))syscall_exit,       (void(*))syscall_wait,
+    (void(*))syscall_brk,        (void(*))syscall_sbrk,
+    (void(*))syscall_pipe,       (void(*))syscall_dup2,
+    (void(*))syscall_close,      (void(*))syscall_openpty,
+    (void(*))syscall_poll,       (void(*))syscall_mmap,
+    (void(*))syscall_accept,     (void(*))syscall_bind,
+    (void(*))syscall_socket,     (void(*))syscall_shm_open,
+    (void(*))syscall_ftruncate,  (void(*))syscall_stat,
+    (void(*))syscall_msleep,     (void(*))syscall_uptime,
+    (void(*))syscall_mkdir,      (void(*))syscall_recvfrom,
+    (void(*))syscall_sendto,     (void(*))syscall_kill,
+    (void(*))syscall_sigaction,  (void(*))syscall_chdir,
+    (void(*))syscall_getcwd,     (void(*))syscall_isatty,
     (void(*))syscall_randomfill,
 };
 
diff --git a/kernel/includes/syscalls.h b/kernel/includes/syscalls.h
index a431d2c..7b2a966 100644
--- a/kernel/includes/syscalls.h
+++ b/kernel/includes/syscalls.h
@@ -16,6 +16,8 @@ typedef struct SYS_ACCEPT_PARAMS {
 
 int syscall_accept(SYS_ACCEPT_PARAMS *args);
 int syscall_open(const char *file, int flags, mode_t mode);
+int syscall_write(int fd, const char *buf, size_t count);
+int syscall_pwrite(int fd, const char *buf, size_t count, size_t offset);
 
 void syscall_randomfill(void *buffer, u32 size);
 
diff --git a/kernel/syscalls/pwrite.c b/kernel/syscalls/pwrite.c
new file mode 100644
index 0000000..b1e47fe
--- /dev/null
+++ b/kernel/syscalls/pwrite.c
@@ -0,0 +1,6 @@
+#include <fs/vfs.h>
+#include <syscalls.h>
+
+int syscall_pwrite(int fd, const char *buf, size_t count, size_t offset) {
+  return vfs_pwrite(fd, (char*)buf, count, offset);
+}
diff --git a/kernel/syscalls/write.c b/kernel/syscalls/write.c
new file mode 100644
index 0000000..bdd3927
--- /dev/null
+++ b/kernel/syscalls/write.c
@@ -0,0 +1,12 @@
+#include <fs/vfs.h>
+#include <syscalls.h>
+#include <errno.h>
+
+int syscall_write(int fd, const char *buf, size_t count) {
+  vfs_fd_t *fd_ptr = get_vfs_fd(fd);
+  if (!fd_ptr)
+    return -EBADF;
+  int rc = vfs_pwrite(fd, (char *)buf, count, fd_ptr->offset);
+  fd_ptr->offset += rc;
+  return rc;
+}
diff --git a/userland/libc/Makefile b/userland/libc/Makefile
index 6d654d5..7f745cb 100644
--- a/userland/libc/Makefile
+++ b/userland/libc/Makefile
@@ -2,7 +2,7 @@ CC="i686-sb-gcc"
 AR="i686-sb-ar"
 AS="i686-sb-as"
 CFLAGS = -ggdb -ffreestanding -O2 -Wall -Wextra -pedantic -Wimplicit-fallthrough -I./include/ -static -I../../include/ -Wno-int-conversion -Wno-unused-parameter -Wno-return-type
-OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.o memset.o assert.o stdio/snprintf.o stdio/vfprintf.o string/memcpy.o string/memcmp.o string/strcmp.o ubsan.o string/strcpy.o isspace.o stdio/puts.o stdio/putchar.o dirent/opendir.o dirent/readdir.o dirent/closedir.o unistd/getopt.o dirent/scandir.o dirent/alphasort.o stdio/printf.o stdio/vdprintf.o stdio/vprintf.o stdio/dprintf.o stdio/vprintf.o string/strlen.o string/strnlen.o stdio/stdin.o stdio/getchar.o stdio/fgetc.o arpa/inet/htons.o arpa/inet/htonl.o stdio/fread.o stdio/fwrite.o stdio/fopen.o stdio/fclose.o stdio/fseek.o ctype/isascii.o stdio/fprintf.o stdlib/atoi.o stdlib/strtol.o ctype/toupper.o ctype/tolower.o string/strcat.o string/strchr.o string/sscanf.o sys/stat/stat.o stdlib/getenv.o string/strrchr.o stdio/ftell.o stdio/tmpfile.o stdio/fgets.o stdio/feof.o stdio/fscanf.o stdio/ungetc.o string/strncmp.o stdio/fputc.o string/strncpy.o stdio/remove.o stdio/ferror.o stdio/fputs.o stdlib/rand.o stdlib/srand.o unistd/getpid.o stdlib/strtoul.o stdio/fflush.o stdlib/abort.o string/strcspn.o time/localtime.o time/time.o time/clock_gettime.o time/gmtime.o time/strftime.o string/strpbrk.o ctype/isdigit.o ctype/isalpha.o ctype/isxdigit.o ctype/ispunct.o stdio/setvbuf.o stdio/fileno.o stdio/putc.o stdio/sprintf.o stdlib/abs.o string/strspn.o stdlib/qsort.o string/memmove.o setjmp/longjmp.o setjmp/setjmp.o libgen/basename.o string/strdup.o string/strndup.o string/strlcpy.o stdlib/atexit.o stdio/open_memstream.o libgen/dirname.o unistd/unlink.o string/strstr.o string/strcasecmp.o string/strncasecmp.o stdlib/mkstemp.o string/strtok.o unistd/execvp.o unistd/_exit.o ctype/isalnum.o time/ctime_r.o stdlib/strtold.o sys/time/gettimeofday.o stdio/fgetpos.o stdio/fsetpos.o ctype/isprint.o stdlib/system.o stdio/tmpnam.o unistd/msleep.o stdlib/atof.o stdlib/strtod.o stdio/rename.o sys/stat/mkdir.o unistd/uptime.o unistd/ftruncate.o sys/socket/recvfrom.o sys/socket/sendto.o signal/kill.o signal/sigaction.o unistd/chdir.o unistd/getcwd.o stdio/getdelim.o stdio/getline.o unistd/isatty.o sys/socket/listen.o stdlib/realpath.o math/min.o math/max.o systemcall.o sys/random/randomfill.o fcntl/open.o
+OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.o memset.o assert.o stdio/snprintf.o stdio/vfprintf.o string/memcpy.o string/memcmp.o string/strcmp.o ubsan.o string/strcpy.o isspace.o stdio/puts.o stdio/putchar.o dirent/opendir.o dirent/readdir.o dirent/closedir.o unistd/getopt.o dirent/scandir.o dirent/alphasort.o stdio/printf.o stdio/vdprintf.o stdio/vprintf.o stdio/dprintf.o stdio/vprintf.o string/strlen.o string/strnlen.o stdio/stdin.o stdio/getchar.o stdio/fgetc.o arpa/inet/htons.o arpa/inet/htonl.o stdio/fread.o stdio/fwrite.o stdio/fopen.o stdio/fclose.o stdio/fseek.o ctype/isascii.o stdio/fprintf.o stdlib/atoi.o stdlib/strtol.o ctype/toupper.o ctype/tolower.o string/strcat.o string/strchr.o string/sscanf.o sys/stat/stat.o stdlib/getenv.o string/strrchr.o stdio/ftell.o stdio/tmpfile.o stdio/fgets.o stdio/feof.o stdio/fscanf.o stdio/ungetc.o string/strncmp.o stdio/fputc.o string/strncpy.o stdio/remove.o stdio/ferror.o stdio/fputs.o stdlib/rand.o stdlib/srand.o unistd/getpid.o stdlib/strtoul.o stdio/fflush.o stdlib/abort.o string/strcspn.o time/localtime.o time/time.o time/clock_gettime.o time/gmtime.o time/strftime.o string/strpbrk.o ctype/isdigit.o ctype/isalpha.o ctype/isxdigit.o ctype/ispunct.o stdio/setvbuf.o stdio/fileno.o stdio/putc.o stdio/sprintf.o stdlib/abs.o string/strspn.o stdlib/qsort.o string/memmove.o setjmp/longjmp.o setjmp/setjmp.o libgen/basename.o string/strdup.o string/strndup.o string/strlcpy.o stdlib/atexit.o stdio/open_memstream.o libgen/dirname.o unistd/unlink.o string/strstr.o string/strcasecmp.o string/strncasecmp.o stdlib/mkstemp.o string/strtok.o unistd/execvp.o unistd/_exit.o ctype/isalnum.o time/ctime_r.o stdlib/strtold.o sys/time/gettimeofday.o stdio/fgetpos.o stdio/fsetpos.o ctype/isprint.o stdlib/system.o stdio/tmpnam.o unistd/msleep.o stdlib/atof.o stdlib/strtod.o stdio/rename.o sys/stat/mkdir.o unistd/uptime.o unistd/ftruncate.o sys/socket/recvfrom.o sys/socket/sendto.o signal/kill.o signal/sigaction.o unistd/chdir.o unistd/getcwd.o stdio/getdelim.o stdio/getline.o unistd/isatty.o sys/socket/listen.o stdlib/realpath.o math/min.o math/max.o systemcall.o sys/random/randomfill.o fcntl/open.o unistd/write.o unistd/pwrite.o
 all: libc.a
 
 %.o: %.c
diff --git a/userland/libc/include/stdio.h b/userland/libc/include/stdio.h
index be8629d..aebf086 100644
--- a/userland/libc/include/stdio.h
+++ b/userland/libc/include/stdio.h
@@ -67,8 +67,6 @@ int putchar(int c);
 int puts(const char *s);
 int brk(void *addr);
 void *sbrk(intptr_t increment);
-int write(int fd, const char *buf, size_t count);
-int pwrite(int fd, const char *buf, size_t count, size_t offset);
 int printf(const char *format, ...);
 int pread(int fd, void *buf, size_t count, size_t offset);
 int read(int fd, void *buf, size_t count);
diff --git a/userland/libc/include/unistd.h b/userland/libc/include/unistd.h
index ccfec5e..d6811b8 100644
--- a/userland/libc/include/unistd.h
+++ b/userland/libc/include/unistd.h
@@ -28,4 +28,6 @@ char *getcwd(char *buf, size_t size);
 int isatty(int fd);
 int pread(int fd, void *buf, size_t count, size_t offset);
 int fork(void);
+int write(int fd, const char *buf, size_t count);
+int pwrite(int fd, const char *buf, size_t count, size_t offset);
 #endif
diff --git a/userland/libc/libc.c b/userland/libc/libc.c
index 6985ff9..ea097cb 100644
--- a/userland/libc/libc.c
+++ b/userland/libc/libc.c
@@ -181,20 +181,6 @@ int execv(char *path, char **argv) {
 
 int s_syscall(int sys);
 
-int write(int fd, const char *buf, size_t count) {
-  return syscall(SYS_WRITE, fd, buf, count, 0, 0);
-}
-
-int pwrite(int fd, const char *buf, size_t count, size_t offset) {
-  struct SYS_PWRITE_PARAMS args = {
-      .fd = fd,
-      .buf = buf,
-      .count = count,
-      .offset = offset,
-  };
-  return syscall(SYS_PWRITE, (u32)&args, 0, 0, 0, 0);
-}
-
 int wait(int *stat_loc) { return syscall(SYS_WAIT, (u32)stat_loc, 0, 0, 0, 0); }
 
 void exit(int status) { syscall(SYS_EXIT, (u32)status, 0, 0, 0, 0); }
diff --git a/userland/libc/unistd/pwrite.c b/userland/libc/unistd/pwrite.c
new file mode 100644
index 0000000..c777865
--- /dev/null
+++ b/userland/libc/unistd/pwrite.c
@@ -0,0 +1,6 @@
+#include <syscall.h>
+#include <unistd.h>
+
+int pwrite(int fd, const char *buf, size_t count, size_t offset) {
+  return syscall(SYS_PWRITE, fd, buf, count, offset, 0);
+}
diff --git a/userland/libc/unistd/write.c b/userland/libc/unistd/write.c
new file mode 100644
index 0000000..661fb01
--- /dev/null
+++ b/userland/libc/unistd/write.c
@@ -0,0 +1,6 @@
+#include <syscall.h>
+#include <unistd.h>
+
+int write(int fd, const char *buf, size_t count) {
+  return syscall(SYS_WRITE, fd, buf, count, 0, 0);
+}
-- 
cgit v1.2.3