From 1ee4dff7246949cbebef884d84ccdc127450bee0 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Wed, 17 Oct 2012 20:16:06 +0100 Subject: [PATCH] include/c: add get_hostname_max() inline function Using sysconf(_SC_HOST_NAME_MAX) does not work everywhere. At such even header values for hostname length should be preferred, and if nothing else works guess the maxium being 64 bytes. Based-on-code-by: Karel Zak CC: Mike Frysinger References: http://www.spinics.net/lists/util-linux-ng/msg07050.html Signed-off-by: Sami Kerola Signed-off-by: Karel Zak --- include/c.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/c.h b/include/c.h index 64c0138f27..e15a994d71 100644 --- a/include/c.h +++ b/include/c.h @@ -234,6 +234,24 @@ static inline int dirfd(DIR *d) # endif #endif +/* + * MAXHOSTNAMELEN replacement + */ +static inline size_t get_hostname_max(void) +{ + long len = sysconf(_SC_HOST_NAME_MAX); + + if (0 < len) + return len; + +#ifdef MAXHOSTNAMELEN + return MAXHOSTNAMELEN; +#elif HOST_NAME_MAX + return HOST_NAME_MAX; +#endif + return 64; +} + /* * Constant strings for usage() functions. For more info see * Documentation/howto-usage-function.txt and sys-utils/arch.c -- 2.47.3