From 9c5752e9a9210056424b9ee4592183d5c438342b Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Fri, 16 Jul 2021 22:52:26 -0400 Subject: [PATCH] add environment variables --- src/array.c | 97 +++++++++++++++++++++++++++++++++++++++++++-- src/builtins.c | 3 ++ src/console_line.c | 7 ++-- src/include/array.h | 19 +++++++++ src/loop.c | 6 ++- 5 files changed, 124 insertions(+), 8 deletions(-) diff --git a/src/array.c b/src/array.c index 0c2bbed..30d2a92 100644 --- a/src/array.c +++ b/src/array.c @@ -38,7 +38,7 @@ StringArray *create_string_array() { * @param string The string to insert into the String Array. */ void insert_string_array(StringArray *string_array, char *string) { - if (string_array->size == 0) { + if (string_array->array == NULL) { string_array->array = malloc(2 * sizeof(char *)); if (string_array->array == NULL) { perror("malloc"); @@ -54,7 +54,7 @@ void insert_string_array(StringArray *string_array, char *string) { exit(EXIT_FAILURE); } } - string_array->array[string_array->size] = malloc(sizeof(string)); + string_array->array[string_array->size] = malloc(strlen(string) * sizeof(char *)); if (string_array->array == NULL) { perror("malloc"); free_string_array(string_array); @@ -76,7 +76,7 @@ void delete_string_array(StringArray *string_array, int index) { for (size_t i = index; i < string_array->size - 1; i++) { free(string_array->array[i]); string_array->array[i] = NULL; - string_array->array[i] = malloc(sizeof(string_array->array[i + 1])); + string_array->array[i] = malloc(strlen(string_array->array[i + 1]) * sizeof(char *)); if (string_array->array[i] == NULL) { perror("malloc"); exit(EXIT_FAILURE); @@ -121,3 +121,94 @@ void free_string_array(StringArray *string_array) { string_array = NULL; } } + +/** + * Create a new Array List. + * @return Returns the newly created Array List. + */ +ArrayList *create_array_list() { + ArrayList *array_list = malloc(sizeof(ArrayList)); + if (array_list == NULL) { + perror("malloc"); + exit(EXIT_FAILURE); + } + array_list->keys = NULL; + array_list->values = NULL; + array_list->size = 0; + return array_list; +} + +/** + * Set a key inside the Array List. + * @param array_list The Array List to work on. + * @param key The key to insert/update. + * @param value The value to insert/update. + */ +void set_array_list(ArrayList *array_list, char *key, char *value) { + if (array_list->keys == NULL) { + array_list->keys = create_string_array(); + array_list->values = create_string_array(); + } + for (size_t i = 0; i < array_list->size; i++) { + if (strcmp(array_list->keys->array[i], key) == 0) { + array_list->values->array[i] = realloc(array_list->values->array[i], strlen(value) * sizeof(char *)); + strcpy(array_list->values->array[i], value); + if (array_list->values->array[i] == NULL) { + perror("realloc"); + exit(EXIT_FAILURE); + } + return; + } + } + insert_string_array(array_list->keys, key); + insert_string_array(array_list->values, value); + array_list->size++; +} + +/** + * Get a value based on a key from an Array List. + * @param array_list The Array List to work on. + * @param key The key to search for. + * @return Returns the value if the key is found in the Array List otherwise it returns NULL. + */ +char *get_array_list(ArrayList *array_list, char *key) { + if (array_list->keys != NULL) { + for (size_t i = 0; i < array_list->size; i++) { + if (strcmp(array_list->keys->array[i], key) == 0) { + return array_list->values->array[i]; + } + } + } + return NULL; +} + +/** + * Remove a key from the Array List. + * @param array_list The Array List to work on. + * @param key The key to remove. + */ +void unset_array_list(ArrayList *array_list, char *key) { + if (array_list->keys != NULL) { + for (size_t i = 0; i < array_list->size; i++) { + if (strcmp(array_list->keys->array[i], key) == 0) { + delete_string_array(array_list->keys, i); + delete_string_array(array_list->values, i); + array_list->size--; + return; + } + } + } +} + +/** + * Free all the memory used in the Array List. + * @param array_list The Array List to free. + */ +void free_array_list(ArrayList *array_list) { + if (array_list != NULL) { + free_string_array(array_list->keys); + free_string_array(array_list->values); + free(array_list); + array_list = NULL; + } +} diff --git a/src/builtins.c b/src/builtins.c index 1f33298..586f82c 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -84,5 +84,8 @@ void change_directory(StringArray *args) { if (chdir(args->array[1]) != 0) { perror("cd"); } + else { + set_array_list(variables, "PWD", get_working_directory()); + } } } diff --git a/src/console_line.c b/src/console_line.c index 6c31c86..6af95c6 100644 --- a/src/console_line.c +++ b/src/console_line.c @@ -21,6 +21,7 @@ #include #include #include +#include "array.h" #include "color.h" #include "console_line.h" @@ -37,7 +38,7 @@ void remove_new_line(char* line) { * @return Returns the logged in user's username. */ char *get_username() { - struct passwd *pass; + struct passwd *pass = NULL; pass = getpwuid(getuid()); if (pass == NULL) { perror("getpwuid"); @@ -67,8 +68,8 @@ char *get_hostname() { * @return Returns the current working directory. */ char *get_working_directory() { - char *cwd = NULL; - cwd = getcwd(NULL, PATH_MAX); + char *cwd = malloc(PATH_MAX * sizeof(char *)); + getcwd(cwd, PATH_MAX); if (cwd == NULL) { perror("getcwd"); exit(EXIT_FAILURE); diff --git a/src/include/array.h b/src/include/array.h index a30e90d..1b046c0 100644 --- a/src/include/array.h +++ b/src/include/array.h @@ -27,6 +27,15 @@ typedef struct { size_t size; } CleanArray; +typedef struct { + StringArray *keys; + StringArray *values; + size_t size; +} ArrayList; + +CleanArray clean; +ArrayList *variables; + StringArray *create_string_array(); void insert_string_array(StringArray *string_array, char *string); @@ -34,4 +43,14 @@ void insert_string_array(StringArray *string_array, char *string); void delete_string_array(StringArray *string_array, int index); void free_string_array(StringArray *string_array); + +ArrayList *create_array_list(); + +void set_array_list(ArrayList *array_list, char *key, char *value); + +char *get_array_list(ArrayList *array_list, char *key); + +void unset_array_list(ArrayList *array_list, char *key); + +void free_array_list(ArrayList *array_list); #endif diff --git a/src/loop.c b/src/loop.c index 5aaf20e..8860444 100644 --- a/src/loop.c +++ b/src/loop.c @@ -22,8 +22,6 @@ #include "console_line.h" #include "launch.h" -static CleanArray clean; - /** * Add memory address to array to be cleaned up later. * @param data The data to be cleaned up on exit. @@ -51,6 +49,7 @@ void exit_cleanup() { free(clean.array); clean.array = NULL; } + free_array_list(variables); } /** @@ -58,7 +57,10 @@ void exit_cleanup() { */ void loop() { clean.size = 0; + variables = create_array_list(); atexit(exit_cleanup); + set_array_list(variables, "PWD", get_working_directory()); + while (1) { print_input_line();