summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorAbdellah El Morabit <nsrddyn@gmail.com>2025-02-19 11:39:53 +0100
committerAbdellah El Morabit <nsrddyn@gmail.com>2025-02-19 11:39:53 +0100
commitdd08a04b9d40222063a6ceb223c614c7197f8dfc (patch)
treee7c1a8e711c59f4e19f1a673bf81a5c8db992f6d /main.c
parent47f7b0ca57a37722c02dc3a4b8153c8f0ace93e6 (diff)
huge step in the code redesign got the core of the code working, now whats left is to work on small little features and then fine tune everything
Diffstat (limited to 'main.c')
-rw-r--r--main.c70
1 files changed, 4 insertions, 66 deletions
diff --git a/main.c b/main.c
index 4304a92..5ee11f6 100644
--- a/main.c
+++ b/main.c
@@ -5,7 +5,7 @@
#include <unistd.h>
#define MAX_HISTORY 100
-#define MAX_COMMAND_LENGTH 255 // not setting a macro for the argument because they are included in the command)
+#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 command {
@@ -15,44 +15,15 @@ struct command {
struct exec_command {
const char *cmd;
+
void (*func)(const char *);
};
-
// 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) {
- struct command NEW_COMMAND = {};
-
- // cycle through the command array and find the matching command and execute it
-}
-
-
// shell commands functions prototypes
void list(const char *path);
@@ -189,15 +160,7 @@ void print_cdirectory(const char *arg) {
return;
}
char *current_working_directory = getcwd(NULL, 0);
- /* getcwd() command already allocates malloc for the path that should be returned,
- to do: should I free it when finished or not?/
- so what I understand from the documentation, you should only free it when actually setting an exact size
- but when keeping the max size to 0 and relying on the malloc there is no need for it
- not sure though
- should rely on further debugging
- */
-
- return current_working_directory;
+ printf("%s", current_working_directory);
}
void change_directory(const char *path) {
@@ -214,31 +177,6 @@ int main(void) {
char *input = malloc(sizeof(char *) * MAX_COMMAND_LENGTH);
fgets(input, MAX_COMMAND_LENGTH, stdin);
-
- const struct command new_input = split_command(input);
- new_input.com[strcspn(new_input.com, "\n")] = '\0';
-
- if (new_input.arg != NULL) {
- new_input.arg[strcspn(new_input.arg, "\n")] = '\0';
- }
-
- if (strcmp(new_input.com, "exit") == 0) {
- free(input);
- return 0;
- }
- if (strcmp(new_input.com, "ls") == 0) {
- ls(new_input.arg);
- free(new_input.com);
- if (new_input.arg != NULL) {
- free(new_input.arg);
- }
- }
- if (strcmp(new_input.com, "echo") == 0) {
- printf("%s", new_input.arg);
- free(new_input.arg);
- }
- if (strcmp(new_input.com, "clear") == 0) {
- clear();
- }
+ exec_command(input);
}
}