diff options
| author | Abdellah El Morabit <nsrddyn@gmail.com> | 2025-02-19 11:32:33 +0100 |
|---|---|---|
| committer | Abdellah El Morabit <nsrddyn@gmail.com> | 2025-02-19 11:32:33 +0100 |
| commit | 747896d1d1dcdfa1c066ca05a0103e4464a584a6 (patch) | |
| tree | e8472d24349c8dd493056722b99bce8839762028 /main.c | |
| parent | 994b0fa8d5ac69a3f6c2d3f33bef83cc6b33e5f6 (diff) | |
somehow made a function that exeutes the commands that it pulls from an array of commands and its working
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -77,6 +77,42 @@ struct exec_command CommandsList[] = { {"ls", ls}, }; +// code functions +struct command split_command(char *input) { + struct command NEW_COMMAND = {}; + + char *command = strtok(input, " "); + char *argument = strtok(NULL, " "); + + if (command != NULL) { + NEW_COMMAND.com = strdup(command); + } else { + printf("failed, null pointer detected, no initial command has been inserted"); + free(command); + } + + if (argument != NULL) { + NEW_COMMAND.arg = strdup(argument); + } else { + free(argument); + } + return NEW_COMMAND; +} + +void exec_command(char *input) { + const struct command user_command = split_command(input); + user_command.com[strcspn(user_command.com, "\n")] = '\0'; + if (user_command.arg != NULL) { + user_command.arg[strcspn(user_command.arg, "\n")] = '\0'; + } + + 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); + } + } +} // shell command functions // ls command |
