[v2,08/16] lib: Implement error properly even when not HAVE_ERR_H

Message ID 20221217165213.152-9-luoyonggang@gmail.com
State Rejected
Headers
Series Patches for building with mingw/gcc msvc/clang-cl |

Commit Message

Yonggang Luo Dec. 17, 2022, 4:52 p.m. UTC
  on win32, there is no err.h

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
---
 lib/error.c  | 16 +++++++++++++++-
 lib/system.h |  4 +---
 2 files changed, 16 insertions(+), 4 deletions(-)
  

Comments

Mark Wielaard Feb. 23, 2023, 12:31 p.m. UTC | #1
Hi,

On Sun, 2022-12-18 at 00:52 +0800, Yonggang Luo via Elfutils-devel
wrote:
> on win32, there is no err.h

See also the discussion around v1 of this patch, I don't believe this
is a correct implementation of error. In particular it seems to not
print the errno string.
https://inbox.sourceware.org/elfutils-devel/dbcc7e30f35097e8d231e6a0a071a088f98a7d48.camel@klomp.org

Cheers,

Mark
  

Patch

diff --git a/lib/error.c b/lib/error.c
index 5186fc15..d4cbf0ff 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -28,12 +28,14 @@ 
 
 #include <config.h>
 
-#if !defined(HAVE_ERROR_H) && defined(HAVE_ERR_H)
+#if !defined(HAVE_ERROR_H)
 #include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
+#if defined(HAVE_ERR_H)
 #include <err.h>
+#endif
 
 unsigned int error_message_count = 0;
 
@@ -44,6 +46,7 @@  void error(int status, int errnum, const char *format, ...) {
   fflush (stdout);
 
   va_start(argp, format);
+#if defined(HAVE_ERR_H)
   if (status)
     {
       if (errnum)
@@ -64,6 +67,17 @@  void error(int status, int errnum, const char *format, ...) {
       else
         vwarnx (format, argp);
     }
+#else
+  if (errnum)
+    {
+      errno = errnum;
+    }
+  vfprintf(stderr, format, argp);
+  if (status)
+    {
+      _exit(status);
+    }
+#endif
   va_end(argp);
 
   fflush (stderr);
diff --git a/lib/system.h b/lib/system.h
index 561d3e03..7f9f2a91 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -50,11 +50,9 @@ 
 
 #if defined(HAVE_ERROR_H)
 #include <error.h>
-#elif defined(HAVE_ERR_H)
+#else
 extern int error_message_count;
 void error(int status, int errnum, const char *format, ...);
-#else
-#error "err.h or error.h must be available"
 #endif
 
 /* error (EXIT_FAILURE, ...) should be noreturn but on some systems it