From 67c58a898db26131d22000ffda8d7a8f97900acb Mon Sep 17 00:00:00 2001 From: Ryusuke Konishi Date: Mon, 19 Jan 2026 23:04:37 +0900 Subject: [PATCH] build: add portable getprogname() support Add a compatibility layer for getprogname() to allow utilities to retrieve the short program name uniformly, removing the need for manual argv[0] parsing. The logic prioritizes the GNU extension variable 'program_invocation_short_name', falling back to getprogname. If neither is available, the build fails with an #error. Signed-off-by: Ryusuke Konishi --- configure.ac | 7 +++++++ include/compat.h | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/configure.ac b/configure.ac index 3d42057..1cccdf0 100644 --- a/configure.ac +++ b/configure.ac @@ -182,6 +182,13 @@ elif test -f /etc/mtab; then mtab_type=file fi +# Check program_invocation_short_name and getprogname() +AC_CHECK_DECLS([program_invocation_short_name], [], [], + [[#include ]]) + +AS_IF([test "x$ac_cv_have_decl_program_invocation_short_name" != "xyes"], + [AC_CHECK_FUNCS([getprogname])]) + # Check for conditional libraries and headers. AS_IF([test "${with_blkid}" = "yes"], [ NILFS_UTILS_PKG_CHECK([BLKID], [blkid], [ diff --git a/include/compat.h b/include/compat.h index 214bbca..c52229a 100644 --- a/include/compat.h +++ b/include/compat.h @@ -203,4 +203,14 @@ #endif /* HAVE_SYS_SYSMACROS_H */ +#ifndef HAVE_GETPROGNAME +#if defined(HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME) && \ + HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME +#include +#define getprogname() ((const char *)program_invocation_short_name) +#else +#error "No way to retrieve the program name" +#endif +#endif /* HAVE_GETPROGNAME */ + #endif /* __COMPAT_H__ */