diff --git a/src/fileutils.c b/src/fileutils.c index a9e7982..394be81 100644 --- a/src/fileutils.c +++ b/src/fileutils.c @@ -87,6 +87,14 @@ char* fs_resolve_path(const char* path) { char* new_path = NULL; char* tmp = __str_duplicate(path); int pos = __str_find_reverse(tmp, '/'); + + if (pos == -1) { + char* cwd = fs_cwd(); + new_path = calloc(strlen(cwd) + 2 + strlen(path), sizeof(char)); + snprintf(new_path, strlen(cwd) + 2 + strlen(path), "%s/%s", cwd, path); + free(cwd); + } + while (pos != -1) { tmp[pos] = '\0'; char* p = realpath(tmp, NULL); diff --git a/src/stringlib.c b/src/stringlib.c index 91d9bf0..2aff38a 100644 --- a/src/stringlib.c +++ b/src/stringlib.c @@ -373,11 +373,11 @@ char* s_append(char* s1, const char* s2) { char* s_append_alt(char* (*s1), const char* s2) { - if ((*s1) == NULL || s2 == NULL) + if (s2 == NULL) return (*s1); - size_t len = strlen(s2); - char* res = realloc(*s1, strlen(*s1) + len + 1); + size_t len = (*s1) == NULL ? 0 : strlen(*s1); + char* res = realloc(*s1, len + strlen(s2) + 1); strcat(res, s2); /* set s1 pointer to the res pointer */ *s1 = res; @@ -386,6 +386,8 @@ char* s_append_alt(char* (*s1), const char* s2) { } char* s_concat(const char* s1, const char* s2) { + if (s1 == NULL || s2 == NULL) + return NULL; char* ret = s_duplicate(s1); return s_append(ret, s2); } diff --git a/tests/fileutils_test.c b/tests/fileutils_test.c index 482bc68..6f87162 100644 --- a/tests/fileutils_test.c +++ b/tests/fileutils_test.c @@ -63,6 +63,11 @@ MU_TEST(test_setup_resolve_paths) { char* cwd = fs_cwd(); mu_assert_string_eq(cwd, path); free(path); + + path = NULL; + path = fs_resolve_path(""); /* this simulates a relative path without leading . */ + mu_assert_string_eq(cwd, path); + free(path); free(cwd); } diff --git a/tests/stringlib_test.c b/tests/stringlib_test.c index 2afc4fb..0142749 100644 --- a/tests/stringlib_test.c +++ b/tests/stringlib_test.c @@ -334,10 +334,16 @@ MU_TEST(test_append) { mu_assert_string_eq("This is a test of the system!", test); test = s_append(test, NULL); // this returns the same thing passed in mu_assert_string_eq("This is a test of the system!", test); - mu_assert_null(s_append(NULL, test)); + // mu_assert_null(s_append(NULL, test)); free(test); } +MU_TEST(test_append_alt) { + char* test = NULL; + test = s_append_alt(&test, "Test should no longer be null!"); + mu_assert_string_eq("Test should no longer be null!", test); + free(test); +} /******************************************************************************* * Test concat @@ -672,6 +678,7 @@ MU_TEST_SUITE(test_suite) { /* append */ MU_RUN_TEST(test_append); + MU_RUN_TEST(test_append_alt); /* concat */ MU_RUN_TEST(test_concat);