]> err.no Git - util-linux/commitdiff
provide a workaround if program_invocation_short_name is missing
authorFabian Groffen <grobian@gentoo.org>
Tue, 25 Jan 2011 20:40:46 +0000 (21:40 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 31 Jan 2011 14:51:06 +0000 (15:51 +0100)
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>
configure.ac
include/c.h
shlibs/blkid/samples/partitions.c

index 59b55ea094c11d9744b2246835d82c39ffa08070..f27ba3e18c9eaae9ba015034969f9e9078fd3b80 100644 (file)
@@ -191,6 +191,7 @@ AC_CHECK_FUNCS(
        strtoull \
        sysconf \
        getdtablesize \
+       getexecname \
        getrlimit \
        srandom \
        setresgid \
@@ -248,6 +249,21 @@ no:no)
 esac
 
 
+AC_MSG_CHECKING(whether program_invocation_short_name is defined)
+AC_TRY_COMPILE([#include <argp.h>],
+               [program_invocation_short_name = "test";],
+               AC_DEFINE(HAVE_PROGRAM_INVOCATION_SHORT_NAME, 1,
+                         [Define if program_invocation_short_name is defined])
+               AC_MSG_RESULT(yes),
+               AC_MSG_RESULT(no))
+
+AC_MSG_CHECKING([whether __progname is defined])
+AC_LINK_IFELSE([AC_LANG_PROGRAM([extern char *__progname;],
+           [if (*__progname == 0) return;])],
+           AC_DEFINE(HAVE___PROGNAME, 1, [Define if __progname is defined])
+           AC_MSG_RESULT(yes),
+           AC_MSG_RESULT(no))
+
 dnl Static compilation
 m4_define([UTIL_STATIC_PROGRAMS], [losetup, mount, umount, fdisk, sfdisk, blkid])
 
index b37c44224a2b2b389053348ea4a87234ee218791..70e911ddc396d3520034719ed3d497388341b55d 100644 (file)
@@ -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 */
index db1b5ff2ee5d58c39da04a2c98024c8c0eedd0a0..8ee55996929e6ed76cda1a0f24ddfc9f3a1d1aca 100644 (file)
@@ -14,6 +14,7 @@
 #include <errno.h>
 
 #include <blkid.h>
+#include "c.h"
 
 int main(int argc, char *argv[])
 {