diff options
Diffstat (limited to 'util/libreboot-utils/lib/string.c')
| -rw-r--r-- | util/libreboot-utils/lib/string.c | 66 |
1 files changed, 63 insertions, 3 deletions
diff --git a/util/libreboot-utils/lib/string.c b/util/libreboot-utils/lib/string.c index cb37c1ba..2f2be5f3 100644 --- a/util/libreboot-utils/lib/string.c +++ b/util/libreboot-utils/lib/string.c @@ -122,6 +122,8 @@ void err_no_cleanup(int nvm_errval, const char *msg, ...) { va_list args; + int saved_errno = errno; + const char *p; #if defined(__OpenBSD__) && defined(OpenBSD) #if (OpenBSD) >= 509 @@ -129,11 +131,11 @@ err_no_cleanup(int nvm_errval, const char *msg, ...) fprintf(stderr, "pledge failure during exit"); #endif #endif - if (!errno) - errno = ECANCELED; + saved_errno = errno = ECANCELED; - fprintf(stderr, "nvmutil: "); + if ((p = getnvmprogname()) != NULL) + fprintf(stderr, "%s: ", p); va_start(args, msg); vfprintf(stderr, msg, args); @@ -144,3 +146,61 @@ err_no_cleanup(int nvm_errval, const char *msg, ...) exit(EXIT_FAILURE); } +const char * +getnvmprogname(void) +{ + static char *rval = NULL; + static char *p; + static int setname = 0; + + if (!setname) { + if ((rval = lbgetprogname(NULL)) == NULL) + return NULL; + + p = strrchr(rval, '/'); + if (p) + rval = p + 1; + + setname = 1; + } + + return rval; +} + +/* singleton. if string not null, + sets the string. after set, + will not set anymore. either + way, returns the string + */ +char * +lbgetprogname(char *argv0) +{ + static int setname = 0; + static char *progname = NULL; + size_t len; + + if (!setname) { + if (if_err(argv0 == NULL || *argv0 == '\0', EFAULT) || + slen(argv0, 4096, &len) < 0 || + (progname = malloc(len + 1)) == NULL) + return NULL; + + memcpy(progname, argv0, len + 1); + setname = 1; + } + + return progname; +} + + + + + + + + + + + + + |
