From 5a2dc6770718e5ecef950878bc4e4c398b4e33e3 Mon Sep 17 00:00:00 2001 From: Guamss Date: Thu, 30 Apr 2026 20:16:10 +0200 Subject: [PATCH] add: mkdir --- Makefile | 2 +- src/disk.c | 41 ++++++++++++++++++++++++++++++++++++++++- src/disk.h | 1 + src/exec.c | 5 +++-- src/utils.c | 29 +++++++++++++++++++++++++++++ src/utils.h | 3 ++- 6 files changed, 76 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 50008c9..9eb9398 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ NAME := seyshell DRIVE := disk CC := gcc -CFLAGS := -Wall -Wextra -Werror -g +CFLAGS := OBJ_DIR := obj SRC_DIR := src diff --git a/src/disk.c b/src/disk.c index 06886f3..02affd9 100644 --- a/src/disk.c +++ b/src/disk.c @@ -1,5 +1,9 @@ /** @file */ #include "disk.h" +#include "env.h" +#include +#include +#include int init_inode_in_disk(disk *d, int inode_index, int filetype, unsigned short perms, int owned_bloc) { @@ -100,6 +104,41 @@ int create_directory(disk *disk, inode *parent, char *dirname) { return new_idx; } +int do_mkdir(disk *d, char *dirpath) { + char *parent_path; + char *dirname; + + if (get_dirname_and_parent_path_by_absolute_path(dirpath, &parent_path, &dirname) == -1) { + dprintf(STDERR_FILENO, "mkdir: erreur jsp"); + return -1; + } + + char *token = strtok(parent_path, "/"); + int dir_index = 0; + + while (token != NULL) { + + dir_index = find_dir_inode_by_name(token, dir_index, d); + + if (dir_index == -1) { + dprintf(STDERR_FILENO, "mkdir: %s directory does not exist\n", token); + return -1; + } + + if (d->inodes[dir_index].filetype != TYPE_DIRECTORY) { + dprintf(STDERR_FILENO, "mkdir: %s is not a directory\n", token); + return -1; + } + + token = strtok(NULL, "/"); + } + + + create_directory(d, &d->inodes[dir_index], dirname); + + return 0; +} + void write_in_file(disk *d, int file_index, char *data /*, char mode*/) { inode file = d->inodes[file_index]; if (file.filetype != TYPE_FILE) { @@ -142,7 +181,7 @@ int find_dir_inode_by_name(char *name, int dir_index, disk *d) { int do_ls(disk *d, char *path) { - char* path_to_process = malloc(1024); + char *path_to_process = malloc(1024); if (path_to_process == NULL) { return -1; } diff --git a/src/disk.h b/src/disk.h index 2bb1c0d..cb3fff1 100644 --- a/src/disk.h +++ b/src/disk.h @@ -15,4 +15,5 @@ disk create_disk(); disk open_disk(char* filename); int do_ls(disk *d, char* path); +int do_mkdir(disk* d, char *dirpath); int find_dir_inode_by_name(char *name, int dir_index, disk *d); \ No newline at end of file diff --git a/src/exec.c b/src/exec.c index 861fc99..2dfcfd0 100644 --- a/src/exec.c +++ b/src/exec.c @@ -1,7 +1,6 @@ /** @file */ #include "exec.h" - int do_cd(disk *d, char *path) { char full_path[1024] = {0}; @@ -33,6 +32,8 @@ int do_cd(disk *d, char *path) { int child_process_job(disk *d, char **args) { if (strcmp("ls", args[0]) == 0) { do_ls(d, args[1]); + } else if (strcmp("mkdir", args[0]) == 0) { + do_mkdir(d, args[1]); } else { dprintf(STDERR_FILENO, "%s: no such command\n", args[0]); } @@ -44,7 +45,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 index a80d3e6..c0f4b46 100644 --- a/src/utils.c +++ b/src/utils.c @@ -12,6 +12,35 @@ void format_path(char *dest, char *src, int dest_len) { } } +int get_dirname_and_parent_path_by_absolute_path(char* dirpath, char** parent_path, char** dirname) { + if (dirpath == NULL || strlen(dirpath) == 0) { + return -1; + } + + size_t len = strlen(dirpath); + if (len > 1 && dirpath[len - 1] == '/') { + dirpath[len - 1] = '\0'; + } + + char *last_occ = strrchr(dirpath, '/'); + + if (last_occ == NULL) { + *parent_path = strdup(get_env_value("PWD")); + *dirname = strdup(dirpath); + } else { + *dirname = strdup(last_occ + 1); + + if (last_occ == dirpath) { + *parent_path = strdup("/"); + } else { + *last_occ = '\0'; + *parent_path = strdup(dirpath); + *last_occ = '/'; + } + } + return 0; +} + void canonicalize_path(char *path) { char *tokens[100]; int depth = 0; diff --git a/src/utils.h b/src/utils.h index 2aa98d8..6c00d7b 100644 --- a/src/utils.h +++ b/src/utils.h @@ -7,4 +7,5 @@ #include void canonicalize_path(char *path); -void format_path(char *dest, char *src, int dest_len); \ No newline at end of file +void format_path(char *dest, char *src, int dest_len); +int get_dirname_and_parent_path_by_absolute_path(char* dirpath, char** parent_path, char** dirname); \ No newline at end of file