add got_error_fmt() got_error_fmt() could eventually replace got_error_path() which has already been used to construct errors with strings that are not actually paths... 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
diff --git a/include/got_error.h b/include/got_error.h
index d3127f6..9244c2c 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -380,3 +380,10 @@ const struct got_error *got_error_uuid(uint32_t, const char *);
/* Return an error with a path prefixed to the error message. */
const struct got_error *got_error_path(const char *, int);
+
+/*
+ * Return an error with an error message prefix built by vsnprintf(3)
+ * from the provided format string and the variable-length list of
+ * additional arguments.
+*/
+const struct got_error *got_error_fmt(int, const char *, ...);
diff --git a/lib/error.c b/lib/error.c
index f8cf59b..772c50e 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -212,3 +212,29 @@ got_error_path(const char *path, int code)
abort();
}
+
+const struct got_error *
+got_error_fmt(int code, const char *fmt, ...)
+{
+ static struct got_error err;
+ static char msg[PATH_MAX * 4 + 128];
+ char buf[PATH_MAX * 4];
+ va_list ap;
+ size_t i;
+
+ va_start(ap, fmt);
+ vsnprintf(buf, sizeof(buf), fmt, ap);
+ va_end(ap);
+
+ for (i = 0; i < nitems(got_errors); i++) {
+ if (code == got_errors[i].code) {
+ err.code = code;
+ snprintf(msg, sizeof(msg), "%s: %s", buf,
+ got_errors[i].msg);
+ err.msg = msg;
+ return &err;
+ }
+ }
+
+ abort();
+}