]> git.ipfire.org Git - thirdparty/xz.git/commitdiff
xz: Add a function to print Windows specific error messages.
authorJia Tan <jiat0218@gmail.com>
Sat, 21 Oct 2023 12:32:05 +0000 (20:32 +0800)
committerJia Tan <jiat0218@gmail.com>
Mon, 29 Jan 2024 13:40:53 +0000 (21:40 +0800)
Native Windows C API functions do not use errno, but instead have to
call GetLastError(). There is not an easy way to convert this error
code into a helpful message, so this creates a wrapper around the
slightly complicated FormatMessage() function.

The new message_windows_error() function calls message_error() under the
hood, so it will set the exit status to 1.

src/xz/message.c
src/xz/message.h

index 7756b5b27eabb776b697681f0b9947ee405591a2..0aabbadf76cf79f03a11c55709254fcd95acbbdd 100644 (file)
@@ -806,6 +806,28 @@ message_signal_handler(void)
 }
 
 
+#ifdef _MSC_VER
+extern void
+message_windows_error(const char* message, DWORD error_code)
+{
+       char *error_message;
+
+       if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
+               | FORMAT_MESSAGE_FROM_SYSTEM
+               | FORMAT_MESSAGE_IGNORE_INSERTS,
+               NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+               (LPTSTR)&error_message, 0, NULL)) {
+               message_error("%s: %s", message, error_message);
+       }
+       else {
+               message_error("%s\n", message);
+       }
+
+       LocalFree(error_message);
+}
+#endif
+
+
 extern const char *
 message_strm(lzma_ret code)
 {
index 20381705afcbef69af18dc2072361b2abc01bb2b..a60d9693a9d019e6537d3c0759c47004d41e281f 100644 (file)
@@ -84,6 +84,20 @@ tuklib_attr_noreturn
 extern void message_signal_handler(void);
 
 
+#ifdef _MSC_VER
+/// \brief     Print an error message using a Windows specific error code
+///
+/// The function uses message_error() internally, so it will set the
+/// exit code to 1 after printing.
+///
+/// \param     message     Message describing where the error occurred
+/// \param     error_code  Error number from GetLastError()
+extern void
+message_windows_error(const char* message, DWORD error_code);
+
+#endif
+
+
 /// Convert lzma_ret to a string.
 extern const char *message_strm(lzma_ret code);