aboutsummaryrefslogtreecommitdiff
path: root/include
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 /include
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 'include')
-rw-r--r--include/compat.h28
1 files changed, 28 insertions, 0 deletions
diff --git a/include/compat.h b/include/compat.h
index 7c9f937..f376665 100644
--- a/include/compat.h
+++ b/include/compat.h
@@ -265,4 +265,32 @@ int sqfs_tools_fprintf(FILE *strm, const char *fmt, ...) PRINTF_ATTRIB(2, 3);
#define putc sqfs_tools_fputc
#endif
+/****************************** error handling ******************************/
+
+#if defined(_WIN32) || defined(__WINDOWS__)
+typedef struct {
+ int unix_errno;
+ DWORD w32_errno;
+} os_error_t;
+
+static SQFS_INLINE os_error_t get_os_error_state(void)
+{
+ os_error_t out = { errno, GetLastError() };
+ return out;
+}
+
+static SQFS_INLINE void set_os_error_state(os_error_t err)
+{
+ SetLastError(err.w32_errno);
+ errno = err.unix_errno;
+}
+#else
+#include <errno.h>
+
+typedef int os_error_t;
+
+#define get_os_error_state() errno
+#define set_os_error_state(err) errno = (err)
+#endif
+
#endif /* COMPAT_H */