From patchwork Tue Apr 5 14:21:17 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [2,of,3] chg: add util function abortmsge to print error with errno From: Jun Wu X-Patchwork-Id: 14369 Message-Id: <6b037b1f423c08785c7c.1459866077@x1c> To: Date: Tue, 5 Apr 2016 15:21:17 +0100 # HG changeset patch # User Jun Wu # Date 1459865259 -3600 # Tue Apr 05 15:07:39 2016 +0100 # Node ID 6b037b1f423c08785c7c1abe3751f3be1fe550ed # Parent 704d3febf4b7ea1063c2c3a58788de13fea31e79 chg: add util function abortmsge to print error with errno It's common to abort with the errno information. Let's make it a utility function. diff --git a/contrib/chg/util.c b/contrib/chg/util.c --- a/contrib/chg/util.c +++ b/contrib/chg/util.c @@ -7,6 +7,7 @@ * GNU General Public License version 2 or any later version. */ +#include #include #include #include @@ -27,13 +28,15 @@ fprintf(fp, "\033[%sm", code); } -void abortmsg(const char *fmt, ...) +void abortmsge(const char *fmt, ...) { va_list args; va_start(args, fmt); fsetcolor(stderr, "1;31"); fputs("chg: abort: ", stderr); vfprintf(stderr, fmt, args); + if (errno != 0) + fprintf(stderr, " (errno = %d, %s)", errno, strerror(errno)); fsetcolor(stderr, ""); fputc('\n', stderr); va_end(args); diff --git a/contrib/chg/util.h b/contrib/chg/util.h --- a/contrib/chg/util.h +++ b/contrib/chg/util.h @@ -16,7 +16,8 @@ #define PRINTF_FORMAT_ #endif -void abortmsg(const char *fmt, ...) PRINTF_FORMAT_; +void abortmsge(const char *fmt, ...) PRINTF_FORMAT_; +#define abortmsg(...) { errno = 0; abortmsge(__VA_ARGS__); } void enablecolor(void); void enabledebugmsg(void);