Improved ldap_int_strtok. If strtok_r does not exists, it will be worked

simulated with strspn and strpbrk. If strspn or strpbrk aren't there, they
will be supplied.
In addition, the memory-handling of the ldap_int_gethostby's is better now.
This commit is contained in:
Bart Hartgers 1999-01-14 15:19:29 +00:00
parent b7beec1663
commit f6a47058b6
2 changed files with 100 additions and 42 deletions

View file

@ -1254,6 +1254,8 @@ AC_CHECK_FUNCS( \
strtok \ strtok \
strtol \ strtol \
strtoul \ strtoul \
strspn \
strpbrk \
sysconf \ sysconf \
waitpid \ waitpid \
) )

View file

@ -30,12 +30,68 @@
#include "ldap-int.h" #include "ldap-int.h"
static int int_strspn( const char *str, const char *delim )
{
#if defined( HAVE_STRSPN )
return strspn( str, delim );
#else
int pos;
const char *p=delim;
for( pos=0; (*str) ; pos++,str++) {
if (*str!=*p)
for( p=delim; (*p) ; p++ ) {
if (*str==*p)
break;
}
if (*p=='\0')
return pos;
}
return pos;
#endif
}
static char *int_strpbrk( const char *str, const char *accept )
{
#if defined( HAVE_STRPBRK )
return strpbrk( str, accept );
#else
const char *p;
for( ; (*str) ; str++ ) {
for( p=accept; (*p) ; p++) {
if (*str==*p)
return str;
}
}
return NULL;
#endif
}
char *ldap_int_strtok( char *str, const char *delim, char **pos ) char *ldap_int_strtok( char *str, const char *delim, char **pos )
{ {
#ifdef HAVE_STRTOK_R #ifdef HAVE_STRTOK_R
return strtok_r(str, delim, pos); return strtok_r(str, delim, pos);
#else #else
return strtok(str, delim); char *p;
if (pos==NULL)
return NULL;
if (str==NULL) {
if (*pos==NULL)
return NULL;
str=*pos;
}
/* skip any initial delimiters */
str += int_strspn( str, delim );
if (*str == '\0')
return NULL;
p = int_strpbrk( str, delim );
if (p==NULL) {
*pos = NULL;
} else {
*p ='\0';
*pos = p+1;
}
return str;
#endif #endif
} }
@ -50,7 +106,8 @@ char *ldap_int_ctime( const time_t *tp, char *buf )
return ctime_r(tp,buf); return ctime_r(tp,buf);
# endif # endif
#else #else
return ctime(tp); memcpy( buf, ctime(tp), 26 );
return buf;
#endif #endif
} }
@ -75,11 +132,12 @@ int ldap_int_gethostbyname_a(
int *herrno_ptr ) int *herrno_ptr )
{ {
#ifdef HAVE_GETHOSTBYNAME_R #ifdef HAVE_GETHOSTBYNAME_R
int r; int r=-1;
int buflen=BUFSTART; int buflen=BUFSTART;
*buf = NULL;
if (safe_realloc( buf, buflen)) {
for(;buflen<BUFMAX;) { for(;buflen<BUFMAX;) {
if (safe_realloc( buf, buflen )==NULL)
return r;
r = gethostbyname_r( name, resbuf, *buf, r = gethostbyname_r( name, resbuf, *buf,
buflen, result, herrno_ptr ); buflen, result, herrno_ptr );
#ifdef NETDB_INTERNAL #ifdef NETDB_INTERNAL
@ -87,16 +145,14 @@ int ldap_int_gethostbyname_a(
(*herrno_ptr==NETDB_INTERNAL) && (*herrno_ptr==NETDB_INTERNAL) &&
(errno==ERANGE)) (errno==ERANGE))
{ {
if (safe_realloc( buf, buflen*=2 )) { buflen*=2;
continue; continue;
} }
}
#endif #endif
return r; return r;
} }
} return -1;
#else
#else /* gethostbyname() */
*result = gethostbyname( name ); *result = gethostbyname( name );
if (*result!=NULL) { if (*result!=NULL) {
@ -104,9 +160,9 @@ int ldap_int_gethostbyname_a(
} }
*herrno_ptr = h_errno; *herrno_ptr = h_errno;
#endif
return -1; return -1;
#endif
} }
int ldap_int_gethostbyaddr_a( int ldap_int_gethostbyaddr_a(
@ -119,10 +175,12 @@ int ldap_int_gethostbyaddr_a(
int *herrno_ptr ) int *herrno_ptr )
{ {
#ifdef HAVE_GETHOSTBYADDR_R #ifdef HAVE_GETHOSTBYADDR_R
int r; int r=-1;
int buflen=BUFSTART; int buflen=BUFSTART;
if (safe_realloc( buf, buflen)) { *buf = NULL;
for(;buflen<BUFMAX;) { for(;buflen<BUFMAX;) {
if (safe_realloc( buf, buflen )==NULL)
return r;
r = gethostbyaddr_r( addr, len, type, r = gethostbyaddr_r( addr, len, type,
resbuf, *buf, buflen, resbuf, *buf, buflen,
result, herrno_ptr ); result, herrno_ptr );
@ -131,21 +189,19 @@ int ldap_int_gethostbyaddr_a(
(*herrno_ptr==NETDB_INTERNAL) && (*herrno_ptr==NETDB_INTERNAL) &&
(errno==ERANGE)) (errno==ERANGE))
{ {
if (safe_realloc( buf, buflen*=2)) buflen*=2;
continue; continue;
} }
#endif #endif
return r; return r;
} }
} return -1;
#else /* gethostbyaddr() */ #else /* gethostbyaddr() */
*result = gethostbyaddr( addr, len, type ); *result = gethostbyaddr( addr, len, type );
if (*result!=NULL) { if (*result!=NULL) {
return 0; return 0;
} }
#endif
return -1; return -1;
#endif
} }