Get_current_directory() allocate buffer if needed

This commit is contained in:
Thomas Bernard 2019-01-15 14:14:10 +01:00
parent f90a2331dd
commit a69ad46902
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C
2 changed files with 18 additions and 1 deletions

View File

@ -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)
{

View File

@ -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);
///