From ba12753402fe22c33b3dec93cedc29221a166cf4 Mon Sep 17 00:00:00 2001 From: Guamss Date: Tue, 21 Apr 2026 11:25:50 +0200 Subject: [PATCH] add: read in files --- Makefile | 2 +- src/disk.c | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 50008c9..5944011 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 0281dca..a50b21e 100644 --- a/src/disk.c +++ b/src/disk.c @@ -140,15 +140,44 @@ void do_ls(disk *d, int dir_index) { return; } + // TODO: chercher sur tous les blocs possédés int bloc = dir.blocs[0]; for (int i = 0; i < MAX_BYTES_PER_BLOC; i += MAX_INODE_NAME + sizeof(int)) { if (d->blocs[bloc].datas[i] != '\0') { - printf("%s\n", &d->blocs[bloc].datas[i]); + 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); } } } +char *read_in_file(disk *d, int file_index) { + inode file = d->inodes[file_index]; + if (file.filetype != TYPE_FILE) { + dprintf(STDERR_FILENO, "Can only read in files\n"); + return NULL; + } + + // TODO: chercher sur tous les blocs possédés + int bloc = file.blocs[0]; + char *out; + int c_index = 0; + while (d->blocs[bloc].datas[c_index] != '\0' || c_index < MAX_BYTES_PER_BLOC) { + c_index++; + } + + out = malloc(c_index * sizeof(char)); + if (out == NULL) { + dprintf(STDERR_FILENO, "Error while allocating with malloc\n"); + exit(EXIT_FAILURE); + } + + strncpy(out, d->blocs[bloc].datas, c_index); + + return out; +} + void init_env_var_file(disk *d) { int inode_idx = create_inode(d, &d->inodes[0], TYPE_FILE, "env"); write_in_file(d, inode_idx, "PWD=/\nUSER=user"); @@ -159,7 +188,6 @@ void init_env_var_file(disk *d) { * renvoi ce disque * @return Le disque créé */ - disk create_disk() { FILE *fptr; fptr = fopen("disk", "wb");