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 <konishi.ryusuke@gmail.com>
This commit is contained in:
Ryusuke Konishi 2026-01-19 23:04:37 +09:00
parent a421d4f6e7
commit 67c58a898d
2 changed files with 17 additions and 0 deletions

View File

@ -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 <errno.h>]])
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], [

View File

@ -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 <errno.h>
#define getprogname() ((const char *)program_invocation_short_name)
#else
#error "No way to retrieve the program name"
#endif
#endif /* HAVE_GETPROGNAME */
#endif /* __COMPAT_H__ */