summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2026-09-05 19:18:44 -0400
committerAndrew Opalach <andrew@akon.city> 2026-09-05 19:18:44 -0400
commita27216c145b1b13ad863e49e5400778ad608640f (patch)
tree2531f2337c5a99af8c3f93676ddf0eb86e3715f4 /src/util
parent3a51988d764d54723288dc31a1779ece44cac1e9 (diff)
downloadlibnaunet-a27216c145b1b13ad863e49e5400778ad608640f.tar.gz
libnaunet-a27216c145b1b13ad863e49e5400778ad608640f.tar.bz2
libnaunet-a27216c145b1b13ad863e49e5400778ad608640f.zip
Fix Windows locale and error messages
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'src/util')
-rw-r--r--src/util/error.c29
-rw-r--r--src/util/file/file_stdio.c18
2 files changed, 38 insertions, 9 deletions
diff --git a/src/util/error.c b/src/util/error.c
index 46fd8e6..7f2e847 100644
--- a/src/util/error.c
+++ b/src/util/error.c
@@ -1,6 +1,9 @@
#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)
{
@@ -19,21 +22,31 @@ char *nn_strerror(s32 errnum)
#ifdef NAUNET_ON_WINDOWS
#include "../winwrap.h"
-// @TODO: GetLastError() should be used in common places like file_stdio.
+#include <wchar.h>
// https://stackoverflow.com/a/46104456
char *nn_win32_error_message(s32 err)
{
- /*
- DWORD ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+ DWORD ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- errorbuf,
+ 0,
+ errorbufw,
NNWT_ERROR_STR_MAXLEN,
NULL);
- return !ret ? NULL : errorbuf;
- */
- return 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
diff --git a/src/util/file/file_stdio.c b/src/util/file/file_stdio.c
index 5bdc7d9..6a30137 100644
--- a/src/util/file/file_stdio.c
+++ b/src/util/file/file_stdio.c
@@ -4,12 +4,27 @@
#ifdef NAUNET_ON_WINDOWS
#include "../../winwrap.h"
+#else
+#include <unistd.h>
#endif
#include "../error.h"
#include "file.h"
+static void check_error_description(s32 err)
+{
+#ifdef NAUNET_ON_WINDOWS
+ err = GetLastError();
+ char *strerror = nn_win32_error_message(err);
+ if (strerror) {
+ log_error("Error description: %s.", strerror);
+ }
+#else
+ log_error("Error description: %s.", nn_strerror(err));
+#endif
+}
+
static inline bool open_stdio_file(FILE **file, str *path, const char *mode)
{
char *c_str = al_str_to_c_str(path);
@@ -34,7 +49,8 @@ bool nn_file_open(struct nn_file *file, str *path, s32 flags)
{
const char *mode = (flags & NNWT_FILE_CREATE) ? "wb+" : "rb+";
if (!open_stdio_file(&file->file, path, mode)) {
- log_error("fopen(%.*s) failed (%d: %s).", al_str_x(path), errno, nn_strerror(errno));
+ log_error("fopen(%.*s) failed (%d).", al_str_x(path), errno);
+ check_error_description(errno);
return false;
}