[PATCH] lib: Fix unused parameter warning in lib/error.c

2021-09-08 Thread Colin Cross via Elfutils-devel
Mark the errnum parameter with __attribute__((unused)).

Signed-off-by: Colin Cross 
---
 lib/error.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/error.c b/lib/error.c
index 75e964fd..651a0f3d 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -35,7 +35,7 @@
 
 unsigned int error_message_count = 0;
 
-void error(int status, int errnum, const char *format, ...) {
+void error(int status, int errnum __attribute__ ((unused)), const char 
*format, ...) {
   va_list argp;
 
   va_start(argp, format);
-- 
2.33.0.153.gba50c8fa24-goog



[PATCH] lib: Make error.c more like error(3)

2021-09-10 Thread Colin Cross via Elfutils-devel
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 
---
 lib/error.c | 27 ---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/lib/error.c b/lib/error.c
index 75e964fd..dba046ef 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -29,7 +29,9 @@
 #include 
 
 #if !defined(HAVE_ERROR_H) && defined(HAVE_ERR_H)
+#include 
 #include 
+#include 
 #include 
 #include 
 
@@ -37,13 +39,32 @@ 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
-- 
2.33.0.309.g3052b89438-goog