aboutsummaryrefslogtreecommitdiff
path: root/lib/common
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-15 18:13:26 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-15 18:13:26 +0200
commit8bff2715bfdfd2e6ce0c707f1310b169d4e454d0 (patch)
tree50c4dfdb2e4daa5b8c94697261ff8081389b449c /lib/common
parentc9f37f069c6e294e58c64c9f5ad94f0bd3e2fd02 (diff)
libcompat: Add a helper to get/set OS error state
On Unix like OSes, this saves/restores errno, on Windows both errno and GetLastError state are saved/restored. This should make it simpler to preserve that across function calls. Additionally, while tracking down uses of GetLastError, some places where the error code was printed out directly where replaced with instances of w32_perror. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/common')
-rw-r--r--lib/common/src/perror.c32
1 files changed, 5 insertions, 27 deletions
diff --git a/lib/common/src/perror.c b/lib/common/src/perror.c
index 5f62748..edefc9e 100644
--- a/lib/common/src/perror.c
+++ b/lib/common/src/perror.c
@@ -8,22 +8,10 @@
#include <stdio.h>
-#if defined(_WIN32) || defined(__WINDOWS__)
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-#else
-#include <errno.h>
-#include <string.h>
-#endif
-
void sqfs_perror(const char *file, const char *action, int error_code)
{
+ os_error_t syserror;
const char *errstr;
-#if defined(_WIN32) || defined(__WINDOWS__)
- DWORD syserror = GetLastError();
-#else
- int syserror = errno;
-#endif
switch (error_code) {
case SQFS_ERROR_ALLOC:
@@ -82,6 +70,7 @@ void sqfs_perror(const char *file, const char *action, int error_code)
break;
}
+ syserror = get_os_error_state();
if (file != NULL)
fprintf(stderr, "%s: ", file);
@@ -89,24 +78,13 @@ void sqfs_perror(const char *file, const char *action, int error_code)
fprintf(stderr, "%s: ", action);
fprintf(stderr, "%s.\n", errstr);
+ set_os_error_state(syserror);
if (error_code == SQFS_ERROR_IO) {
#if defined(_WIN32) || defined(__WINDOWS__)
- LPVOID msg = NULL;
-
- FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, syserror,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR)&msg, 0, NULL);
-
- fprintf(stderr, "OS error: %s\n", (const char *)msg);
-
- if (msg != NULL)
- LocalFree(msg);
+ w32_perror("OS error");
#else
- fprintf(stderr, "OS error: %s\n", strerror(syserror));
+ perror("OS error");
#endif
}
}