syscalls: implement getcwd syscall

This commit is contained in:
2025-10-30 20:26:28 +03:00
parent 26c8280059
commit f4b0429062
9 changed files with 192 additions and 11 deletions
+52 -2
View File
@@ -416,8 +416,56 @@ int sys_chdir(TrapFrame *tf)
return -1;
}
int sys_getcwd(TrapFrame *tf)
#include "../include/vfs.h"
/**
* I'm very sorry before myself for this but these directory traversing things are so hard, follow_path in vfs was pretty okay,
* this is when shit got real though
*/
uint32_t sys_getcwd(TrapFrame *tf)
{
l9660_dir current_dir = *(current->cwd);
char path_reversed[256] = {0};
char component_name[64];
while (!is_root_dir(&current_dir)) {
uint32_t child_sector = current_dir.file.first_sector;
// Open the parent directory
l9660_dir parent_dir;
l9660_opendirat(&parent_dir, &current_dir, "..");
// Find our previous directory's name within the parent
find_name_for_sector(component_name, sizeof(component_name), &parent_dir, child_sector);
// Prepend the name to our reversed path string (e.g., "dash/" + "katau/" -> "katau/dash/")
strcat(path_reversed, component_name);
strcat(path_reversed, "/");
// Move up one level
current_dir = parent_dir;
}
// Now, reverse the string "dash/katau/" to create the final path "/katau/dash"
char final_path[256] = "/";
char* token = strtok(path_reversed, "/");
while (token != NULL) {
// A bit of a trick to prepend tokens
char temp[256];
strcpy(temp, "/");
strcat(temp, token);
strcat(temp, final_path);
strcpy(final_path, temp);
token = strtok(NULL, "/");
}
// Handle the root case where the loop doesn't run
if (strlen(final_path) > 1) {
final_path[strlen(final_path) - 1] = '\0'; // Remove trailing slash
}
strcpy((char*)tf->ebx, final_path);
//strcpy((char*)tf->ebx, current->cwd->fat->path);
return tf->ebx;
@@ -489,6 +537,8 @@ void handle_syscall(TrapFrame *tf)
debug_log("EDX: %X\n", tf->edx);
*/
debug_log("calling 0x%X\n", tf->eax);
switch(tf->eax)
{
case 1://exit
@@ -544,7 +594,7 @@ void handle_syscall(TrapFrame *tf)
default:
debug_log("unknown syscall: %X\n", tf->eax);
tf->eax = -1;
tf->eax = -38;//-ENOSYS
break;
}
}