diff --git a/libobs/obs-windows.c b/libobs/obs-windows.c index ef837e69b..f578e6d6c 100644 --- a/libobs/obs-windows.c +++ b/libobs/obs-windows.c @@ -508,22 +508,6 @@ void obs_key_combination_to_str(obs_key_combination_t combination, } } -static char *get_abs_path(const char *path) -{ - wchar_t wpath[512]; - wchar_t wabspath[512]; - char *abspath = NULL; - size_t len; - - len = os_utf8_to_wcs(path, 0, wpath, 512); - if (!len) - return NULL; - - if (_wfullpath(wabspath, wpath, 512) != NULL) - os_wcs_to_utf8_ptr(wabspath, 0, &abspath); - return abspath; -} - bool sym_initialize_called = false; void reset_win32_symbol_paths(void) @@ -561,7 +545,7 @@ void reset_win32_symbol_paths(void) if (!initialize_success) return; - abspath = get_abs_path("."); + abspath = os_get_abs_path_ptr("."); if (abspath) da_push_back(paths, &abspath); @@ -591,7 +575,7 @@ void reset_win32_symbol_paths(void) } if (!found) { - abspath = get_abs_path(path.array); + abspath = os_get_abs_path_ptr(path.array); if (abspath) da_push_back(paths, &abspath); } diff --git a/libobs/util/platform-nix.c b/libobs/util/platform-nix.c index d2f8830a0..c99c39beb 100644 --- a/libobs/util/platform-nix.c +++ b/libobs/util/platform-nix.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -241,6 +242,34 @@ bool os_file_exists(const char *path) return access(path, F_OK) == 0; } +size_t os_get_abs_path(const char *path, char *abspath, size_t size) +{ + size_t min_size = size < PATH_MAX ? size : PATH_MAX; + char newpath[PATH_MAX]; + int ret; + + if (!abspath) + return 0; + + if (!realpath(path, newpath)) + return 0; + + ret = snprintf(abspath, min_size, "%s", newpath); + return ret >= 0 ? ret : 0; +} + +char *os_get_abs_path_ptr(const char *path) +{ + char *ptr = bmalloc(512); + + if (!os_get_abs_path(path, ptr, 512)) { + bfree(ptr); + ptr = NULL; + } + + return ptr; +} + struct os_dir { const char *path; DIR *dir; diff --git a/libobs/util/platform-windows.c b/libobs/util/platform-windows.c index 8fa88ed60..8123ff338 100644 --- a/libobs/util/platform-windows.c +++ b/libobs/util/platform-windows.c @@ -263,6 +263,37 @@ bool os_file_exists(const char *path) return hFind != INVALID_HANDLE_VALUE; } +size_t os_get_abs_path(const char *path, char *abspath, size_t size) +{ + wchar_t wpath[512]; + wchar_t wabspath[512]; + size_t out_len = 0; + size_t len; + + if (!abspath) + return 0; + + len = os_utf8_to_wcs(path, 0, wpath, 512); + if (!len) + return 0; + + if (_wfullpath(wabspath, wpath, 512) != NULL) + out_len = os_wcs_to_utf8(wabspath, 0, abspath, size); + return out_len; +} + +char *os_get_abs_path_ptr(const char *path) +{ + char *ptr = bmalloc(512); + + if (!os_get_abs_path(path, ptr, 512)) { + bfree(ptr); + ptr = NULL; + } + + return ptr; +} + struct os_dir { HANDLE handle; WIN32_FIND_DATA wfd; diff --git a/libobs/util/platform.h b/libobs/util/platform.h index af3d03620..372d11b0a 100644 --- a/libobs/util/platform.h +++ b/libobs/util/platform.h @@ -101,6 +101,9 @@ EXPORT char *os_get_config_path_ptr(const char *name); EXPORT bool os_file_exists(const char *path); +EXPORT size_t os_get_abs_path(const char *path, char *abspath, size_t size); +EXPORT char *os_get_abs_path_ptr(const char *path); + struct os_dir; typedef struct os_dir os_dir_t;