scsh-users
[Top] [All Lists]

get-host-name uses MAXHOSTNAMELEN

To: scsh-news@zurich.ai.mit.edu
Subject: get-host-name uses MAXHOSTNAMELEN
From: Andreas Voegele <voegelas@gmx.net>
Date: Sun, 01 Jun 2003 10:34:45 +0200
--=-=-=
Content-Disposition: inline

I'm trying to build scsh on a GNU/Hurd system.  Since POSIX does not
specify MAXHOSTNAMELEN the Hurd does not define this value, which is
used by s48_get_host_name() in c/unix/socket.c.

I've modified s48_get_host_name() so that the host name is temporarily
stored in a dynamically allocated variable.  The attached patch is
based on code from libinetutils/localhost.c (see
http://www.gnu.org/software/inetutils/).

s48_get_host_name() can be tested with the following statements:

$ scsh
> ,open sockets
> (get-host-name)

Of course, a definition like

#ifndef MAXHOSTNAMELEN
# define MAXHOSTNAMELEN 256
#endif

could simply be put into socket.c but the Hurd people recommend the
code from libinetutils/localhost.c.


--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment; filename=socket.c.diff
Content-Description: s48_get_host_name() without MAXHOSTNAMELEN

--- scsh-0.6.4.orig/c/unix/socket.c     2003-02-14 15:17:36.000000000 +0100
+++ scsh-0.6.4/c/unix/socket.c  2003-06-01 09:33:19.000000000 +0200
@@ -381,11 +381,38 @@
 static s48_value
 s48_get_host_name(void)
 {
-  char mbuff[MAXHOSTNAMELEN];
+  char         *mbuff = NULL;
+  size_t       mbuff_len = 0;
+  int          status = 0;
+  s48_value    name;
 
-  if (gethostname(mbuff, sizeof(mbuff)) < 0)
+  do {
+    char       *tmp;
+
+    mbuff_len += 256; /* Initial guess */
+    tmp = (char *) realloc(mbuff, mbuff_len);
+
+    if (tmp == NULL) {
+      free(mbuff);
+      s48_raise_os_error(ENOMEM);
+    }
+    else
+      mbuff = tmp;
+  } while (((status = gethostname(mbuff, mbuff_len)) == 0
+           && !memchr(mbuff, '\0', mbuff_len))
+#ifdef ENAMETOOLONG
+          || errno == ENAMETOOLONG
+#endif
+          );
+
+  if (status != 0 && errno != 0) {
+    /* gethostname failed, abort.  */
+    free(mbuff);
     s48_raise_os_error(errno);
-  
-  return s48_enter_string(mbuff);
+  }
+
+  name = s48_enter_string(mbuff);
+  free(mbuff);
+  return name;
 }
 

--=-=-=--

<Prev in Thread] Current Thread [Next in Thread>