#include "error.h" static __thread char errorbuf[NNWT_ERROR_STR_MAXLEN]; #ifdef NAUNET_ON_WINDOWS static __thread wchar_t errorbufw[NNWT_ERROR_STR_MAXLEN]; #endif char *nn_strerror(s32 errnum) { #ifdef NAUNET_ON_WINDOWS strerror_s(errorbuf, NNWT_ERROR_STR_MAXLEN, errnum); #else #if (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE if (strerror_r(errnum, errorbuf, NNWT_ERROR_STR_MAXLEN) != 0) errorbuf[0] = '\0'; #else return strerror_r(errnum, errorbuf, NNWT_ERROR_STR_MAXLEN); #endif #endif return errorbuf; } #ifdef NAUNET_ON_WINDOWS #include "../winwrap.h" #include // https://stackoverflow.com/a/46104456 char *nn_win32_error_message(s32 err) { DWORD ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, 0, errorbufw, NNWT_ERROR_STR_MAXLEN, NULL); if (!ret) { return NULL; } errorbufw[ret] = L'\0'; mbstate_t mbstate = { 0 }; wchar_t *wc = errorbufw; size_t length = wcsrtombs(NULL, (const wchar_t **)&wc, 0, &mbstate); if (length == (size_t)-1) { return NULL; } length = wcsrtombs(errorbuf, (const wchar_t **)&wc, length, &mbstate); al_assert(length < NNWT_ERROR_STR_MAXLEN); errorbuf[length] = L'\0'; return errorbuf; } #endif