From 9359850dd1944e0331f42c25d969c6f2770ca9b2 Mon Sep 17 00:00:00 2001 From: nasrlol Date: Mon, 10 Feb 2025 22:03:54 +0100 Subject: worked on an algorithm for the echo function --- a.out | Bin 15720 -> 15816 bytes current_directory.c | 37 ++++++++++++++++++++++++ main.c | 79 ++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 92 insertions(+), 24 deletions(-) create mode 100644 current_directory.c diff --git a/a.out b/a.out index eed2891..be27e11 100755 Binary files a/a.out and b/a.out differ diff --git a/current_directory.c b/current_directory.c new file mode 100644 index 0000000..a9eec1b --- /dev/null +++ b/current_directory.c @@ -0,0 +1,37 @@ +/* + * ===================================================================================== + * + * Filename: current_directory.c + * + * + * Version: 1.0 + * Created: 02/10/2025 10:26:51 AM + * Revision: none + * Compiler: gcc + * + * Author: YOUR NAME (), + * Organization: + * + * ===================================================================================== + */ + +#include +#include +#include + +int main() +{ + char cwd[PATH_MAX]; + if (getcwd(cwd, sizeof(cwd)) != NULL) + printf("%s", cwd); + else { + perror("getcwd() error"); + return 1; + } + return 0; +} + + + + + diff --git a/main.c b/main.c index ca375e5..5f370bb 100644 --- a/main.c +++ b/main.c @@ -8,13 +8,14 @@ #include #include #include +#include #define MAX_HISTORY 100 #define MAX_COMMAND_LENGTH 256 -static int CURRENT_COMMAND_INDEX = 0; -static int CURRENT_HISTORY_INDEX = 0; -static char *COMMAND_HISTORY[MAX_HISTORY] = { NULL}; +// static int CURRENT_COMMAND_INDEX = 0; +// static int CURRENT_HISTORY_INDEX = 0; +// static char *COMMAND_HISTORY[MAX_HISTORY] = { NULL}; void test_print() @@ -29,48 +30,76 @@ void exit_program() exit(0); } -// pwd command -void list_directories() +// ls command +void list_current_directories() { - struct dirent **namelist; - int n; - n = scandir(".", &namelist, NULL, alphasort); - if (n == -1) - { - perror("scandir"); - exit(EXIT_FAILURE); - } + fork(); + + DIR *DIRECTORY_STREAM; + DIRECTORY_STREAM = opendir("."); - while (n--) + if (DIRECTORY_STREAM == NULL) { - printf("%s\n", namelist[n]->d_name); - free(namelist[n]); + perror("ERROR_DIRECTORY_STREAM_EMPTY"); } - free(namelist[n]); - exit(EXIT_SUCCESS); + else + printf("%p", (&DIRECTORY_STREAM)); + _exit(0); + } -void print_working_directory() +char *print_working_directory() { - char current_working_directory[100]; + char *current_working_directory = malloc(sizeof(char) * 100); if(getcwd(current_working_directory, sizeof(current_working_directory) != 0)) { - printf("%s", current_working_directory); + return current_working_directory; } else { perror("getcwd() error"); + return "ERROR"; } } // echo emulation command, without variables -void echo(char *command, char *argv) +char *echo(char *command) { - if (strcmp(command, "echo") == 0) + char *argv = (char *)malloc(sizeof(char) * 4); + char *command_argument = (char *)malloc(sizeof(char) * 100); + + int separation_index = 0; + for (int i = 0; i < sizeof(command); i++) + { + if (strcmp(&command[i], " ") == 0) + { + separation_index = i; + + for ( int j = 0; j < separation_index; j++) + { + argv = &command[j]; + } + } + } + if (strcmp(argv, "echo") == 0) { - printf("argv"); + + for (int i = separation_index + 1; i < (sizeof(command) - separation_index); i++) + { + if(strcmp(&command[i], "\0") != 0) + { + break; + } + else + { + strcpy(&command_argument[i - (separation_index - 1)], &command[i]); + } + } } + + return command_argument; + } @@ -107,6 +136,8 @@ int main(void) print_working_directory(); else if(strcmp(input, "test") == 0) test_print(); + else if(strcmp(input,"ls") == 0) + list_current_directories(); } return 0; -- cgit v1.2.3-70-g09d2