fix: bug with cd path

This commit is contained in:
Guamss 2026-04-30 11:25:04 +02:00
parent a43436b20b
commit 8971ed233d
7 changed files with 101 additions and 24 deletions

View File

@ -8,4 +8,8 @@
#define TYPE_NULL 0
#define TYPE_FILE 1
#define TYPE_DIRECTORY 3
#define TYPE_SYMBOLIC_LINK 4
#define TYPE_SYMBOLIC_LINK 4
#define COLOR_BLUE "\x1b[34m"
#define COLOR_GREEN "\x1b[32m"
#define ESCAPE_COLOR "\x1b[0m"

View File

@ -128,6 +128,8 @@ void write_in_file(disk *d, int file_index, char *data /*, char mode*/) {
}
int find_dir_inode_by_name(char *name, int dir_index, disk *d) {
if (dir_index < 0 || dir_index >= MAX_INODE)
return -1;
int bloc = d->inodes[dir_index].blocs[0];
@ -145,6 +147,9 @@ 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;
if (path == NULL) {
@ -158,7 +163,7 @@ int do_ls(disk *d, char *path) {
while (token != NULL) {
dir_index = find_dir_inode_by_name(token, dir_index, d);
if (dir_index == -1) {
dprintf(STDERR_FILENO, "ls: This directory does not exist\n");
free(path_to_process);
@ -178,15 +183,18 @@ int do_ls(disk *d, char *path) {
int bloc = dir_to_list.blocs[0];
if (bloc == -1) {
free(path_to_process);
return 1;
free(path_to_process);
return 1;
}
for (int i = 0; i < MAX_BYTES_PER_BLOC; i += MAX_INODE_NAME + sizeof(int)) {
if (d->blocs[bloc].datas[i] != '\0') {
int inode_id;
memcpy(&inode_id, &d->blocs[bloc].datas[i + MAX_INODE_NAME], sizeof(int));
dprintf(STDOUT_FILENO, "%-16s %d\n", &d->blocs[bloc].datas[i], inode_id);
dprintf(STDOUT_FILENO, "%s%-16s %s%d\n",
d->inodes[inode_id].filetype == TYPE_FILE ? COLOR_GREEN
: COLOR_BLUE,
&d->blocs[bloc].datas[i], ESCAPE_COLOR, inode_id);
}
}
@ -256,6 +264,10 @@ disk create_disk() {
d.inodes[0].blocs[0] = 0; // utilise le bloc 0
d.owned_blocs[0] = 1; // le bloc 1 est maintenant occupé
strncpy(&d.blocs[0].datas[0], ".", MAX_INODE_NAME);
int point_inode = 1;
memcpy(&d.blocs[0].datas[MAX_INODE_NAME], &point_inode, sizeof(int));
init_test_files(&d);
int n = fwrite(&d, sizeof(disk), 1, fptr);

View File

@ -9,4 +9,5 @@
disk create_disk();
disk open_disk(char* filename);
int do_ls(disk *d, char* path);
int do_ls(disk *d, char* path);
int find_dir_inode_by_name(char *name, int dir_index, disk *d);

View File

@ -44,4 +44,16 @@ char *get_env_value(char *key) {
}
}
return NULL;
}
int set_env_value(char *key, char *value) {
env *envs = get_instance();
for (int i = 0; i < get_env_len(envs); i++) {
if (strcmp(envs[i].key, key) == 0) {
envs[i].value = strdup(value);
return 1;
}
}
return 0;
}

View File

@ -4,5 +4,6 @@
#include <string.h>
char* get_env_value(char* key);
int set_env_value(char *key, char *value);
int get_env_len(env *envs);
env* init_envs();

View File

@ -1,41 +1,87 @@
/** @file */
#include "exec.h"
#include "disk.h"
#include "env.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// à changer parce que je veux pas chercher dans le PATH, je veux juste executer
// les futures primitives qui seront implémentées
/*int execute_cmd(char **args) {
if (!is_builtin_cmd(args[0])) {
int pid = fork();
if (pid == -1)
return -1;
if (pid == 0) {
// Ce que va exécuter l'enfant
int status_code = execvp(args[0], args);
perror(args[0]);
return status_code;
} else {
// Ce que va exécuter le parent
int status;
waitpid(pid, &status, WUNTRACED);
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);
}
char *path_tmp = strdup(full_path);
int dir_index = 0;
char *token = strtok(path_tmp, "/");
while (token != NULL) {
dir_index = find_dir_inode_by_name(token, dir_index, d);
if (dir_index == -1 || d->inodes[dir_index].filetype != TYPE_DIRECTORY) {
dprintf(STDERR_FILENO, "cd: invalid path: %s\n", token);
free(path_tmp);
return -1;
}
token = strtok(NULL, "/");
}
canonicalize_path(full_path);
set_env_value("PWD", full_path);
free(path_tmp);
return 1;
}
*/
int child_process_job(disk *d, char **args) {
if (strcmp("ls", args[0]) == 0) {
do_ls(d, args[1]);
} else {
dprintf(STDERR_FILENO, "%s: no such command\n", args[0]);
}
return 1;
}
int execute_cmd(disk *d, char **args) {
if (strcmp(args[0], "cd") == 0) {
return do_cd(d, args[1]);
}
int pid = fork();
if (pid == 1) {

View File

@ -2,6 +2,7 @@
#pragma once
#include "disk.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int is_builtin_cmd(char *executable);