From eb4f92268d205b6c3d61e161d4d693d16b51a554 Mon Sep 17 00:00:00 2001 From: Abdellah El Morabit Date: Tue, 18 Feb 2025 19:49:17 +0100 Subject: ls is working now --- main.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 42a1894..07bc1a1 100644 --- a/main.c +++ b/main.c @@ -39,7 +39,7 @@ void ls(const char *path) { } struct com_struct split_command(char *input) { - struct com_struct NEW_COMMAND; + struct com_struct NEW_COMMAND = {}; char *command = strtok(input, " "); char *argument = strtok(NULL, " "); @@ -48,48 +48,42 @@ struct com_struct split_command(char *input) { if (command != NULL) { NEW_COMMAND.com = strdup(command); } else { - printf("failed, null pointer detected"); + printf("failed, null pointer detected, no initial command has been inserted"); free(command); } if (argument != NULL) { NEW_COMMAND.arg = strdup(argument); } else { - printf("failed, null pointer detected"); + printf("failed, null pointer detected, no argument has been added"); free(argument); } - NEW_COMMAND.error = 1; + return NEW_COMMAND; } int main(void) { while (1) { - char *input = malloc(sizeof(char *) * MAX_COMMAND_LENGTH); - printf("\n$ "); + char *input = malloc(sizeof(char *) * MAX_COMMAND_LENGTH); fgets(input, MAX_COMMAND_LENGTH, stdin); - struct com_struct new_input = split_command(input); - new_input.com[strcspn(new_input.com, "\n")] = '\0'; + 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) { - if (input != NULL) - free(input); - if (new_input.com != NULL) - free(new_input.com); - if (new_input.arg != NULL) - free(new_input.arg); + if (strcmp(new_input.com, "exit") == 0) { + free(input); return 0; } - if (strcmp(new_input.com, "ls\n") == 0) { + if (strcmp(new_input.com, "ls") == 0) { ls(new_input.arg); free(new_input.com); free(new_input.arg); } - if (strcmp(new_input.com, "echo\n") == 0) { - printf("DETECTED ECHO COMMAND"); + if (strcmp(new_input.com, "echo") == 0) { printf("%s", new_input.arg); free(new_input.arg); } -- cgit v1.2.3-70-g09d2