From e6b16a313f3ab539f5baba4ab027778165a2f535 Mon Sep 17 00:00:00 2001 From: Abdellah El Morabit Date: Thu, 20 Feb 2025 21:08:07 +0100 Subject: encountering one bug after an other, having trouble with the pointers and string readings --- .idea/material_theme_project_new.xml | 10 +++++ a.out | Bin 51160 -> 51160 bytes cmake-build-debug/.ninja_deps | Bin 21248 -> 31076 bytes cmake-build-debug/.ninja_log | 42 +++++++++++++++++ cmake-build-debug/Testing/Temporary/LastTest.log | 4 +- cmake-build-debug/program | Bin 34552 -> 52216 bytes main.c | 55 ++++++++--------------- 7 files changed, 73 insertions(+), 38 deletions(-) create mode 100644 .idea/material_theme_project_new.xml diff --git a/.idea/material_theme_project_new.xml b/.idea/material_theme_project_new.xml new file mode 100644 index 0000000..49ce0d2 --- /dev/null +++ b/.idea/material_theme_project_new.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/a.out b/a.out index 554e6c7..fed0cda 100755 Binary files a/a.out and b/a.out differ diff --git a/cmake-build-debug/.ninja_deps b/cmake-build-debug/.ninja_deps index 432a091..87e96a7 100644 Binary files a/cmake-build-debug/.ninja_deps and b/cmake-build-debug/.ninja_deps differ diff --git a/cmake-build-debug/.ninja_log b/cmake-build-debug/.ninja_log index 7aca7ae..d40030a 100644 --- a/cmake-build-debug/.ninja_log +++ b/cmake-build-debug/.ninja_log @@ -31,3 +31,45 @@ 190 289 1739892455578321356 program ddf45f8a5c0d42d 2 59 1739892544799803717 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe 60 99 1739892544857519699 program ddf45f8a5c0d42d +10 293 1740076285695557812 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +293 426 1740076285978837845 program ddf45f8a5c0d42d +2 195 1740076415722251468 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +196 307 1740076415916320714 program ddf45f8a5c0d42d +2 220 1740076644746012336 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +220 343 1740076644963800405 program ddf45f8a5c0d42d +1 47 1740076738261761859 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +47 76 1740076738307199864 program ddf45f8a5c0d42d +4 228 1740076999657367434 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +228 363 1740076999881263708 program ddf45f8a5c0d42d +2 218 1740077352671269137 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +219 341 1740077352888230948 program ddf45f8a5c0d42d +2 192 1740077438589118222 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +194 298 1740077438780601359 program ddf45f8a5c0d42d +2 185 1740077565771877520 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +186 291 1740077565956181983 program ddf45f8a5c0d42d +1 47 1740077582306079915 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +47 77 1740077582352001936 program ddf45f8a5c0d42d +2 52 1740077594306692654 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +52 85 1740077594357132248 program ddf45f8a5c0d42d +2 167 1740080357181877690 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +168 266 1740080357348224979 program ddf45f8a5c0d42d +2 54 1740080368508784503 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +55 94 1740080368561584868 program ddf45f8a5c0d42d +2 49 1740080412454052242 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +50 80 1740080412501768200 program ddf45f8a5c0d42d +2 166 1740080486844809130 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +168 261 1740080487010403129 program ddf45f8a5c0d42d +5 186 1740080642790905250 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +188 292 1740080642973642634 program ddf45f8a5c0d42d +5 170 1740080821751019299 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +171 261 1740080821916713672 program ddf45f8a5c0d42d +2 224 1740081032462421227 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +225 346 1740081032685944457 program ddf45f8a5c0d42d +2 274 1740081379237611529 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +276 401 1740081379510882254 program ddf45f8a5c0d42d +13 338 1740081575536324940 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +340 491 1740081575862447237 program ddf45f8a5c0d42d +4 249 1740081678252707297 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +249 374 1740081678497134479 program ddf45f8a5c0d42d +2 247 1740082014330084017 CMakeFiles/program.dir/main.c.o 857d70be1ede4dfe +247 372 1740082014575379482 program ddf45f8a5c0d42d diff --git a/cmake-build-debug/Testing/Temporary/LastTest.log b/cmake-build-debug/Testing/Temporary/LastTest.log index ca88207..2db7162 100644 --- a/cmake-build-debug/Testing/Temporary/LastTest.log +++ b/cmake-build-debug/Testing/Temporary/LastTest.log @@ -1,3 +1,3 @@ -Start testing: Feb 18 16:29 CET +Start testing: Feb 20 21:06 CET ---------------------------------------------------------- -End testing: Feb 18 16:29 CET +End testing: Feb 20 21:06 CET diff --git a/cmake-build-debug/program b/cmake-build-debug/program index 28e15e9..695fe79 100755 Binary files a/cmake-build-debug/program and b/cmake-build-debug/program differ diff --git a/main.c b/main.c index 5ee11f6..ffe953d 100644 --- a/main.c +++ b/main.c @@ -21,44 +21,34 @@ struct exec_command { // code prototype functions struct command split_command(char *input); - void exec_command(char *input); // shell commands functions prototypes void list(const char *path); - void make_dir(const char *path); - void remove_dir(const char *path); - void remove_file(const char *path); - void copy_files(const char *arg); - void move_files(const char *arg); - void print_cdirectory(const char *arg); - void change_directory(const char *path); - void clear(const char *arg); - void echo(const char *arg); struct exec_command CommandsList[] = { {"ls", list}, - {"make_dir", make_dir}, - {"remove_dir", remove_dir}, - {"remove_file", remove_file}, - {"copy_files", copy_files}, - {"move_files", move_files}, - {"print_cdirectory", print_cdirectory}, - {"change_directory", change_directory}, - {"clear", clear}, + {"mkdir", make_dir}, + {"rmdir", remove_dir}, + {"rm", remove_file}, + {"cp", copy_files}, + {"mv", move_files}, + {"pwd", print_cdirectory}, + {"cd", change_directory}, + {"clr", clear}, {"echo", echo} }; -// code functions +// Code functions struct command split_command(char *input) { struct command NEW_COMMAND = {}; @@ -68,14 +58,16 @@ struct command split_command(char *input) { if (command != NULL) { NEW_COMMAND.com = strdup(command); } else { - printf("failed, null pointer detected, no initial command has been inserted"); + perror("failed, null pointer detected, no initial command has been inserted: see command"); free(command); } if (argument != NULL) { NEW_COMMAND.arg = strdup(argument); } else { + perror("failed, null pointer detected, no initial command has been inserted: see argument"); free(argument); + NEW_COMMAND.arg = NULL; } return NEW_COMMAND; } @@ -88,19 +80,19 @@ void exec_command(char *input) { } for (int i = 0; i < sizeof(*CommandsList) / sizeof(CommandsList->cmd[0]); i++) { - // ReSharper disable once CppIncompatiblePointerConversion if (strcmp(user_command.com, &CommandsList->cmd[i]) == 0) { CommandsList[i].func(user_command.arg); } } + free(user_command.com); + free(user_command.arg); } -// shell command functions // ls command void list(const char *path) { struct dirent *entry; if (path == NULL) { - printf("No path found\n"); + perror("No path found\n"); return; } DIR *dP = opendir(path); @@ -111,7 +103,7 @@ void list(const char *path) { return; } - // print the folder|directory name + // Print the folder|directory name while ((entry = readdir(dP)) != NULL) { printf("%s\n", entry->d_name); } @@ -139,15 +131,9 @@ void move_files(const char *arg) { } void clear(const char *arg) { - if (arg != NULL) { - printf("clear doesn't support any arguments, see help() for help"); - return; - } - - // clear the visible part of the terminal -> see man pages clear command printf("\033[2J"); - // clear the input buffer of the terminal -> see man pages clear command printf("\033[3J"); + printf("\033[H"); } void echo(const char *arg) { @@ -155,10 +141,6 @@ void echo(const char *arg) { } void print_cdirectory(const char *arg) { - if (arg != NULL) { - printf("print_cdirectory doesn't support any arguments, see help() for help"); - return; - } char *current_working_directory = getcwd(NULL, 0); printf("%s", current_working_directory); } @@ -170,7 +152,7 @@ void change_directory(const char *path) { } int main(void) { - clear(NULL); + clear(""); // ReSharper disable once CppDFAEndlessLoop while (1) { printf("\n$ "); @@ -178,5 +160,6 @@ int main(void) { char *input = malloc(sizeof(char *) * MAX_COMMAND_LENGTH); fgets(input, MAX_COMMAND_LENGTH, stdin); exec_command(input); + free(input); } } -- cgit v1.2.3-70-g09d2