diff options
author | Fabian Groffen | 2011-01-25 21:40:46 +0100 |
---|---|---|
committer | Karel Zak | 2011-01-31 15:51:06 +0100 |
commit | a804f444eb6c0a5232e37db3a58193fa2a549cd9 (patch) | |
tree | 027336f80ef190d98ce6c95662f5833b1ebad9af /include/c.h | |
parent | build-sys: enable lsblk and libmount for Linux only (diff) | |
download | kernel-qcow2-util-linux-a804f444eb6c0a5232e37db3a58193fa2a549cd9.tar.gz kernel-qcow2-util-linux-a804f444eb6c0a5232e37db3a58193fa2a549cd9.tar.xz kernel-qcow2-util-linux-a804f444eb6c0a5232e37db3a58193fa2a549cd9.zip |
provide a workaround if program_invocation_short_name is missing
Try some replacements, such as getexecname() on Solaris and __progname
on BSDs and Darwin. When not found, base program_invocation_short_name
on the source filename it is used in, as not to require argv[0] to be
passed along. This latter approach is not dynamic, but doesn't require
code changes for all places where program_invocation_short_name is used
now.
Signed-off-by: Fabian Groffen <grobian@gentoo.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'include/c.h')
-rw-r--r-- | include/c.h | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/include/c.h b/include/c.h index b37c44224..70e911ddc 100644 --- a/include/c.h +++ b/include/c.h @@ -82,5 +82,41 @@ static inline int dirfd(DIR *d) } #endif +#ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME +# ifdef HAVE___PROGNAME +extern char *__progname; +# define program_invocation_short_name __progname +# else +# include <string.h> +# ifdef HAVE_GETEXECNAME +# include <stdlib.h> +# define program_invocation_short_name \ + prog_inv_sh_nm_from_file(getexecname(), 0) +# else +# define program_invocation_short_name \ + prog_inv_sh_nm_from_file(__FILE__, 1) +# endif +static char prog_inv_sh_nm_buf[256]; +static inline char * +prog_inv_sh_nm_from_file(char *f, char stripext) +{ + char *t; + + if ((t = strrchr(f, '/')) != NULL) + t++; + else + t = f; + + strncpy(prog_inv_sh_nm_buf, t, sizeof(prog_inv_sh_nm_buf) - 1); + prog_inv_sh_nm_buf[sizeof(prog_inv_sh_nm_buf) - 1] = '\0'; + + if (stripext && (t = strrchr(prog_inv_sh_nm_buf, '.')) != NULL) + *t = '\0'; + + return prog_inv_sh_nm_buf; +} +# endif +#endif + #endif /* UTIL_LINUX_C_H */ |