From bbb2acda9269c0f66d3b570e5860c7a184597f71 Mon Sep 17 00:00:00 2001 From: Abdellah El Morabit Date: Mon, 17 Feb 2025 21:29:37 +0100 Subject: fixed the segmentation faults --- main.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 52eb63b..650d646 100644 --- a/main.c +++ b/main.c @@ -22,7 +22,8 @@ void exit_() { } // ls command -// get the requested path and put it into a const later on when getting the user input +// get the requested path and put it into a const later on when getting the user input + void ls(const char *path) { struct dirent *entry; DIR *dP = opendir(path); @@ -46,9 +47,21 @@ struct com_struct split_command(char *input) { const char *command = strtok(input, " "); const char *argument = strtok(NULL, " "); - NEW_COMMAND.com = strdup(command); - NEW_COMMAND.arg = strdup(argument); + if (command != NULL) { + NEW_COMMAND.com = strdup(command); + } else + { + printf("failed, null pointer detected"); + free(command); + } + + if (argument != NULL) { + NEW_COMMAND.arg = strdup(argument); + } else { + printf("failed, null pointer detected"); + free(argument); + } return NEW_COMMAND; } @@ -62,7 +75,7 @@ int main(void) { const struct com_struct new_input = split_command(input); new_input.com[strcspn(new_input.com, "\n")] = '\0'; - new_input.arg[strcspn(new_input.arg, "\n")] = '\0'; + if (strcmp(new_input.com, "exit\n") == 0) { exit_(); @@ -73,6 +86,12 @@ int main(void) { } if (strcmp(new_input.com, "ls\n") == 0) { printf("DETECTED LS COMMAND"); + if (new_input.arg == NULL) { + printf("\nARGUMENT NOT DEFINED"); + free(new_input.com); + free(new_input.arg); + continue; + } ls(new_input.arg); } if (strcmp(new_input.com, "echo\n") == 0) { @@ -82,4 +101,3 @@ int main(void) { } } } - -- cgit v1.2.3-70-g09d2