diff --git a/src/disk.c b/src/disk.c index 02bb0af..61f6170 100644 --- a/src/disk.c +++ b/src/disk.c @@ -198,6 +198,8 @@ int do_rmdir(disk *disk, char *filepath) { free(parent_path); free(dirname); + persist_on_disk(disk); + return (res == 1) ? 0 : -1; } @@ -256,6 +258,8 @@ int do_rm(disk *disk, char *filepath) { free(parent_path); free(dirname); + persist_on_disk(disk); + return (res == 1) ? 0 : -1; } @@ -507,6 +511,42 @@ char *read_in_file(disk *d, int file_index) { return out; } +int do_cat(disk *disk, char* path_to_file) { + (void)disk; + (void)path_to_file; + + if (path_to_file == NULL) { + char buffer[4096]; + + while (fgets(buffer, 4096, stdin)) { + fprintf(stdout, "%s", buffer); + } + } + + char full_path[1024] = {0}; + + format_path(full_path, path_to_file, 1024); + + char *path_tmp = strdup(full_path); + int inode_index = 0; + char *token = strtok(path_tmp, "/"); + + while (token != NULL) { + inode_index = find_dir_inode_by_name(token, inode_index, disk); + + token = strtok(NULL, "/"); + } + + if (disk->inodes[inode_index].filetype == TYPE_DIRECTORY) { + dprintf(STDERR_FILENO, "cat: '%s' is a directory\n", full_path); + return -1; + } + + printf("%s\n", read_in_file(disk, inode_index)); + + return 1; +} + /** * @brief Créer un fichier "disk" de taille MAX_BYTES_PER_BLOC * MAX_BLOCS, * renvoi ce disque @@ -535,9 +575,13 @@ 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)); + //strncpy(&d.blocs[0].datas[0], ".", MAX_INODE_NAME); + //int point_inode = 1; + //d.owned_blocs[point_inode] = 1; + //memcpy(&d.blocs[0].datas[MAX_INODE_NAME], &point_inode, sizeof(int)); + + int test_file_inode = create_file(&d, &d.inodes[0], "test"); + write_in_file(&d, test_file_inode, "coucou petit\ntest!!"); int n = fwrite(&d, sizeof(disk), 1, fptr); if (n == 1) { diff --git a/src/disk.h b/src/disk.h index 0ca9807..4a75888 100644 --- a/src/disk.h +++ b/src/disk.h @@ -20,5 +20,6 @@ int do_mkdir(disk* d, char *dirpath); int do_df(disk *d); int do_rm(disk *disk, char *filepath); int do_rmdir(disk *disk, char *filepath); +int do_cat(disk *disk, char* path_to_file); int find_dir_inode_by_name(char *name, int dir_index, disk *d); void persist_on_disk(disk* d); \ No newline at end of file diff --git a/src/exec.c b/src/exec.c index fd926c1..b233c40 100644 --- a/src/exec.c +++ b/src/exec.c @@ -102,6 +102,8 @@ int child_process_job(disk *d, char **args) { do_rm(d, args[1]); } else if (strcmp("rmdir", args[0]) == 0) { do_rmdir(d, args[1]); + } else if(strcmp("cat", args[0]) == 0) { + do_cat(d, args[1]); } else { dprintf(STDERR_FILENO, "%s: no such command\n", args[0]); } @@ -109,6 +111,13 @@ int child_process_job(disk *d, char **args) { } int execute_cmd(disk *d, char **args) { + + // cas ou l'utilisateur fait CTRD+D + if (args[0] == NULL) { + printf("\n"); + exit(EXIT_SUCCESS); + } + if (strcmp(args[0], "cd") == 0) { return do_cd(d, args[1]); } else if (strcmp(args[0], "echo") == 0) { @@ -122,6 +131,7 @@ int execute_cmd(disk *d, char **args) { } if (pid == 0) { + signal(SIGINT, SIG_DFL); return child_process_job(d, args); } else { int status; diff --git a/src/parsing.c b/src/parsing.c index 085525c..a037f91 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -1,5 +1,7 @@ /** @file */ #include "parsing.h" +#include +#include /** * @brief Attend que l'utilisateur appuie sur entrée pour récupérer la ligne écrite @@ -91,7 +93,20 @@ void shell_loop(disk *disk) { } while (status); } +void signal_handler(int sig) { + (void)sig; + char *user = get_env_value("USER"); + if (user == NULL) { + exit(EXIT_FAILURE); + } + char *hostname = get_env_value("HOST"); + + dprintf(STDOUT_FILENO, "\n"); + dprintf(STDOUT_FILENO, "%s@%s %s> ", user, hostname, get_env_value("PWD")); +} + void open_seyshell(disk* disk) { + signal(SIGINT, signal_handler); // le processus père ne s'arrête pas quand il y a un SIGINT = sigma boy env* envs = init_envs(); shell_loop(disk); free(envs);