public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Colin Cross <ccross@google.com>
To: elfutils-devel@sourceware.org
Cc: Colin Cross <ccross@google.com>
Subject: [PATCH] lib: Make error.c more like error(3)
Date: Fri, 10 Sep 2021 11:07:16 -0700	[thread overview]
Message-ID: <20210910180716.1464674-1-ccross@google.com> (raw)
In-Reply-To: <6098de6222cc54410231586581d08cf442e00f56.camel@klomp.org>

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/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 <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,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


  reply	other threads:[~2021-09-10 18:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-08 18:21 [PATCH] lib: Fix unused parameter warning in lib/error.c Colin Cross
2021-09-09 10:09 ` Mark Wielaard
2021-09-10 18:07   ` Colin Cross [this message]
2021-09-12 21:00     ` [PATCH] lib: Make error.c more like error(3) Mark Wielaard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210910180716.1464674-1-ccross@google.com \
    --to=ccross@google.com \
    --cc=elfutils-devel@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).