commit 73e7eb7d1cfead096b0297377490aeaac95a73f5 from: Stefan Sperling date: Tue Dec 15 23:45:34 2020 UTC 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@ commit - 4cc6a5a5fa3d5a5351d59385e614a0a78d167987 commit + 73e7eb7d1cfead096b0297377490aeaac95a73f5 blob - d3127f6f9acf465b17b825b7deb3d42b7a42fe7d blob + 9244c2c00f8a56e7ef54417632a95c0f7aa36ddd --- include/got_error.h +++ include/got_error.h @@ -380,3 +380,10 @@ const struct got_error *got_error_uuid(uint32_t, const /* 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 *, ...); blob - f8cf59b20a799b2b360c647f8eb7f1c331257431 blob + 772c50ed2ea44fc86f4cb411b60f6bbf55ef0a88 --- lib/error.c +++ 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(); +}