diff options
author | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-05-20 18:07:43 +0200 |
---|---|---|
committer | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-05-20 18:07:43 +0200 |
commit | 9f7bf8ddf255f06a3c27c13dad87e34a70356d58 (patch) | |
tree | a09f18cbf96bd8f983ad1b67de22d57561ce4cf1 /02_exercise/prompt_utils.c | |
parent | 08d6167988a0bbec72d84ff12f8857f0b4d0b42a (diff) | |
download | betriebssysteme-9f7bf8ddf255f06a3c27c13dad87e34a70356d58.tar.gz betriebssysteme-9f7bf8ddf255f06a3c27c13dad87e34a70356d58.zip |
First implementation of prompt
Diffstat (limited to '02_exercise/prompt_utils.c')
-rw-r--r-- | 02_exercise/prompt_utils.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/02_exercise/prompt_utils.c b/02_exercise/prompt_utils.c new file mode 100644 index 0000000..819509d --- /dev/null +++ b/02_exercise/prompt_utils.c @@ -0,0 +1,90 @@ +#include "prompt_utils.h" + +#include "array.h" +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +char const *const relative_path(char const *const from_dir, char const *const to_dir) { + // easiest cases first + { + int result = strcmp(from_dir, to_dir); + if (result == 0) { + char *return_value = malloc(2); + strcpy(return_value, "."); + return return_value; + } + } + { + if (strcmp("/", from_dir) == 0) { + char *return_value = malloc(strlen(to_dir) + 2); + return_value[0] = '.'; + memcpy(return_value + 1, to_dir, strlen(to_dir) + 1); + return return_value; + } + } + // Splitting path into pieces so we can do strcmp on each of them + size_t const *const from_dir_indeces = get_seperator_indeces(from_dir, '/'); + size_t const *const to_dir_indeces = get_seperator_indeces(to_dir, '/'); + + size_t from_dir_len = arrayLen(from_dir_indeces); + size_t to_dir_len = arrayLen(to_dir_indeces); + + // finding the longest common substring + size_t array_len = from_dir_len < to_dir_len ? from_dir_len : to_dir_len; + size_t i = 0; + size_t common_position = 0; + for (; i < array_len - 1; i++) { + if (from_dir_indeces[i + 1] != to_dir_indeces[i + 1]) { + break; + } + size_t index = from_dir_indeces[i]; + size_t count = from_dir_indeces[i + 1] - from_dir_indeces[i]; + if (strncmp(from_dir + index, to_dir + index, count) != 0) { + break; + } + common_position = from_dir_indeces[i + 1]; + } + + size_t levels_up = from_dir_len - i - 1; + char *return_value; + if (levels_up == 0) { + // Equal dirs for whole length of path and not equal => subdir + size_t length = strlen(to_dir + common_position) + strlen("./") + 1; + return_value = malloc(length * sizeof(char)); + strcpy(return_value, "."); + strcat(return_value, to_dir + common_position); + } else { + char const *const go_up = "/.."; + size_t length = strlen(to_dir + common_position) + strlen("..") + + strlen(go_up) * (levels_up - 1); + return_value = malloc(length * sizeof(char)); + strcpy(return_value, ".."); + for (size_t j = 0; j < levels_up - 1; j++) { + strcat(return_value, go_up); + } + strcat(return_value, to_dir + common_position); + } + + arrayRelease((void *)from_dir_indeces); + arrayRelease((void *)to_dir_indeces); + + return return_value; +} + +size_t *get_seperator_indeces(char const *const text, char seperator) { + size_t *indeces; + arrayInit(indeces); + char const *current = text; + if (strchr(current, seperator) == NULL) { + arrayRelease(indeces); + return NULL; + } + while ((current = strchr(current, seperator)) != NULL) { + arrayPush(indeces) = current - text; + ++current; + } + arrayPush(indeces) = strlen(text); + return indeces; +} |