From f117343477e596937bdfcf0ddce447c0d127b9cf Mon Sep 17 00:00:00 2001 From: Stefan Zabka Date: Wed, 20 May 2020 21:13:35 +0200 Subject: Fixed another edge case and documentation --- 02_exercise/prompt_utils.c | 4 +++- 02_exercise/prompt_utils.h | 2 +- 02_exercise/prompt_utils_test.c | 1 + 02_exercise/prompt_utils_test.h | 4 ++-- 02_exercise/shell.c | 1 + 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/02_exercise/prompt_utils.c b/02_exercise/prompt_utils.c index 819509d..4f86811 100644 --- a/02_exercise/prompt_utils.c +++ b/02_exercise/prompt_utils.c @@ -64,7 +64,9 @@ char const *const relative_path(char const *const from_dir, char const *const to for (size_t j = 0; j < levels_up - 1; j++) { strcat(return_value, go_up); } - strcat(return_value, to_dir + common_position); + if (strcmp("/", to_dir) != 0) { + strcat(return_value, to_dir + common_position); + } } arrayRelease((void *)from_dir_indeces); diff --git a/02_exercise/prompt_utils.h b/02_exercise/prompt_utils.h index 93de502..908019c 100644 --- a/02_exercise/prompt_utils.h +++ b/02_exercise/prompt_utils.h @@ -6,6 +6,6 @@ // Returns the relative path to get from the `from_dir` to the `to_dir` char const *const relative_path(char const *const from_dir, char const *const to_dir); -// Returns the position of each element one after the seperator +// Returns the position of each occurence of the seperator size_t *get_seperator_indeces(char const *const text, char seperator); #endif // PROMPT_UTILS_H_INCLUDED \ No newline at end of file diff --git a/02_exercise/prompt_utils_test.c b/02_exercise/prompt_utils_test.c index b26ce6f..f648275 100644 --- a/02_exercise/prompt_utils_test.c +++ b/02_exercise/prompt_utils_test.c @@ -33,4 +33,5 @@ void test_relative_path() { test_paths("/Test/b/c", "/Test/b", ".."); test_paths("/", "/Test/a/asd", "./Test/a/asd"); test_paths("/Test/c", "/Test/b", "../b"); + test_paths("/B/C", "/", "../.."); } \ No newline at end of file diff --git a/02_exercise/prompt_utils_test.h b/02_exercise/prompt_utils_test.h index 5c637b2..fbaabde 100644 --- a/02_exercise/prompt_utils_test.h +++ b/02_exercise/prompt_utils_test.h @@ -1,5 +1,5 @@ -#ifndef PROMPT_UTILS_H_INCLUDED -#define PROMPT_UTILS_H_INCLUDED +#ifndef PROMPT_UTILS_TEST_H_INCLUDED +#define PROMPT_UTILS_TEST_H_INCLUDED void test_get_seperator_indeces(); void test_relative_path(); diff --git a/02_exercise/shell.c b/02_exercise/shell.c index c0c51e1..d155a58 100644 --- a/02_exercise/shell.c +++ b/02_exercise/shell.c @@ -20,6 +20,7 @@ void print_prompt(char const *const original_wd, char const *current_wd); int exec_command(char *command); int main(void) { + char const *const original_wd = get_current_dir_name(); char const *prompt = relative_path(original_wd, original_wd); printf("%s \n", original_wd); -- cgit v1.2.3-54-g00ecf