From a69ad46902e1efae0a21ac82a2d090149a17fd8c Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Tue, 15 Jan 2019 14:14:10 +0100 Subject: [PATCH] Get_current_directory() allocate buffer if needed --- src/io.c | 15 ++++++++++++++- src/io.h | 4 ++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/io.c b/src/io.c index 2941b507..d4e1e81c 100644 --- a/src/io.c +++ b/src/io.c @@ -886,7 +886,18 @@ char * Get_current_directory(char * buf, word * buf_unicode, size_t size) return buf; #elif defined(WIN32) - /// @TODO allocate buf if needed + if (buf == NULL) + { + size = (size_t)GetCurrentDirectoryA(0, buf); + if (size == 0) + return NULL; + buf = malloc(size); + if (buf == NULL) + { + GFX2_Log(GFX2_ERROR, "Failed to allocate %lu bytes.\n", (unsigned long)size); + return NULL; + } + } if (GetCurrentDirectoryA(size, buf) == 0) return NULL; if (buf_unicode != NULL) @@ -903,6 +914,8 @@ char * Get_current_directory(char * buf, word * buf_unicode, size_t size) return buf; #else char * ret = getcwd(buf, size); + if (ret == NULL) + GFX2_Log(GFX2_ERROR, "getcwd(%p, %lu) failed !\n", buf, (unsigned long)size); #ifdef ENABLE_FILENAMES_ICONV if (ret != NULL && buf_unicode != NULL) { diff --git a/src/io.h b/src/io.h index 1ef7085a..d1f900ef 100644 --- a/src/io.h +++ b/src/io.h @@ -183,6 +183,10 @@ void Release_lock_file(const char *file_directory); /// /// Return the current directory, equivalent to getcwd() +/// @param buf destination buffer. can be NULL +/// @param buf_unicode destination buffer for the unicode version of the path +/// @param size destination buffer size, ignored if buf is NULL +/// @return NULL for error, buf or a malloc'ed buffer char * Get_current_directory(char * buf, word * buf_unicode, size_t size); ///