[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
gethostname max len
From: |
Kevin Ryde |
Subject: |
gethostname max len |
Date: |
Mon, 22 Mar 2004 08:22:52 +1000 |
User-agent: |
Gnus/5.110002 (No Gnus v0.2) Emacs/21.3 (gnu/linux) |
* posix.c (scm_gethostname): Use sysconf(_SC_HOST_NAME_MAX) and
MAXHOSTNAMELEN when available.
It's possible MAXHOSTNAMELEN is enough in practice, but posix
specifies HOST_NAME_MAX instead of MAXHOSTNAMELEN, so it seems
worthwhile checking that too.
--- 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 <crypt.h>
#endif
+#ifdef HAVE_NETDB_H
+#include <netdb.h> /* for MAXHOSTNAMELEN on Solaris */
+#endif
+
+#ifdef HAVE_SYS_PARAM_H
+#include <sys/param.h> /* for MAXHOSTNAMELEN */
+#endif
+
#if HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#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)
{
- gethostname max len,
Kevin Ryde <=