+}
+/*
+ * ldap_int_utils_init() should be called before any other function.
+ */
+
+void ldap_int_utils_init( void )
+{
+ static int done=0;
+ if (done)
+ return;
+ done=1;
+
+#ifdef LDAP_R_COMPILE
+
+#if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
+ ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
+#endif
+
+#if !defined( HAVE_GETHOSTBYNAME_R )
+ ldap_pvt_thread_mutex_init( &ldap_int_gethostbyname_mutex );
+#endif
+
+#if !defined( HAVE_GETHOSTBYADDR_R )
+ ldap_pvt_thread_mutex_init( &ldap_int_gethostbyaddr_mutex );
+#endif
+
+ /* call other module init functions here... */
+#endif
+}
+
+#if defined( NEED_COPY_HOSTENT )
+# undef NEED_SAFE_REALLOC
+#define NEED_SAFE_REALLOC
+
+static char *cpy_aliases( char ***tgtio, char *buf, char **src )
+{
+ int len;
+ char **tgt=*tgtio;
+ for( ; (*src) ; src++ ) {
+ len = strlen( *src ) + 1;
+ memcpy( buf, *src, len );
+ *tgt++=buf;
+ buf+=len;
+ }
+ *tgtio=tgt;
+ return buf;
+}
+
+static char *cpy_addresses( char ***tgtio, char *buf, char **src, int len )
+{
+ char **tgt=*tgtio;
+ for( ; (*src) ; src++ ) {
+ memcpy( buf, *src, len );
+ *tgt++=buf;
+ buf+=len;
+ }
+ *tgtio=tgt;
+ return buf;
+}
+
+static int copy_hostent( struct hostent *res, char **buf, struct hostent * src )
+{
+ char **p;
+ char **tp;
+ char *tbuf;
+ int name_len;
+ int n_alias;
+ int total_alias_len;
+ int n_addr;
+ int total_addr_len;
+ int total_len;
+
+ /* calculate the size needed for the buffer */
+ name_len = strlen( src->h_name ) + 1;
+
+ for( n_alias=total_alias_len=0, p=src->h_aliases; (*p) ; p++ ) {
+ total_alias_len += strlen( *p ) + 1;
+ n_alias++;
+ }