diff options
author | Michael Smith <mikesmiffy128@gmail.com> | 2024-08-03 16:11:57 +0100 |
---|---|---|
committer | Michael Smith <mikesmiffy128@gmail.com> | 2024-08-22 00:01:06 +0100 |
commit | 44902cb49cd51e2bb2f1cef05cb3c0e799b83434 (patch) | |
tree | b64af413b10c32173aa535df22b5ee4162eb0290 /src/errmsg.h | |
parent | 01ec55edd9f767bebb1222c88f0e53e9cf8b2d59 (diff) |
Rework OS abstractions
- As much as possible avoid dragging system headers into translation
units. This should avoid namespace pollution and, hopefully, speed up
builds a little bit.
- Avoid leaning on the UCRT so much on Windows - prefer native win32
calls and native file handles except where doing so is inconvenient
(in particular, for stat(), which we might try and replace later).
- Also, switch from SystemFunction036 to ProcessPrng on Windows. This
requires us to generate a stub for bcryptprimitives.dll because
Microsoft haven't bothered to provide a link library, but the function
is better-documented and seems to be a more direct under-the-hood call
as well. Apparently it's what's used by the major web browsers these
days, which seems like a good indication it's stable and trusted.
- Lastly, remove a bunch of functions and macros and stuff that weren't
actually being used. It seems good to try and keep the scope of
OS-dependent stuff relatively contained and only add to it when
actually required.
Diffstat (limited to 'src/errmsg.h')
-rw-r--r-- | src/errmsg.h | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/src/errmsg.h b/src/errmsg.h index 17a4457..cdf5095 100644 --- a/src/errmsg.h +++ b/src/errmsg.h @@ -1,5 +1,5 @@ /* - * Copyright © 2022 Michael Smith <mikesmiffy128@gmail.com> + * Copyright © 2024 Michael Smith <mikesmiffy128@gmail.com> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -17,6 +17,15 @@ #ifndef INC_ERRMSG_H #define INC_ERRMSG_H +#ifdef _WIN32 +#include <stdarg.h> + +// note: redundant declspec avoids warnings if Windows.h was also included +__declspec(dllimport) unsigned long __stdcall FormatMessageA(unsigned long, + const void *, unsigned long, unsigned long, char *, unsigned long, + va_list *); +#endif + #include "con_.h" #include "os.h" @@ -48,14 +57,17 @@ extern const char msg_note[], msg_warn[], msg_error[]; #ifdef _WIN32 #define _errmsg_sys(msg, ...) do { \ - char _warnsys_buf[512]; \ - OS_WINDOWS_ERROR(_warnsys_buf); \ - _errmsg_msg(_ERRMSG_STR(MODULE_NAME), msg, __VA_ARGS__, _warnsys_buf); \ + char _errmsg_buf[512]; \ + FormatMessageA(/*FORMAT_MESSAGE_FROM_SYSTEM*/ 4096, 0, os_lasterror(), \ + /*MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)*/ 1024, _errmsg_buf, \ + sizeof(_errmsg_buf), 0); \ + _errmsg_msg(_ERRMSG_STR(MODULE_NAME), msg, __VA_ARGS__, _errmsg_buf); \ } while (0) #define _errmsg_dl _errmsg_sys #else #define _errmsg_sys _errmsg_std static inline const char *_errmsg_dlerror(void) { + char *dlerror(void); const char *e = dlerror(); if (!e) return "Unknown error"; // just in case, better avoid weirdness! return e; |