From 5b01fa2dc19d327aabb9a443bbe541768b18bda2 Mon Sep 17 00:00:00 2001 From: OV2 Date: Tue, 15 Jan 2019 23:23:39 +0100 Subject: [PATCH] win32: make _twfullpath work with NULL destination --- win32/_tfwopen.cpp | 14 ++++++++++---- win32/_tfwopen.h | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/win32/_tfwopen.cpp b/win32/_tfwopen.cpp index ec6103aa..67f55923 100644 --- a/win32/_tfwopen.cpp +++ b/win32/_tfwopen.cpp @@ -48,12 +48,18 @@ extern "C" int _twopen(const char *filename, int oflag, int pmode) { return _wopen(Utf8ToWide(filename), oflag, pmode); } -extern "C" void _twfullpath(char* dst, const char* src, int len) { +extern "C" char *_twfullpath(char* dst, const char* src, int len) { wchar_t *resolved = _wfullpath(NULL, Utf8ToWide(src), MAX_PATH); - strncpy(dst, WideToUtf8(resolved), len); - free(resolved); + WideToUtf8 utf8resolved = WideToUtf8(resolved); + free(resolved); + if (dst == NULL) + { + len = strlen(utf8resolved) + 1; + dst = (char *)malloc(len); + } + strncpy(dst, utf8resolved, len); dst[len - 1] = '\0'; - return; + return dst; } #endif // UNICODE diff --git a/win32/_tfwopen.h b/win32/_tfwopen.h index 98d32cc9..3a519a20 100644 --- a/win32/_tfwopen.h +++ b/win32/_tfwopen.h @@ -18,7 +18,7 @@ extern "C" { FILE *_tfwopen(const char *filename, const char *mode ); int _twremove(const char *filename ); int _twopen(const char *filename, int oflag, int pmode); -void _twfullpath(char* dst, const char* src, int len); +char *_twfullpath(char* dst, const char* src, int len); #ifdef __cplusplus }