diff options
| author | nasrlol <nsrddyn@gmail.com> | 2025-02-10 22:03:54 +0100 |
|---|---|---|
| committer | nasrlol <nsrddyn@gmail.com> | 2025-02-10 22:03:54 +0100 |
| commit | 9359850dd1944e0331f42c25d969c6f2770ca9b2 (patch) | |
| tree | 8b4076a20757cf981d3fa86e1d07091050296a28 | |
| parent | 0df51505f5d7035de1ad2007e08ea0d3bf365669 (diff) | |
worked on an algorithm for the echo function
| -rwxr-xr-x | a.out | bin | 15720 -> 15816 bytes | |||
| -rw-r--r-- | current_directory.c | 37 | ||||
| -rw-r--r-- | main.c | 79 |
3 files changed, 92 insertions, 24 deletions
| Binary files 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 <unistd.h> +#include <stdio.h> +#include <linux/limits.h> + +int main() +{ + char cwd[PATH_MAX]; + if (getcwd(cwd, sizeof(cwd)) != NULL) + printf("%s", cwd); + else { + perror("getcwd() error"); + return 1; + } + return 0; +} + + + + + @@ -8,13 +8,14 @@ #include <dirent.h> #include <unistd.h> #include <stdbool.h> +#include <linux/limits.h> #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++) { - printf("argv"); + if (strcmp(&command[i], " ") == 0) + { + separation_index = i; + + for ( int j = 0; j < separation_index; j++) + { + argv = &command[j]; + } + } + } + if (strcmp(argv, "echo") == 0) + { + + 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; |
