diff options
| author | nasrlol <nsrddyn@gmail.com> | 2025-02-18 22:56:53 +0100 |
|---|---|---|
| committer | nasrlol <nsrddyn@gmail.com> | 2025-02-18 22:56:53 +0100 |
| commit | 2bafe1c8d17533e9e45c96c243b6d2413a7ab67b (patch) | |
| tree | 68ed4d9f15a259306594fdb71869ae9c186a7744 /main.c | |
| parent | 3960ffc393a97a26805f7a2e9e98f3125ae0f20d (diff) | |
Reworked alot of code and features
Signed-off-by: nasrlol <nsrddyn@gmail.com>
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 70 |
1 files changed, 35 insertions, 35 deletions
@@ -5,16 +5,45 @@ #include <unistd.h> #define MAX_HISTORY 100 -#define MAX_COMMAND_LENGTH 255 +#define MAX_COMMAND_LENGTH 255 // not setting a macro for the argument because they are included in the command) +#define MAX_PATH_LENGTH 1024 -struct com_struct { +struct command { char *com; char *arg; - int error; // 0: okay 1: not okay }; -void test() { - printf("\nprint to output"); +struct exec_command { + struct command *cmd; + void (*func); +}; + + +// code prototype functions +struct command split_command(char *input); + +void exec_command(char *input); + +// 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) { @@ -51,8 +80,6 @@ struct exec_command CommandsList[] = { // shell command functions // ls command -// 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; if (path == NULL) { @@ -69,36 +96,11 @@ void ls(const char *path) { // print the folder|directory name while ((entry = readdir(dP)) != NULL) { - char *temporary_variable = entry->d_name; printf("%s\n", entry->d_name); } closedir(dP); } -struct com_struct split_command(char *input) { - struct com_struct 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 { - printf("failed, null pointer detected, no argument has been added"); - free(argument); - } - - return NEW_COMMAND; -} - void clear() { // clear the visible part of the terminal -> see man pages clear command printf("\033[2J"); @@ -122,7 +124,6 @@ char *print_cdirectory() { void change_directory(const char *path) { if (path == NULL) { printf("No path found\n"); - return; } } @@ -134,8 +135,7 @@ int main(void) { char *input = malloc(sizeof(char *) * MAX_COMMAND_LENGTH); fgets(input, MAX_COMMAND_LENGTH, stdin); - const struct com_struct new_input = split_command(input); - + const struct command new_input = split_command(input); new_input.com[strcspn(new_input.com, "\n")] = '\0'; if (new_input.arg != NULL) { |
