From 9d968cbf5d718210011acc40f69a456a3b7c4836 Mon Sep 17 00:00:00 2001 From: nasrlol Date: Tue, 18 Feb 2025 21:11:55 +0100 Subject: started working on the change directory command, and im still reading the documentation about how I should approach this, also made a clear command, found how to do this in the clear man pages, what I did was pretty simple, just using the clear terminal screen text sequence and clear scroll buffer text sequence Signed-off-by: nasrlol --- main.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'main.c') diff --git a/main.c b/main.c index 07bc1a1..17e0469 100644 --- a/main.c +++ b/main.c @@ -62,6 +62,20 @@ struct com_struct split_command(char *input) { return NEW_COMMAND; } +void clear() { + // clear the visible part of the terminal -> see man pages clear command + printf("\033[2J"); + // clear the input buffer of the terminal -> see man pages clear command + printf("\033[3J"); +} + +void change_directory(const char *path) { + if (path == NULL) { + printf("No path found\n"); + return; + } +} + int main(void) { while (1) { printf("\n$ "); -- cgit v1.2.3-70-g09d2 From dcf4c564a03f6ebde88e301ff2135a2b2e00dba9 Mon Sep 17 00:00:00 2001 From: nasrlol Date: Tue, 18 Feb 2025 21:12:11 +0100 Subject: handling segmentation fault Signed-off-by: nasrlol --- main.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'main.c') diff --git a/main.c b/main.c index 17e0469..80a5fe7 100644 --- a/main.c +++ b/main.c @@ -22,6 +22,10 @@ void test() { void ls(const char *path) { struct dirent *entry; + if (path == NULL) { + printf("No path found\n"); + return; + } DIR *dP = opendir(path); // check if the directory got opened successfully -- cgit v1.2.3-70-g09d2 From 6334869a10cf038f67fb1192b531cd4deaf4ea49 Mon Sep 17 00:00:00 2001 From: nasrlol Date: Tue, 18 Feb 2025 21:12:35 +0100 Subject: thought that clearing the terminal before showing the shell would give it a more real experience Signed-off-by: nasrlol --- main.c | 1 + 1 file changed, 1 insertion(+) (limited to 'main.c') diff --git a/main.c b/main.c index 80a5fe7..6c81cfb 100644 --- a/main.c +++ b/main.c @@ -81,6 +81,7 @@ void change_directory(const char *path) { } int main(void) { + clear(); while (1) { printf("\n$ "); -- cgit v1.2.3-70-g09d2 From 343880eb1afe7dea39a459e871eca70519d7b14b Mon Sep 17 00:00:00 2001 From: nasrlol Date: Tue, 18 Feb 2025 21:12:56 +0100 Subject: handling seg faults again Signed-off-by: nasrlol --- main.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'main.c') diff --git a/main.c b/main.c index 6c81cfb..28f0677 100644 --- a/main.c +++ b/main.c @@ -91,7 +91,10 @@ 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 (new_input.arg != NULL) { + new_input.arg[strcspn(new_input.arg, "\n")] = '\0'; + } if (strcmp(new_input.com, "exit") == 0) { free(input); -- cgit v1.2.3-70-g09d2 From a12ce036193a43fdd802167895ef64abdd3d4383 Mon Sep 17 00:00:00 2001 From: nasrlol Date: Tue, 18 Feb 2025 21:13:06 +0100 Subject: again seg faults= Signed-off-by: nasrlol --- main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'main.c') diff --git a/main.c b/main.c index 28f0677..7ed0cee 100644 --- a/main.c +++ b/main.c @@ -103,7 +103,9 @@ int main(void) { if (strcmp(new_input.com, "ls") == 0) { ls(new_input.arg); free(new_input.com); - free(new_input.arg); + if (new_input.arg != NULL) { + free(new_input.arg); + } } if (strcmp(new_input.com, "echo") == 0) { printf("%s", new_input.arg); -- cgit v1.2.3-70-g09d2 From d5ad52317a8abdfaa5237cf326ce718d6f75eedf Mon Sep 17 00:00:00 2001 From: nasrlol Date: Tue, 18 Feb 2025 21:13:36 +0100 Subject: implemented the clear function in the if else ladder, dont know if there is a nicer way to do this, ill have to think about it Signed-off-by: nasrlol --- main.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'main.c') diff --git a/main.c b/main.c index 7ed0cee..797329d 100644 --- a/main.c +++ b/main.c @@ -111,5 +111,8 @@ int main(void) { printf("%s", new_input.arg); free(new_input.arg); } + if (strcmp(new_input.com, "clear") == 0) { + clear(); + } } } -- cgit v1.2.3-70-g09d2 From 6d0a41b2c3400ffc2d98edc0ea727e53760b2b7b Mon Sep 17 00:00:00 2001 From: nasrlol Date: Tue, 18 Feb 2025 22:56:09 +0100 Subject: started on the optimization of the code, i didnt like the if ladder so now im working on altnerative and better way to do what it dit, this involves cycling thorgh a strutred array and executing the matching function, this should be more performant and give better code readability also added function prototypes and figured out the feature scope of de software Signed-off-by: nasrlol --- main.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'main.c') diff --git a/main.c b/main.c index 797329d..076d23c 100644 --- a/main.c +++ b/main.c @@ -17,6 +17,39 @@ void test() { printf("\nprint to output"); } +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); + +void make_dir(const char *path); + +void remove_dir(const char *path); + +void remove_file(const char *path); + +void copy_files(const char *source, const char *dest); + +void move_files(const char *source, const char *dest); + +char *print_cdirectory(); + +void change_directory(const char *path); + +void clear(); + + +struct exec_command CommandsList[] = { + {"ls", ls}, +}; + + +// shell command functions // ls command // get the requested path and put it into a const later on when getting the user input -- cgit v1.2.3-70-g09d2 From 3960ffc393a97a26805f7a2e9e98f3125ae0f20d Mon Sep 17 00:00:00 2001 From: nasrlol Date: Tue, 18 Feb 2025 22:56:36 +0100 Subject: these are my curent findings on the getcwd command from the unistd.h library Signed-off-by: nasrlol --- main.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'main.c') diff --git a/main.c b/main.c index 076d23c..5db0488 100644 --- a/main.c +++ b/main.c @@ -106,6 +106,19 @@ void clear() { printf("\033[3J"); } +char *print_cdirectory() { + 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; +} + void change_directory(const char *path) { if (path == NULL) { printf("No path found\n"); -- cgit v1.2.3-70-g09d2 From 2bafe1c8d17533e9e45c96c243b6d2413a7ab67b Mon Sep 17 00:00:00 2001 From: nasrlol Date: Tue, 18 Feb 2025 22:56:53 +0100 Subject: Reworked alot of code and features Signed-off-by: nasrlol --- a.out | Bin 34088 -> 15936 bytes main | Bin 34088 -> 15832 bytes main.c | 70 ++++++++++++++++++++++++++++++++--------------------------------- 3 files changed, 35 insertions(+), 35 deletions(-) (limited to 'main.c') diff --git a/a.out b/a.out index 8f1b87b..bf2fd0d 100755 Binary files a/a.out and b/a.out differ diff --git a/main b/main index 9e7e6f9..36d8f50 100755 Binary files a/main and b/main differ diff --git a/main.c b/main.c index 5db0488..b0299c5 100644 --- a/main.c +++ b/main.c @@ -5,16 +5,45 @@ #include #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) { -- cgit v1.2.3-70-g09d2