From b9a8a75bd6396ccca828864ed4e451812ddd1ccc Mon Sep 17 00:00:00 2001 From: Guamss Date: Thu, 30 Apr 2026 13:51:13 +0200 Subject: [PATCH] fix: bug with ls path --- src/disk.c | 14 ++++++++------ src/env.c | 2 +- src/exec.c | 39 ++++----------------------------------- src/utils.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/utils.h | 4 ++++ 5 files changed, 62 insertions(+), 42 deletions(-) create mode 100644 src/utils.c create mode 100644 src/utils.h diff --git a/src/disk.c b/src/disk.c index e98d9f5..a8c26a9 100644 --- a/src/disk.c +++ b/src/disk.c @@ -2,6 +2,7 @@ #include "disk.h" #include "env.h" #include "struct.h" +#include "utils.h" #include #include #include @@ -148,16 +149,17 @@ int find_dir_inode_by_name(char *name, int dir_index, disk *d) { int do_ls(disk *d, char *path) { - // problème de path, il faut faire comme sur cd - - char *path_to_process; + char* path_to_process = malloc(1024); + if (path_to_process == NULL) { + return -1; + } if (path == NULL) { - path_to_process = strdup(get_env_value("PWD")); - } else { - path_to_process = strdup(path); + path = strdup(get_env_value("PWD")); } + format_path(path_to_process, path, 1024); + int dir_index = 0; char *token = strtok(path_to_process, "/"); diff --git a/src/env.c b/src/env.c index b97b594..9fa13cd 100644 --- a/src/env.c +++ b/src/env.c @@ -7,7 +7,7 @@ env *init_envs() { return NULL; envs[0].key = "PWD"; - envs[0].value = "/"; + envs[0].value = "/dir"; envs[1].key = "USER"; envs[1].value = "guams"; diff --git a/src/exec.c b/src/exec.c index 783fa9a..b91601c 100644 --- a/src/exec.c +++ b/src/exec.c @@ -2,48 +2,17 @@ #include "exec.h" #include "disk.h" #include "env.h" +#include "utils.h" +#include "utils.h" #include #include #include #include -void canonicalize_path(char *path) { - char *tokens[100]; - int depth = 0; - - char *tmp = strdup(path); - char *token = strtok(tmp, "/"); - - while (token != NULL) { - if (strcmp(token, "..") == 0) { - if (depth > 0) depth--; - } else if (strcmp(token, ".") != 0 && strlen(token) > 0) { - tokens[depth++] = token; - } - token = strtok(NULL, "/"); - } - - strcpy(path, "/"); - for (int i = 0; i < depth; i++) { - strcat(path, tokens[i]); - if (i < depth - 1) strcat(path, "/"); - } - - free(tmp); -} - int do_cd(disk *d, char *path) { char full_path[1024] = {0}; - if (path == NULL || strcmp(path, "/") == 0 || strlen(path) == 0) { - strcpy(full_path, "/"); - } else if (path[0] == '/') { - strncpy(full_path, path, 1023); - } else { - char *current_pwd = get_env_value("PWD"); - snprintf(full_path, sizeof(full_path), "%s/%s", - strcmp(current_pwd, "/") == 0 ? "" : current_pwd, path); - } + format_path(full_path, path, 1024); char *path_tmp = strdup(full_path); int dir_index = 0; @@ -82,7 +51,7 @@ int execute_cmd(disk *d, char **args) { return do_cd(d, args[1]); } - int pid = fork(); + int pid = 0; if (pid == 1) { return -1; diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..e2a5545 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,45 @@ +#include "utils.h" +#include "env.h" +#include +#include +#include +#include + +void format_path(char *dest, char *src, int dest_len) { + if (src == NULL || strcmp(src, "/") == 0 || strlen(src) == 0) { + strcpy(dest, "/"); + } else if (src[0] == '/') { + strncpy(dest, src, 1023); + } else { + char *current_pwd = get_env_value("PWD"); + snprintf(dest, dest_len, "%s/%s", + strcmp(current_pwd, "/") == 0 ? "" : current_pwd, src); + } +} + +void canonicalize_path(char *path) { + char *tokens[100]; + int depth = 0; + + char *tmp = strdup(path); + char *token = strtok(tmp, "/"); + + while (token != NULL) { + if (strcmp(token, "..") == 0) { + if (depth > 0) + depth--; + } else if (strcmp(token, ".") != 0 && strlen(token) > 0) { + tokens[depth++] = token; + } + token = strtok(NULL, "/"); + } + + strcpy(path, "/"); + for (int i = 0; i < depth; i++) { + strcat(path, tokens[i]); + if (i < depth - 1) + strcat(path, "/"); + } + + free(tmp); +} \ No newline at end of file diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..5544065 --- /dev/null +++ b/src/utils.h @@ -0,0 +1,4 @@ +#pragma once + +void canonicalize_path(char *path); +void format_path(char *dest, char *src, int dest_len); \ No newline at end of file