Skip to content

Commit c87dac0

Browse files
committed
#102 improve realpath unix management
1 parent 7ddc1aa commit c87dac0

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

clove-unit.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -983,15 +983,15 @@ char* __clove_path_to_absolute(const char* rel_path) {
983983

984984
//case where rel_path not really exists on fs
985985
//(in this case only the first subpath of the rel_path is added by realpath)
986-
if (!__clove_string_endswith(rel_path)) {
986+
if (!__clove_string_endswith(result, rel_path)) {
987987
if (__clove_path_is_absolute(rel_path)) {
988-
__clove_string_strcpy(result, _MAX_PATH, rel_path);
988+
__clove_string_strcpy(result, PATH_MAX, rel_path);
989989
} else { //relative
990-
realpath(result, ".");
990+
realpath(".", result);
991991
if (!__clove_string_endswith(result, "/")) {
992-
__clove_string_strcat(result, _MAX_PATH, "/");
992+
__clove_string_strcat(result, PATH_MAX, "/");
993993
}
994-
__clove_string_strcat(result, _MAX_PATH, rel_path);
994+
__clove_string_strcat(result, PATH_MAX, rel_path);
995995
}
996996
}
997997
#endif

tests/functs/src/unit/path_test.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ CLOVE_TEST(GetRelativePathFromAbsPath) {
8282
CLOVE_STRING_EQ(abs_path, result);
8383
}
8484

85-
CLOVE_TEST(ConvertToAbsolutePath) {
85+
CLOVE_TEST(ConvertAbsToAbsolutePath) {
8686
char* result = __clove_path_to_absolute("/abs/path/file.c");
8787

8888
#ifdef _WIN32
@@ -95,3 +95,16 @@ CLOVE_TEST(ConvertToAbsolutePath) {
9595
__clove_memory_free(result);
9696
}
9797

98+
CLOVE_TEST(ConvertRelToAbsolutePath) {
99+
char* result = __clove_path_to_absolute("rel/path/file.c");
100+
101+
#ifdef _WIN32
102+
const char* result_without_unit = result + 2; //e.g. c:\abs\path\file.c => \abs\path\file.c
103+
CLOVE_STRING_EQ("\\rel\\path\\file.c", result_without_unit);
104+
#else
105+
CLOVE_IS_TRUE(__clove_string_startswith(result, "/"));
106+
CLOVE_IS_TRUE(__clove_string_endswith(result, "/rel/path/file.c"));
107+
#endif
108+
109+
__clove_memory_free(result);
110+
}

0 commit comments

Comments
 (0)