diff options
Diffstat (limited to 'util/libreboot-utils/lib/string.c')
| -rw-r--r-- | util/libreboot-utils/lib/string.c | 73 |
1 files changed, 68 insertions, 5 deletions
diff --git a/util/libreboot-utils/lib/string.c b/util/libreboot-utils/lib/string.c index cb37c1ba..ea7ca30a 100644 --- a/util/libreboot-utils/lib/string.c +++ b/util/libreboot-utils/lib/string.c @@ -119,9 +119,11 @@ slen(const char *s, /* the one for nvmutil state is in state.c */ /* this one just exits */ void -err_no_cleanup(int nvm_errval, const char *msg, ...) +err_no_cleanup(int stfu, 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,18 +131,79 @@ 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); va_end(args); - fprintf(stderr, ": %s\n", strerror(errno)); + if (p != NULL) + fprintf(stderr, ": %s\n", strerror(errno)); + else + fprintf(stderr, "%s\n", strerror(errno)); 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; +} + + + + + + + + + + + + + |
