fix: bug with ls path

This commit is contained in:
Guamss 2026-04-30 13:51:13 +02:00
parent 8971ed233d
commit b9a8a75bd6
5 changed files with 62 additions and 42 deletions

View File

@ -2,6 +2,7 @@
#include "disk.h"
#include "env.h"
#include "struct.h"
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -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, "/");

View File

@ -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";

View File

@ -2,48 +2,17 @@
#include "exec.h"
#include "disk.h"
#include "env.h"
#include "utils.h"
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
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;

45
src/utils.c Normal file
View File

@ -0,0 +1,45 @@
#include "utils.h"
#include "env.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
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);
}

4
src/utils.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
void canonicalize_path(char *path);
void format_path(char *dest, char *src, int dest_len);