2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * util-int.c Various functions to replace missing threadsafe ones.
7 * Without the real *_r funcs, things will work, but won't be
10 * Written by Bart Hartgers.
12 * Copyright 1998, A. Hartgers, All rights reserved.
13 * This software is not subject to any license of Eindhoven University of
14 * Technology, since it was written in my spare time.
16 * Redistribution and use in source and binary forms are permitted only
17 * as authorized by the OpenLDAP Public License. A copy of this
18 * license is available at http://www.OpenLDAP.org/license.html or
19 * in file LICENSE in the top-level directory of the distribution.
27 #include <ac/socket.h>
28 #include <ac/string.h>
33 static int int_strspn( const char *str, const char *delim )
35 #if defined( HAVE_STRSPN )
36 return strspn( str, delim );
40 for( pos=0; (*str) ; pos++,str++) {
42 for( p=delim; (*p) ; p++ ) {
53 static char *int_strpbrk( const char *str, const char *accept )
55 #if defined( HAVE_STRPBRK )
56 return strpbrk( str, accept );
59 for( ; (*str) ; str++ ) {
60 for( p=accept; (*p) ; p++) {
69 char *ldap_int_strtok( char *str, const char *delim, char **pos )
72 return strtok_r(str, delim, pos);
83 /* skip any initial delimiters */
84 str += int_strspn( str, delim );
87 p = int_strpbrk( str, delim );
98 char *ldap_int_ctime( const time_t *tp, char *buf )
100 #if defined( HAVE_CTIME_R ) && defined( CTIME_R_NARGS )
101 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
102 choke me! nargs should have 2 or 3
103 # elif CTIME_R_NARGS > 2
104 return ctime_r(tp,buf,26);
106 return ctime_r(tp,buf);
109 memcpy( buf, ctime(tp), 26 );
114 #define BUFSTART 1024
115 #define BUFMAX (32*1024)
117 static char *safe_realloc( char **buf, int len )
120 tmpbuf = realloc( *buf, len );
127 int ldap_int_gethostbyname_a(
129 struct hostent *resbuf,
131 struct hostent **result,
134 #ifdef HAVE_GETHOSTBYNAME_R
138 for(;buflen<BUFMAX;) {
139 if (safe_realloc( buf, buflen )==NULL)
141 r = gethostbyname_r( name, resbuf, *buf,
142 buflen, result, herrno_ptr );
143 #ifdef NETDB_INTERNAL
145 (*herrno_ptr==NETDB_INTERNAL) &&
156 *result = gethostbyname( name );
162 *herrno_ptr = h_errno;
168 int ldap_int_gethostbyaddr_a(
172 struct hostent *resbuf,
174 struct hostent **result,
177 #ifdef HAVE_GETHOSTBYADDR_R
181 for(;buflen<BUFMAX;) {
182 if (safe_realloc( buf, buflen )==NULL)
184 r = gethostbyaddr_r( addr, len, type,
185 resbuf, *buf, buflen,
186 result, herrno_ptr );
187 #ifdef NETDB_INTERNAL
189 (*herrno_ptr==NETDB_INTERNAL) &&
199 #else /* gethostbyaddr() */
200 *result = gethostbyaddr( addr, len, type );