Ensure HOST_NAME_MAX as part of the gethostname module.
authorBruno Haible <bruno@clisp.org>
Sun, 2 Aug 2009 10:24:14 +0000 (12:24 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 2 Aug 2009 10:24:14 +0000 (12:24 +0200)
ChangeLog
doc/posix-headers/limits.texi
m4/gethostname.m4
tests/test-gethostname.c

index 7bab960..ee14046 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2009-08-02  Simon Josefsson  <simon@josefsson.org>
+            Bruno Haible  <bruno@clisp.org>
+
+       Ensure HOST_NAME_MAX as part of the gethostname module.
+       * m4/gethostname.m4 (gl_FUNC_GETHOSTNAME): On native Windows platforms,
+       define also HOST_NAME_MAX.
+       * tests/test-gethostname.c (main): Check also HOST_NAME_MAX.
+       * doc/posix-headers/limits.texi: Document the mingw problem.
+
 2009-08-02  Bruno Haible  <bruno@clisp.org>
 
        * lib/gethostname.c (gethostname): Fix handling of large len argument.
index 0b5e431..fa5c1eb 100644 (file)
@@ -3,10 +3,12 @@
 
 POSIX specification: @url{http://www.opengroup.org/susv3xbd/limits.h.html}
 
-Gnulib module: ---
+Gnulib module: gethostname
 
 Portability problems fixed by Gnulib:
 @itemize
+The @code{HOST_NAME_MAX} macro is not defined on some platforms:
+mingw.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index 620e023..8af4aa0 100644 (file)
@@ -1,9 +1,12 @@
-# gethostname.m4 serial 6
+# gethostname.m4 serial 7
 dnl Copyright (C) 2002, 2008, 2009 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
 
+# Ensure
+# - the gethostname() function,
+# - the HOST_NAME_MAX macro in <limits.h>.
 AC_DEFUN([gl_FUNC_GETHOSTNAME],
 [
   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
@@ -38,6 +41,16 @@ AC_DEFUN([gl_FUNC_GETHOSTNAME],
     HAVE_GETHOSTNAME=0
     gl_PREREQ_GETHOSTNAME
   fi
+
+  dnl Also provide HOST_NAME_MAX when <limits.h> lacks it.
+  if test "$gl_cv_w32_gethostname" = "yes"; then
+    # <http://msdn.microsoft.com/en-us/library/ms738527.aspx> says:
+    # "if a buffer of 256 bytes is passed in the name parameter and
+    # the namelen parameter is set to 256, the buffer size will always
+    # be adequate."
+    AC_DEFINE([HOST_NAME_MAX], [256],
+      [Define HOST_NAME_MAX when <limits.h> does not define it.])
+  fi
 ])
 
 # Prerequisites of lib/gethostname.c.
index ef50953..c1914fd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Free Software Foundation
+ * Copyright (C) 2008, 2009 Free Software Foundation
  * Written by Simon Josefsson.
  *
  * This program is free software: you can redistribute it and/or modify
 int
 main (int argc, char *argv[])
 {
-  char buf[2500];
+  char buf[HOST_NAME_MAX];
   int rc;
 
+  if (strlen (NOHOSTNAME) >= HOST_NAME_MAX)
+    {
+      printf ("HOST_NAME_MAX impossibly small?! %d\n", HOST_NAME_MAX);
+      return 2;
+    }
+
   strcpy (buf, NOHOSTNAME);
 
   rc = gethostname (buf, sizeof (buf));