diff options
| author | Abdellah El Morabit <nsrddyn@gmail.com> | 2025-02-17 21:29:37 +0100 |
|---|---|---|
| committer | Abdellah El Morabit <nsrddyn@gmail.com> | 2025-02-17 21:29:37 +0100 |
| commit | bbb2acda9269c0f66d3b570e5860c7a184597f71 (patch) | |
| tree | bd464d872695f8a66cf066a52f7e5e43b021eb94 /main.c | |
| parent | 0e41f26df2454bc79273b733dc78beef49eaa15b (diff) | |
fixed the segmentation faults
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 28 |
1 files changed, 23 insertions, 5 deletions
@@ -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) { } } } - |
