Commit 73e7eb7d1cfead096b0297377490aeaac95a73f5

Stefan Sperling 2020-12-15T23:45:34

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@

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();
+}