--- posix.c.~1.127.~ 2004-03-18 14:02:04.000000000 +1000 +++ posix.c 2004-03-22 08:19:40.000000000 +1000 @@ -114,6 +114,14 @@ # include #endif +#ifdef HAVE_NETDB_H +#include /* for MAXHOSTNAMELEN on Solaris */ +#endif + +#ifdef HAVE_SYS_PARAM_H +#include /* for MAXHOSTNAMELEN */ +#endif + #if HAVE_SYS_RESOURCE_H # include #endif @@ -1746,12 +1754,31 @@ "Return the host name of the current processor.") #define FUNC_NAME s_scm_gethostname { - /* 256 is for Solaris, under Linux ENAMETOOLONG is returned if not - large enough. */ - int len = 256, res, save_errno; + int len, res, save_errno; char *p = scm_malloc (len); SCM name; + /* Default 256 is for Solaris, under Linux ENAMETOOLONG is returned if not + large enough. */ + len = 256; + + /* various systems define MAXHOSTNAMELEN (including Solaris in fact) */ +#ifdef MAXHOSTNAMELEN + len = MAXHOSTNAMELEN; +#endif + + /* POSIX specifies the HOST_NAME_MAX system parameter for the max size, + which may reflect a particular kernel configuration. + Must watch out for this existing but giving -1, as happens for instance + in gnu/linux glibc 2.3.2. */ +#if HAVE_SYSCONF && defined (_SC_HOST_NAME_MAX) + { + long n = sysconf (_SC_HOST_NAME_MAX); + if (n != -1L) + len = n; + } +#endif + res = gethostname (p, len); while (res == -1 && errno == ENAMETOOLONG) {