diff options
Diffstat (limited to 'client/libacron/private/helpers.c')
-rw-r--r-- | client/libacron/private/helpers.c | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/client/libacron/private/helpers.c b/client/libacron/private/helpers.c index 5a23e34..0cb452b 100644 --- a/client/libacron/private/helpers.c +++ b/client/libacron/private/helpers.c @@ -12,6 +12,10 @@ #include <errno.h> #include <stdio.h> +#ifdef WIN32 +#include <windows.h> +#endif + int alloc(void *existing, unsigned int count, unsigned int bytes, @@ -49,17 +53,45 @@ int strdup2(const char *str, char **out) { return AC_E_OK; } +#ifdef WIN32 +static _Thread_local LPTSTR err_buf = NULL; +#else static _Thread_local char err_buf[1024]; +#endif char *strerror2(int errnum) { #ifdef WIN32 - /* Win32 strerror(3) is thread-safe. */ - return strerror(errnum); + if (err_buf) { + LocalFree(err_buf); + err_buf = NULL; + } + FormatMessage( + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + errnum, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR) &err_buf, + 0, + NULL); + if (!err_buf) { + return "Unknown error"; + } #else int r; if ((r = strerror_r(errnum, err_buf, sizeof(err_buf) / sizeof(char)))) { snprintf(err_buf, 1024, "%d (%d)", errnum, r); } +#endif return err_buf; +} + +void helpers_cleanup(void) { +#ifdef WIN32 + if (err_buf) { + LocalFree(err_buf); + err_buf = NULL; + } #endif }
\ No newline at end of file |