summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2023-11-15 21:13:36 +0100
committerAnton Kling <anton@kling.gg>2023-11-15 22:11:26 +0100
commit835101f96e7c9fca00940e967c471c33e4e4dae9 (patch)
tree9c48b871d4b547fac8c3a2eda6c21242e593fdfd
parentc9358cdeac4522922df46fb6e3ab6a517203ec99 (diff)
VFS/LibC: Add getcwd()
-rw-r--r--kernel/Makefile2
-rw-r--r--kernel/cpu/syscall.c2
-rw-r--r--kernel/scalls/getcwd.c11
-rw-r--r--kernel/scalls/getcwd.h3
-rw-r--r--userland/libc/include/syscall.h1
-rw-r--r--userland/libc/include/unistd.h3
-rw-r--r--userland/libc/unistd/getcwd.c6
-rw-r--r--userland/minibox/utilities/ls.c6
8 files changed, 30 insertions, 4 deletions
diff --git a/kernel/Makefile b/kernel/Makefile
index eb8a90f..b5df27e 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 scalls/ppoll.o scalls/ftruncate.o kubsan.o scalls/mmap.o drivers/serial.o scalls/accept.o scalls/bind.o scalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o scalls/shm.o elf.o ksbrk.o sched/scheduler.o scalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o scalls/msleep.o scalls/uptime.o scalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o scalls/recvfrom.o math.o scalls/sendto.o signal.o scalls/kill.o scalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o scalls/chdir.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 scalls/ppoll.o scalls/ftruncate.o kubsan.o scalls/mmap.o drivers/serial.o scalls/accept.o scalls/bind.o scalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o scalls/shm.o elf.o ksbrk.o sched/scheduler.o scalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o scalls/msleep.o scalls/uptime.o scalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o scalls/recvfrom.o math.o scalls/sendto.o signal.o scalls/kill.o scalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o scalls/chdir.o scalls/getcwd.o
CFLAGS = -O0 -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
INCLUDE=-I./includes/ -I./libc/include/
diff --git a/kernel/cpu/syscall.c b/kernel/cpu/syscall.c
index 31fa055..40a5b07 100644
--- a/kernel/cpu/syscall.c
+++ b/kernel/cpu/syscall.c
@@ -11,6 +11,7 @@
#include <scalls/bind.h>
#include <scalls/chdir.h>
#include <scalls/ftruncate.h>
+#include <scalls/getcwd.h>
#include <scalls/kill.h>
#include <scalls/mkdir.h>
#include <scalls/mmap.h>
@@ -169,6 +170,7 @@ void (*syscall_functions[])() = {
(void(*))syscall_mkdir, (void(*))syscall_recvfrom,
(void(*))syscall_sendto, (void(*))syscall_kill,
(void(*))syscall_sigaction, (void(*))syscall_chdir,
+ (void(*))syscall_getcwd,
};
void syscall_function_handler(u32 eax, u32 arg1, u32 arg2, u32 arg3, u32 arg4,
diff --git a/kernel/scalls/getcwd.c b/kernel/scalls/getcwd.c
new file mode 100644
index 0000000..629d0f5
--- /dev/null
+++ b/kernel/scalls/getcwd.c
@@ -0,0 +1,11 @@
+#include <math.h>
+#include <scalls/getcwd.h>
+#include <sched/scheduler.h>
+
+char *syscall_getcwd(char *buf, size_t size) {
+ kprintf("syscall_getcwd\n");
+ const char *cwd = get_current_task()->current_working_directory;
+ size_t len = min(size, strlen(cwd));
+ strlcpy(buf, get_current_task()->current_working_directory, len);
+ return buf;
+}
diff --git a/kernel/scalls/getcwd.h b/kernel/scalls/getcwd.h
new file mode 100644
index 0000000..671d7bd
--- /dev/null
+++ b/kernel/scalls/getcwd.h
@@ -0,0 +1,3 @@
+#include <stddef.h>
+
+char *syscall_getcwd(char *buf, size_t size);
diff --git a/userland/libc/include/syscall.h b/userland/libc/include/syscall.h
index bd1d9fc..32d7b33 100644
--- a/userland/libc/include/syscall.h
+++ b/userland/libc/include/syscall.h
@@ -37,6 +37,7 @@
#define SYS_KILL 29
#define SYS_SIGACTION 30
#define SYS_CHDIR 31
+#define SYS_GETCWD 32
int syscall(uint32_t eax, uint32_t ebx, uint32_t ecx, uint32_t edx,
uint32_t esi, uint32_t edi);
diff --git a/userland/libc/include/unistd.h b/userland/libc/include/unistd.h
index 5654752..89c48d0 100644
--- a/userland/libc/include/unistd.h
+++ b/userland/libc/include/unistd.h
@@ -20,6 +20,7 @@ int unlink(const char *path);
int execvp(const char *file, char *const argv[]);
void _exit(int status);
void msleep(uint32_t ms); // not standard
-uint32_t uptime(void); // not standard
+uint32_t uptime(void); // not standard
int chdir(const char *path);
+char *getcwd(char *buf, size_t size);
#endif
diff --git a/userland/libc/unistd/getcwd.c b/userland/libc/unistd/getcwd.c
new file mode 100644
index 0000000..6050c95
--- /dev/null
+++ b/userland/libc/unistd/getcwd.c
@@ -0,0 +1,6 @@
+#include <syscall.h>
+#include <unistd.h>
+
+char *getcwd(char *buf, size_t size) {
+ return syscall(SYS_GETCWD, buf, size, NULL, NULL, NULL);
+}
diff --git a/userland/minibox/utilities/ls.c b/userland/minibox/utilities/ls.c
index dc93d36..bedc3a3 100644
--- a/userland/minibox/utilities/ls.c
+++ b/userland/minibox/utilities/ls.c
@@ -22,14 +22,16 @@ int ls_main(int argc, char **argv) {
}*/
struct dirent **namelist;
int n;
- COND_PERROR_EXP(-1 == (n = scandir("/", &namelist, 0, 0)), "scandir",
+ char path[256];
+ (void)getcwd(path, 256);
+ COND_PERROR_EXP(-1 == (n = scandir(path, &namelist, 0, 0)), "scandir",
return 1);
int prev = 0;
for (int i = 0; i < n; i++) {
if (!list_hidden)
if ('.' == *namelist[i]->d_name)
- continue;
+ continue;
if (prev)
putchar(newline ? '\n' : ' ');