From 9b18d11878f41c0a146e3e46cf5d73cb7bbe5f76 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Tue, 3 Mar 2026 17:57:27 +0000 Subject: util/nvmutil: use own implementation of err and getprogname, written as getnvmprogname this removes a dependency on err.h, which is non-standard. the remaining code is posix-compliant, or ifdef'd where use of openbsd pledge is concerned - someone could theoretically define __OpenBSD__ that isn't and OpenBSD base maintainer, and then use nvmutil in it, but i so don't care about that evermore hypothetical individual. Signed-off-by: Leah Rowe --- util/nvmutil/nvmutil.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index 76229a94..4fbadfba 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -4,9 +4,9 @@ #include -#include #include #include +#include #include #include #include @@ -45,6 +45,8 @@ static void write_gbe_part(int); static void swap(int); static void usage(const char *); static void err_if(int); +static void err(int, const char *, ...); +static const char *getnvmprogname(void); static int set_err(int); #define NVM_CHECKSUM 0xBABA @@ -72,6 +74,7 @@ static int part_modified[2]; static const char *mac = NULL; static const char *rmac = "xx:xx:xx:xx:xx:xx"; static const char *fname = ""; +static const char *argv0; struct op { const char *str; @@ -92,6 +95,7 @@ static void (*cmd)(void) = NULL; int main(int argc, char *argv[]) { + argv0 = argv[0]; if (argc < 2) usage(argv[0]); fname = argv[1]; @@ -594,6 +598,38 @@ err_if(int x) err(set_err(ECANCELED), "%s", fname); } +static void +err(int rval, const char *msg, ...) +{ + va_list args; + + fprintf(stderr, "%s: ", getnvmprogname()); + + va_start(args, msg); + vfprintf(stderr, msg, args); + va_end(args); + + (void) set_err(ECANCELED); + fprintf(stderr, ": %s", strerror(errno)); + + fprintf(stderr, "\n"); + exit(rval); +} + +static const char * +getnvmprogname(void) +{ + const char *p; + + if ((argv0 == NULL) || (*argv0 == '\0')) + return ""; + + if ((p = strrchr(argv0, '/'))) + return p + 1; + else + return argv0; +} + static int set_err(int x) { -- cgit v1.2.1