changeset 28788:57a78a64de44

chg: add util function abortmsgerrno to print error with errno It's common to abortmsg with the errno information. Let's make a utility function for it.
author Jun Wu <quark@fb.com>
date Tue, 05 Apr 2016 17:25:39 +0100
parents ea86cdcd9b50
children 7f6e0a15189b
files contrib/chg/util.c contrib/chg/util.h
diffstat 2 files changed, 23 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/chg/util.c	Tue Apr 05 14:48:09 2016 +0100
+++ b/contrib/chg/util.c	Tue Apr 05 17:25:39 2016 +0100
@@ -7,6 +7,7 @@
  * GNU General Public License version 2 or any later version.
  */
 
+#include <errno.h>
 #include <signal.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -27,18 +28,33 @@
 	fprintf(fp, "\033[%sm", code);
 }
 
+static void vabortmsgerrno(int no, const char *fmt, va_list args)
+{
+	fsetcolor(stderr, "1;31");
+	fputs("chg: abort: ", stderr);
+	vfprintf(stderr, fmt, args);
+	if (no != 0)
+		fprintf(stderr, " (errno = %d, %s)", no, strerror(no));
+	fsetcolor(stderr, "");
+	fputc('\n', stderr);
+	exit(255);
+}
+
 void abortmsg(const char *fmt, ...)
 {
 	va_list args;
 	va_start(args, fmt);
-	fsetcolor(stderr, "1;31");
-	fputs("chg: abort: ", stderr);
-	vfprintf(stderr, fmt, args);
-	fsetcolor(stderr, "");
-	fputc('\n', stderr);
+	vabortmsgerrno(0, fmt, args);
 	va_end(args);
+}
 
-	exit(255);
+void abortmsgerrno(const char *fmt, ...)
+{
+	int no = errno;
+	va_list args;
+	va_start(args, fmt);
+	vabortmsgerrno(no, fmt, args);
+	va_end(args);
 }
 
 static int debugmsgenabled = 0;
--- a/contrib/chg/util.h	Tue Apr 05 14:48:09 2016 +0100
+++ b/contrib/chg/util.h	Tue Apr 05 17:25:39 2016 +0100
@@ -17,6 +17,7 @@
 #endif
 
 void abortmsg(const char *fmt, ...) PRINTF_FORMAT_;
+void abortmsgerrno(const char *fmt, ...) PRINTF_FORMAT_;
 
 void enablecolor(void);
 void enabledebugmsg(void);