[RFC/PATCH 03/18] usage: implement error_errno() the same way as die_errno()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



die_errno() is very useful, but sometimes we don't want to
die after printing an error message and the error message
from errno. So let's implement error_errno() that does the
same thing as die_errno() except that it calls
error_routine() instead of die_routine().

Signed-off-by: Christian Couder <chriscool@xxxxxxxxxxxxx>
---
 git-compat-util.h |    1 +
 usage.c           |   28 ++++++++++++++++++++++++----
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/git-compat-util.h b/git-compat-util.h
index 490f969..32294cc 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -230,6 +230,7 @@ extern NORETURN void usage(const char *err);
 extern NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2)));
 extern NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
 extern NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
+extern int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
 extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
 
diff --git a/usage.c b/usage.c
index ec4cf53..f3ac869 100644
--- a/usage.c
+++ b/usage.c
@@ -69,10 +69,9 @@ void die(const char *err, ...)
 	va_end(params);
 }
 
-void die_errno(const char *fmt, ...)
+static void format_errno(char *fmt_with_err, size_t fmt_with_err_size,
+			 const char *fmt)
 {
-	va_list params;
-	char fmt_with_err[1024];
 	char str_error[256], *err;
 	int i, j;
 
@@ -90,13 +89,34 @@ void die_errno(const char *fmt, ...)
 		}
 	}
 	str_error[j] = 0;
-	snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, str_error);
+	snprintf(fmt_with_err, fmt_with_err_size, "%s: %s", fmt, str_error);
+}
+
+void die_errno(const char *fmt, ...)
+{
+	va_list params;
+	char fmt_with_err[1024];
+
+	format_errno(fmt_with_err, sizeof(fmt_with_err), fmt);
 
 	va_start(params, fmt);
 	die_routine(fmt_with_err, params);
 	va_end(params);
 }
 
+int error_errno(const char *fmt, ...)
+{
+	va_list params;
+	char fmt_with_err[1024];
+
+	format_errno(fmt_with_err, sizeof(fmt_with_err), fmt);
+
+	va_start(params, fmt);
+	error_routine(fmt_with_err, params);
+	va_end(params);
+	return -1;
+}
+
 int error(const char *err, ...)
 {
 	va_list params;
-- 
1.7.3.2.504.g59d466


--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]