commit 4cc6a5a5fa3d5a5351d59385e614a0a78d167987 from: Stefan Sperling date: Tue Dec 15 22:42:20 2020 UTC add got_error_from_errno_fmt() for more flexibility in error messages suggested by and ok millert commit - 748d5cab9a3d0d5d0edc28c8fd38430c684b1e55 commit + 4cc6a5a5fa3d5a5351d59385e614a0a78d167987 blob - 5fa8ffa0d2de4c2d61942eb2eed66a240149f6d0 blob + d3127f6f9acf465b17b825b7deb3d42b7a42fe7d --- include/got_error.h +++ include/got_error.h @@ -334,6 +334,14 @@ const struct got_error *got_error_from_errno2(const ch */ 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 blob - 6cbb7ca6b8a72218dbc5725fed24856910329b8f blob + f8cf59b20a799b2b360c647f8eb7f1c331257431 --- lib/error.c +++ lib/error.c @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -111,6 +112,25 @@ got_error_from_errno3(const char *prefix, const char * } 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;