add got_error_from_errno_fmt() for more flexibility in error messages suggested by and ok millert
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
diff --git a/include/got_error.h b/include/got_error.h
index 5fa8ffa..d3127f6 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -336,6 +336,14 @@ const struct got_error *got_error_from_errno3(const char *, const char *,
const char *);
/*
+ * Get a statically allocated error object with code GOT_ERR_ERRNO
+ * and an error message obtained from strerror(3), prefixed with a
+ * string built with vsnprintf(3) from the provided format string
+ * and the variable-length list of additional arguments.
+ */
+const struct got_error *got_error_from_errno_fmt(const char *, ...);
+
+/*
* Set errno to the specified error code and return a statically
* allocated error object with code GOT_ERR_ERRNO and an error
* message obtained from strerror(3), optionally prefixed with a
diff --git a/lib/error.c b/lib/error.c
index 6cbb7ca..f8cf59b 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -18,6 +18,7 @@
#include <errno.h>
#include <limits.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -111,6 +112,25 @@ got_error_from_errno3(const char *prefix, const char *prefix2,
}
const struct got_error *
+got_error_from_errno_fmt(const char *fmt, ...)
+{
+ static struct got_error err;
+ static char err_msg[PATH_MAX * 4 + 64];
+ char buf[PATH_MAX * 4];
+ va_list ap;
+
+ va_start(ap, fmt);
+ vsnprintf(buf, sizeof(buf), fmt, ap);
+ va_end(ap);
+
+ snprintf(err_msg, sizeof(err_msg), "%s: %s", buf, strerror(errno));
+
+ err.code = GOT_ERR_ERRNO;
+ err.msg = err_msg;
+ return &err;
+}
+
+const struct got_error *
got_error_set_errno(int code, const char *prefix)
{
errno = code;