]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
lib: Make error.c more like error(3)
authorColin Cross <ccross@google.com>
Fri, 10 Sep 2021 18:07:16 +0000 (11:07 -0700)
committerMark Wielaard <mark@klomp.org>
Sun, 12 Sep 2021 20:43:17 +0000 (22:43 +0200)
Fix some issues with the error reimplementation to make it match
the specification for error(3).

Flush stdout before printing to stderr.  Also flush stderr afterwards,
which is not specified in the man page for error(3), but is what
bionic does.

error(3) prints strerror(errnum) if and only if errnum is nonzero,
but verr prints strerror(errno) unconditionaly.  When errnum is nonzero
copy it to errno and use verr, and when it is not set use verrx that
doesn't print errno.

error(3) only exits if status is nonzero, but verr exits uncondtionally.
Use vwarn/vwarnx when status is zero, which don't exit.

Signed-off-by: Colin Cross <ccross@google.com>
lib/ChangeLog
lib/error.c

index 96eaa33012d4a8e5598ef1970ec0b00fd632ced4..c72452b179acaa8b1914ef451f4aa433e2226cd5 100644 (file)
@@ -1,3 +1,8 @@
+2021-09-10  Colin Cross <ccross@google.com>
+
+       * error.c (error): Call fflush on stdout and stderr. Setup errno and
+       call verr, verrx, vwarn or vwarnx based on status and errnum.
+
 2021-09-06  Dmitry V. Levin  <ldv@altlinux.org>
 
        * color.c (parse_opt): Replace asprintf followed by error(EXIT_FAILURE)
index 75e964fd29dd9eab935a3c93e9f243a0e54244f3..5186fc15e9d59907dc3e4f67bb8f4a6d09793929 100644 (file)
@@ -29,7 +29,9 @@
 #include <config.h>
 
 #if !defined(HAVE_ERROR_H) && defined(HAVE_ERR_H)
+#include <errno.h>
 #include <stdarg.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <err.h>
 
@@ -37,13 +39,37 @@ unsigned int error_message_count = 0;
 
 void error(int status, int errnum, const char *format, ...) {
   va_list argp;
+  int saved_errno = errno;
+
+  fflush (stdout);
 
   va_start(argp, format);
-  verr(status, format, argp);
+  if (status)
+    {
+      if (errnum)
+        {
+          errno = errnum;
+          verr (status, format, argp);
+        }
+      else
+        verrx (status, format, argp);
+    }
+  else
+    {
+      if (errnum)
+        {
+          errno = errnum;
+          vwarn (format, argp);
+        }
+      else
+        vwarnx (format, argp);
+    }
   va_end(argp);
 
-  if (status)
-    exit(status);
+  fflush (stderr);
+
   ++error_message_count;
+
+  errno = saved_errno;
 }
 #endif