[15/25] lib: Implement error properly even when not HAVE_ERR_H

Message ID 20221020182603.815-16-luoyonggang@gmail.com
State Superseded
Headers
Series Patches for building with mingw/gcc msvc/clang-cl |

Commit Message

Yonggang Luo Oct. 20, 2022, 6:25 p.m. UTC
  on win32, there is no err.h

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

Comments

Mark Wielaard Dec. 12, 2022, 3:37 p.m. UTC | #1
Hi,

On Fri, 2022-10-21 at 02:25 +0800, Yonggang Luo via Elfutils-devel
wrote:
> on win32, there is no err.h
> [...]
> +#else
> +  (void)status;
> +  vfprintf(stderr, format, argp);
> +#endif
>    va_end(argp);

That doesn't look like a valid implementation of error, it ignores
errno and doesn't exit when necessary.

Cheers,

Mark
  
lilydjwg--- via Elfutils-devel Dec. 16, 2022, 9:50 p.m. UTC | #2
On Mon, Dec 12, 2022 at 11:37 PM Mark Wielaard <mark@klomp.org> wrote:
>
> Hi,
>
> On Fri, 2022-10-21 at 02:25 +0800, Yonggang Luo via Elfutils-devel
> wrote:
> > on win32, there is no err.h
> > [...]
> > +#else
> > +  (void)status;
> > +  vfprintf(stderr, format, argp);
> > +#endif
> >    va_end(argp);
>
> That doesn't look like a valid implementation of error, it ignores
> errno and doesn't exit when necessary.

Do you mean it should  call `exit(status)` after the  error message is
printed?


>
> Cheers,
>
> Mark



--
         此致
礼
罗勇刚
Yours
    sincerely,
Yonggang Luo
  
Mark Wielaard Dec. 20, 2022, 3:10 p.m. UTC | #3
Hi,

On Sat, 2022-12-17 at 05:50 +0800, 罗勇刚(Yonggang Luo) wrote:
> On Mon, Dec 12, 2022 at 11:37 PM Mark Wielaard <mark@klomp.org>
> wrote:
> > On Fri, 2022-10-21 at 02:25 +0800, Yonggang Luo via Elfutils-devel
> > wrote:
> > > on win32, there is no err.h
> > > [...]
> > > +#else
> > > +  (void)status;
> > > +  vfprintf(stderr, format, argp);
> > > +#endif
> > >    va_end(argp);
> > 
> > That doesn't look like a valid implementation of error, it ignores
> > errno and doesn't exit when necessary.
> 
> Do you mean it should  call `exit(status)` after the  error message
> is
> printed?

Yes, if status != 0. Also errno should be printed (as a string).

Cheers,

Mark
  

Patch

diff --git a/lib/error.c b/lib/error.c
index 5186fc15..7924735e 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,10 @@  void error(int status, int errnum, const char *format, ...) {
       else
         vwarnx (format, argp);
     }
+#else
+  (void)status;
+  vfprintf(stderr, format, argp);
+#endif
   va_end(argp);
 
   fflush (stderr);
diff --git a/lib/system.h b/lib/system.h
index 7132cd6d..264781c2 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -51,11 +51,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