Index: configure
===================================================================
RCS file: /cvsroot/pgsql/configure,v
retrieving revision 1.628
diff -c -c -r1.628 configure
*** configure	14 Jan 2009 18:10:21 -0000	1.628
--- configure	14 Jan 2009 22:00:25 -0000
***************
*** 20379,20384 ****
--- 20379,20440 ----
  
  
  
+ if test "$enable_thread_safety" = yes -a x"$ac_cv_func_gethostbyname_r" = x"yes" ; then
+   { echo "$as_me:$LINENO: checking gethostbyname_r argument count" >&5
+ echo $ECHO_N "checking gethostbyname_r argument count... $ECHO_C" >&6; }
+ # Check to see if 5-argument call generates an error
+   cat >conftest.$ac_ext <<_ACEOF
+ /* confdefs.h.  */
+ _ACEOF
+ cat confdefs.h >>conftest.$ac_ext
+ cat >>conftest.$ac_ext <<_ACEOF
+ /* end confdefs.h.  */
+ #include <netdb.h>
+ int
+ main ()
+ {
+ (void) gethostbyname_r (NULL, NULL, NULL, 0, NULL);
+   ;
+   return 0;
+ }
+ _ACEOF
+ rm -f conftest.$ac_objext
+ if { (ac_try="$ac_compile"
+ case "(($ac_try" in
+   *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+   *) ac_try_echo=$ac_try;;
+ esac
+ eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+   (eval "$ac_compile") 2>conftest.er1
+   ac_status=$?
+   grep -v '^ *+' conftest.er1 >conftest.err
+   rm -f conftest.er1
+   cat conftest.err >&5
+   echo "$as_me:$LINENO: \$? = $ac_status" >&5
+   (exit $ac_status); } && {
+ 	 test -z "$ac_c_werror_flag" ||
+ 	 test ! -s conftest.err
+        } && test -s conftest.$ac_objext; then
+ 
+ cat >>confdefs.h <<\_ACEOF
+ #define GETHOSTBYNAME_R_ARGCNT 3
+ _ACEOF
+  { echo "$as_me:$LINENO: result: 5" >&5
+ echo "${ECHO_T}5" >&6; }
+ else
+   echo "$as_me: failed program was:" >&5
+ sed 's/^/| /' conftest.$ac_ext >&5
+ 
+ 
+ cat >>confdefs.h <<\_ACEOF
+ #define GETHOSTBYNAME_R_ARGCNT 5
+ _ACEOF
+  { echo "$as_me:$LINENO: result: 3" >&5
+ echo "${ECHO_T}3" >&6; }
+ fi
+ 
+ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+ fi
  
  # This test makes sure that run tests work at all.  Sometimes a shared
  # library is found by the linker, but the runtime linker can't find it.
Index: configure.in
===================================================================
RCS file: /cvsroot/pgsql/configure.in,v
retrieving revision 1.587
diff -c -c -r1.587 configure.in
*** configure.in	14 Jan 2009 18:10:21 -0000	1.587
--- configure.in	14 Jan 2009 22:00:25 -0000
***************
*** 1417,1422 ****
--- 1417,1430 ----
  AC_SUBST(LDAP_LIBS_FE)
  AC_SUBST(LDAP_LIBS_BE)
  
+ if test "$enable_thread_safety" = yes -a x"$ac_cv_func_gethostbyname_r" = x"yes" ; then
+   AC_MSG_CHECKING([gethostbyname_r argument count])
+ # Check to see if 5-argument call generates an error
+   AC_TRY_COMPILE([#include <netdb.h>],
+   [(void) gethostbyname_r (NULL, NULL, NULL, 0, NULL);],
+   [AC_DEFINE(GETHOSTBYNAME_R_ARGCNT, 3, [Define to the number of arguments gethostbyname_r accepts]) AC_MSG_RESULT(5)],
+   [AC_DEFINE(GETHOSTBYNAME_R_ARGCNT, 5, [Define to the number of arguments gethostbyname_r accepts]) AC_MSG_RESULT(3)])
+ fi 
  
  # This test makes sure that run tests work at all.  Sometimes a shared
  # library is found by the linker, but the runtime linker can't find it.
Index: src/port/thread.c
===================================================================
RCS file: /cvsroot/pgsql/src/port/thread.c,v
retrieving revision 1.41
diff -c -c -r1.41 thread.c
*** src/port/thread.c	14 Jan 2009 21:18:30 -0000	1.41
--- src/port/thread.c	14 Jan 2009 22:00:28 -0000
***************
*** 12,17 ****
--- 12,22 ----
   *-------------------------------------------------------------------------
   */
  
+ /* Required to access struct hostent_data from c.h on older HPUX (e.g. 11) */
+ #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_STRERROR_R) && defined(__hpux)
+ #undef _XOPEN_SOURCE_EXTENDED
+ #endif
+ 
  #include "c.h"
  
  #include <pwd.h>
***************
*** 127,132 ****
--- 132,138 ----
  {
  #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETHOSTBYNAME_R)
  
+ #if GETHOSTBYNAME_R_ARGCNT == 5
  	/*
  	 * broken (well early POSIX draft) gethostbyname_r() which returns 'struct
  	 * hostent *'
***************
*** 134,140 ****
--- 140,163 ----
  	*result = gethostbyname_r(name, resultbuf, buffer, buflen, herrno);
  	return (*result == NULL) ? -1 : 0;
  #else
+ 	/* even older 3-argument version of gethostbyname_r() */
+     *result = NULL;
+     if (buflen < sizeof(struct hostent_data))
+     {
+          /* linux man page says this gets set when buffer is too small */
+          *herrno = ERANGE;
+          return -1;
+     }
+ 
+     if (gethostbyname_r(name, resultbuf, (struct hostent_data *)buffer))
+     {
+         *herrno = h_errno;
+         return -1;
+     }
+      *result = resultbuf;
+ #endif
  
+ #else
  	/* no gethostbyname_r(), just use gethostbyname() */
  	*result = gethostbyname(name);
  
