1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
1999-07-08 01:58:21 +08:00
|
|
|
/*
|
2003-01-04 04:20:47 +08:00
|
|
|
* Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
|
1999-07-08 01:58:21 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
/* charray.c - routines for dealing with char * arrays */
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
|
|
|
|
#include "ldap-int.h"
|
|
|
|
|
|
|
|
int
|
|
|
|
ldap_charray_add(
|
|
|
|
char ***a,
|
2003-04-30 00:34:37 +08:00
|
|
|
const char *s
|
1999-07-08 01:58:21 +08:00
|
|
|
)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if ( *a == NULL ) {
|
|
|
|
*a = (char **) LDAP_MALLOC( 2 * sizeof(char *) );
|
|
|
|
n = 0;
|
1999-08-30 09:24:33 +08:00
|
|
|
|
|
|
|
if( *a == NULL ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1999-07-08 01:58:21 +08:00
|
|
|
} else {
|
1999-08-30 09:24:33 +08:00
|
|
|
char **new;
|
|
|
|
|
1999-07-08 01:58:21 +08:00
|
|
|
for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
|
|
|
|
; /* NULL */
|
|
|
|
}
|
|
|
|
|
1999-08-30 09:24:33 +08:00
|
|
|
new = (char **) LDAP_REALLOC( (char *) *a,
|
1999-07-08 01:58:21 +08:00
|
|
|
(n + 2) * sizeof(char *) );
|
|
|
|
|
1999-08-30 09:24:33 +08:00
|
|
|
if( new == NULL ) {
|
|
|
|
/* caller is required to call ldap_charray_free(*a) */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*a = new;
|
1999-07-08 01:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
(*a)[n] = LDAP_STRDUP(s);
|
|
|
|
|
|
|
|
if( (*a)[n] == NULL ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*a)[++n] = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldap_charray_merge(
|
|
|
|
char ***a,
|
|
|
|
char **s
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int i, n, nn;
|
|
|
|
char **aa;
|
|
|
|
|
|
|
|
for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
|
|
|
|
; /* NULL */
|
|
|
|
}
|
|
|
|
for ( nn = 0; s[nn] != NULL; nn++ ) {
|
|
|
|
; /* NULL */
|
|
|
|
}
|
|
|
|
|
|
|
|
aa = (char **) LDAP_REALLOC( (char *) *a, (n + nn + 1) * sizeof(char *) );
|
|
|
|
|
2001-12-09 10:34:45 +08:00
|
|
|
if( aa == NULL ) {
|
1999-07-08 01:58:21 +08:00
|
|
|
return -1;
|
2001-12-09 10:34:45 +08:00
|
|
|
}
|
1999-07-08 01:58:21 +08:00
|
|
|
|
|
|
|
*a = aa;
|
|
|
|
|
|
|
|
for ( i = 0; i < nn; i++ ) {
|
|
|
|
(*a)[n + i] = LDAP_STRDUP(s[i]);
|
|
|
|
|
|
|
|
if( (*a)[n + i] == NULL ) {
|
|
|
|
for( --i ; i >= 0 ; i-- ) {
|
|
|
|
LDAP_FREE( (*a)[n + i] );
|
|
|
|
(*a)[n + i] = NULL;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(*a)[n + nn] = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ldap_charray_free( char **a )
|
|
|
|
{
|
|
|
|
char **p;
|
|
|
|
|
|
|
|
if ( a == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( p = a; *p != NULL; p++ ) {
|
|
|
|
if ( *p != NULL ) {
|
|
|
|
LDAP_FREE( *p );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LDAP_FREE( (char *) a );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldap_charray_inlist(
|
|
|
|
char **a,
|
2003-04-30 00:34:37 +08:00
|
|
|
const char *s
|
1999-07-08 01:58:21 +08:00
|
|
|
)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2002-09-25 02:20:59 +08:00
|
|
|
if( a == NULL ) return 0;
|
|
|
|
|
|
|
|
for ( i=0; a[i] != NULL; i++ ) {
|
1999-07-08 01:58:21 +08:00
|
|
|
if ( strcasecmp( s, a[i] ) == 0 ) {
|
2002-09-25 02:20:59 +08:00
|
|
|
return 1;
|
1999-07-08 01:58:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-25 02:20:59 +08:00
|
|
|
return 0;
|
1999-07-08 01:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char **
|
|
|
|
ldap_charray_dup( char **a )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char **new;
|
|
|
|
|
|
|
|
for ( i = 0; a[i] != NULL; i++ )
|
|
|
|
; /* NULL */
|
|
|
|
|
|
|
|
new = (char **) LDAP_MALLOC( (i + 1) * sizeof(char *) );
|
|
|
|
|
|
|
|
if( new == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( i = 0; a[i] != NULL; i++ ) {
|
|
|
|
new[i] = LDAP_STRDUP( a[i] );
|
|
|
|
|
|
|
|
if( new[i] == NULL ) {
|
|
|
|
for( --i ; i >= 0 ; i-- ) {
|
|
|
|
LDAP_FREE( new[i] );
|
|
|
|
}
|
|
|
|
LDAP_FREE( new );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new[i] = NULL;
|
|
|
|
|
|
|
|
return( new );
|
|
|
|
}
|
|
|
|
|
|
|
|
char **
|
2000-01-24 07:07:24 +08:00
|
|
|
ldap_str2charray( const char *str_in, const char *brkstr )
|
1999-07-08 01:58:21 +08:00
|
|
|
{
|
|
|
|
char **res;
|
2000-01-24 07:07:24 +08:00
|
|
|
char *str, *s;
|
1999-07-08 01:58:21 +08:00
|
|
|
char *lasts;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* protect the input string from strtok */
|
2000-01-24 07:07:24 +08:00
|
|
|
str = LDAP_STRDUP( str_in );
|
1999-07-08 01:58:21 +08:00
|
|
|
if( str == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 1;
|
|
|
|
for ( s = str; *s; s++ ) {
|
2000-01-24 07:07:24 +08:00
|
|
|
if ( ldap_utf8_strchr( brkstr, s ) != NULL ) {
|
1999-07-08 01:58:21 +08:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = (char **) LDAP_MALLOC( (i + 1) * sizeof(char *) );
|
|
|
|
|
|
|
|
if( res == NULL ) {
|
|
|
|
LDAP_FREE( str );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
2000-01-24 07:07:24 +08:00
|
|
|
for ( s = ldap_utf8_strtok( str, brkstr, &lasts );
|
1999-07-08 01:58:21 +08:00
|
|
|
s != NULL;
|
2000-01-24 07:07:24 +08:00
|
|
|
s = ldap_utf8_strtok( NULL, brkstr, &lasts ) )
|
1999-07-08 01:58:21 +08:00
|
|
|
{
|
|
|
|
res[i] = LDAP_STRDUP( s );
|
|
|
|
|
|
|
|
if(res[i] == NULL) {
|
|
|
|
for( --i ; i >= 0 ; i-- ) {
|
|
|
|
LDAP_FREE( res[i] );
|
|
|
|
}
|
|
|
|
LDAP_FREE( res );
|
|
|
|
LDAP_FREE( str );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
res[i] = NULL;
|
|
|
|
|
|
|
|
LDAP_FREE( str );
|
|
|
|
return( res );
|
|
|
|
}
|
2000-07-14 06:54:38 +08:00
|
|
|
|
|
|
|
char * ldap_charray2str( char **a, const char *sep )
|
|
|
|
{
|
|
|
|
char *s, **v, *p;
|
2000-09-17 06:46:04 +08:00
|
|
|
int len;
|
2000-07-14 06:54:38 +08:00
|
|
|
int slen;
|
|
|
|
|
|
|
|
if( sep == NULL ) sep = " ";
|
|
|
|
|
|
|
|
slen = strlen( sep );
|
2000-09-17 06:46:04 +08:00
|
|
|
len = 0;
|
2000-07-14 06:54:38 +08:00
|
|
|
|
|
|
|
for ( v = a; *v != NULL; v++ ) {
|
2000-09-17 06:46:04 +08:00
|
|
|
len += strlen( *v ) + slen;
|
2000-07-14 06:54:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( len == 0 ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-09-17 06:46:04 +08:00
|
|
|
/* trim extra sep len */
|
2000-07-14 06:54:38 +08:00
|
|
|
len -= slen;
|
|
|
|
|
2000-09-17 06:46:04 +08:00
|
|
|
s = LDAP_MALLOC ( len + 1 );
|
2000-07-14 06:54:38 +08:00
|
|
|
|
|
|
|
if ( s == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = s;
|
|
|
|
for ( v = a; *v != NULL; v++ ) {
|
|
|
|
if ( v != a ) {
|
|
|
|
strncpy( p, sep, slen );
|
|
|
|
p += slen;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen( *v );
|
|
|
|
strncpy( p, *v, len );
|
|
|
|
p += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
return s;
|
|
|
|
}
|