diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-03 17:57:27 +0000 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-03 17:57:27 +0000 |
| commit | 9b18d11878f41c0a146e3e46cf5d73cb7bbe5f76 (patch) | |
| tree | 64e57f382d2868f870c394f0866766da3fabd2c1 | |
| parent | 0a08045f92c944a0454eeba1f9ae05097297c3db (diff) | |
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 <leah@libreboot.org>
| -rw-r--r-- | util/nvmutil/nvmutil.c | 38 |
1 files changed, 37 insertions, 1 deletions
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 <sys/stat.h> -#include <err.h> #include <errno.h> #include <fcntl.h> +#include <stdarg.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> @@ -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) { |
