add: touch

This commit is contained in:
Guamss 2026-04-30 20:33:33 +02:00
parent 5a2dc67707
commit 2645c3368c
7 changed files with 77 additions and 2903 deletions

2863
Doxyfile

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
NAME := seyshell
DRIVE := disk
CC := gcc
CFLAGS :=
CFLAGS := -Wall -Wextra -Werror -g
OBJ_DIR := obj
SRC_DIR := src

View File

@ -104,11 +104,46 @@ int create_directory(disk *disk, inode *parent, char *dirname) {
return new_idx;
}
int do_touch(disk *d, char *filepath) {
char *parent_path;
char *dirname;
if (get_name_and_parent_path_by_absolute_path(filepath, &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, "touch: %s file does not exist\n", token);
return -1;
}
if (d->inodes[dir_index].filetype != TYPE_DIRECTORY) {
dprintf(STDERR_FILENO, "touch: %s is not a directory\n", token);
return -1;
}
token = strtok(NULL, "/");
}
create_file(d, &d->inodes[dir_index], dirname);
return 0;
}
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) {
if (get_name_and_parent_path_by_absolute_path(dirpath, &parent_path,
&dirname) == -1) {
dprintf(STDERR_FILENO, "mkdir: erreur jsp");
return -1;
}
@ -133,7 +168,6 @@ int do_mkdir(disk *d, char *dirpath) {
token = strtok(NULL, "/");
}
create_directory(d, &d->inodes[dir_index], dirname);
return 0;
@ -266,8 +300,6 @@ char *read_in_file(disk *d, int file_index) {
void init_test_files(disk *d) {
int inode_idx = create_file(d, &d->inodes[0], "test");
write_in_file(d, inode_idx, "Contenu du fichier :)\n");
int dir_idx = create_directory(d, &d->inodes[0], "dir");
create_file(d, &d->inodes[dir_idx], "test1");
}
/**

View File

@ -15,5 +15,6 @@
disk create_disk();
disk open_disk(char* filename);
int do_ls(disk *d, char* path);
int do_touch(disk *d, char *filepath);
int do_mkdir(disk* d, char *dirpath);
int find_dir_inode_by_name(char *name, int dir_index, disk *d);

View File

@ -1,5 +1,7 @@
/** @file */
#include "exec.h"
#include "disk.h"
#include <string.h>
int do_cd(disk *d, char *path) {
char full_path[1024] = {0};
@ -34,6 +36,8 @@ int child_process_job(disk *d, char **args) {
do_ls(d, args[1]);
} else if (strcmp("mkdir", args[0]) == 0) {
do_mkdir(d, args[1]);
} else if (strcmp("touch", args[0]) == 0) {
do_touch(d, args[1]);
} else {
dprintf(STDERR_FILENO, "%s: no such command\n", args[0]);
}

View File

@ -12,29 +12,29 @@ 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) {
int get_name_and_parent_path_by_absolute_path(char* path, char** parent_path, char** name) {
if (path == NULL || strlen(path) == 0) {
return -1;
}
size_t len = strlen(dirpath);
if (len > 1 && dirpath[len - 1] == '/') {
dirpath[len - 1] = '\0';
size_t len = strlen(path);
if (len > 1 && path[len - 1] == '/') {
path[len - 1] = '\0';
}
char *last_occ = strrchr(dirpath, '/');
char *last_occ = strrchr(path, '/');
if (last_occ == NULL) {
*parent_path = strdup(get_env_value("PWD"));
*dirname = strdup(dirpath);
*name = strdup(path);
} else {
*dirname = strdup(last_occ + 1);
*name = strdup(last_occ + 1);
if (last_occ == dirpath) {
if (last_occ == path) {
*parent_path = strdup("/");
} else {
*last_occ = '\0';
*parent_path = strdup(dirpath);
*parent_path = strdup(path);
*last_occ = '/';
}
}

View File

@ -8,4 +8,4 @@
void canonicalize_path(char *path);
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);
int get_name_and_parent_path_by_absolute_path(char* dirpath, char** parent_path, char** dirname);