17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
37c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
47c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
57c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
87c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
97c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
107c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
137c478bd9Sstevel@tonic-gate  * March 31, 1998.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
167c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
177c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
187c478bd9Sstevel@tonic-gate  * Rights Reserved.
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * Contributor(s):
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /* charray.c - routines for dealing with char * arrays */
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
25*1da57d55SToomas Soome #include "ldap-int.h"
26*1da57d55SToomas Soome 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Add s at the end of the array of strings *a.
297c478bd9Sstevel@tonic-gate  * Return 0 for success, -1 for failure.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate int
327c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_charray_add(char *** a,char * s)337c478bd9Sstevel@tonic-gate ldap_charray_add(
347c478bd9Sstevel@tonic-gate     char	***a,
357c478bd9Sstevel@tonic-gate     char	*s
367c478bd9Sstevel@tonic-gate )
377c478bd9Sstevel@tonic-gate {
387c478bd9Sstevel@tonic-gate 	int	n;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 	if ( *a == NULL ) {
417c478bd9Sstevel@tonic-gate 		*a = (char **)NSLDAPI_MALLOC( 2 * sizeof(char *) );
427c478bd9Sstevel@tonic-gate 		if ( *a == NULL ) {
437c478bd9Sstevel@tonic-gate 			return -1;
447c478bd9Sstevel@tonic-gate 		}
457c478bd9Sstevel@tonic-gate 		n = 0;
467c478bd9Sstevel@tonic-gate 	} else {
477c478bd9Sstevel@tonic-gate 		for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
487c478bd9Sstevel@tonic-gate 			;	/* NULL */
497c478bd9Sstevel@tonic-gate 		}
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 		*a = (char **)NSLDAPI_REALLOC( (char *) *a,
527c478bd9Sstevel@tonic-gate 		    (n + 2) * sizeof(char *) );
537c478bd9Sstevel@tonic-gate 		if ( *a == NULL ) {
547c478bd9Sstevel@tonic-gate 			return -1;
557c478bd9Sstevel@tonic-gate 		}
567c478bd9Sstevel@tonic-gate 	}
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	(*a)[n++] = s;
597c478bd9Sstevel@tonic-gate 	(*a)[n] = NULL;
607c478bd9Sstevel@tonic-gate 	return 0;
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * Add array of strings s at the end of the array of strings *a.
657c478bd9Sstevel@tonic-gate  * Return 0 for success, -1 for failure.
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate int
687c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_charray_merge(char *** a,char ** s)697c478bd9Sstevel@tonic-gate ldap_charray_merge(
707c478bd9Sstevel@tonic-gate     char	***a,
717c478bd9Sstevel@tonic-gate     char	**s
727c478bd9Sstevel@tonic-gate )
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	int	i, n, nn;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if ( (s == NULL) || (s[0] == NULL) )
777c478bd9Sstevel@tonic-gate 	    return 0;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
807c478bd9Sstevel@tonic-gate 		;	/* NULL */
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 	for ( nn = 0; s[nn] != NULL; nn++ ) {
837c478bd9Sstevel@tonic-gate 		;	/* NULL */
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	*a = (char **)NSLDAPI_REALLOC( (char *) *a,
877c478bd9Sstevel@tonic-gate 	    (n + nn + 1) * sizeof(char *) );
887c478bd9Sstevel@tonic-gate 	if ( *a == NULL ) {
897c478bd9Sstevel@tonic-gate 		return -1;
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	for ( i = 0; i < nn; i++ ) {
937c478bd9Sstevel@tonic-gate 		(*a)[n + i] = s[i];
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 	(*a)[n + nn] = NULL;
967c478bd9Sstevel@tonic-gate 	return 0;
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate void
1007c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_charray_free(char ** array)1017c478bd9Sstevel@tonic-gate ldap_charray_free( char **array )
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	char	**a;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if ( array == NULL ) {
1067c478bd9Sstevel@tonic-gate 		return;
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	for ( a = array; *a != NULL; a++ ) {
1107c478bd9Sstevel@tonic-gate 		if ( *a != NULL ) {
1117c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE( *a );
1127c478bd9Sstevel@tonic-gate 		}
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 	NSLDAPI_FREE( (char *) array );
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate int
1187c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_charray_inlist(char ** a,char * s)1197c478bd9Sstevel@tonic-gate ldap_charray_inlist(
1207c478bd9Sstevel@tonic-gate     char	**a,
1217c478bd9Sstevel@tonic-gate     char	*s
1227c478bd9Sstevel@tonic-gate )
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	int	i;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if ( a == NULL )
1277c478bd9Sstevel@tonic-gate 		return( 0 );
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	for ( i = 0; a[i] != NULL; i++ ) {
1307c478bd9Sstevel@tonic-gate 		if ( strcasecmp( s, a[i] ) == 0 ) {
1317c478bd9Sstevel@tonic-gate 			return( 1 );
1327c478bd9Sstevel@tonic-gate 		}
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	return( 0 );
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * Duplicate the array of strings a, return NULL upon any memory failure.
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate char **
1427c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_charray_dup(char ** a)1437c478bd9Sstevel@tonic-gate ldap_charray_dup( char **a )
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	int	i;
1467c478bd9Sstevel@tonic-gate 	char	**new;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	for ( i = 0; a[i] != NULL; i++ )
1497c478bd9Sstevel@tonic-gate 		;	/* NULL */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	new = (char **)NSLDAPI_MALLOC( (i + 1) * sizeof(char *) );
1527c478bd9Sstevel@tonic-gate 	if ( new == NULL ) {
1537c478bd9Sstevel@tonic-gate 		return NULL;
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	for ( i = 0; a[i] != NULL; i++ ) {
1577c478bd9Sstevel@tonic-gate 		new[i] = nsldapi_strdup( a[i] );
1587c478bd9Sstevel@tonic-gate 		if ( new[i] == NULL ) {
1597c478bd9Sstevel@tonic-gate 			int	j;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 			for ( j = 0; j < i; j++ )
1627c478bd9Sstevel@tonic-gate 			    NSLDAPI_FREE( new[j] );
1637c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE( new );
1647c478bd9Sstevel@tonic-gate 			return NULL;
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	new[i] = NULL;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	return( new );
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * Tokenize the string str, return NULL upon any memory failure.
1747c478bd9Sstevel@tonic-gate  * XXX: on many platforms this function is not thread safe because it
1757c478bd9Sstevel@tonic-gate  *	uses strtok().
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate char **
1787c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_str2charray(char * str,char * brkstr)1797c478bd9Sstevel@tonic-gate ldap_str2charray( char *str, char *brkstr )
1807c478bd9Sstevel@tonic-gate      /* This implementation fails if brkstr contains multibyte characters.
1817c478bd9Sstevel@tonic-gate         But it works OK if str is UTF-8 and brkstr is 7-bit ASCII.
1827c478bd9Sstevel@tonic-gate       */
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	char	**res;
1857c478bd9Sstevel@tonic-gate 	char	*s;
1867c478bd9Sstevel@tonic-gate 	int	i;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	i = 1;
1897c478bd9Sstevel@tonic-gate 	for ( s = str; *s; s++ ) {
1907c478bd9Sstevel@tonic-gate 		if ( strchr( brkstr, *s ) != NULL ) {
1917c478bd9Sstevel@tonic-gate 			i++;
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	res = (char **)NSLDAPI_MALLOC( (i + 1) * sizeof(char *) );
1967c478bd9Sstevel@tonic-gate 	if ( res == NULL ) {
1977c478bd9Sstevel@tonic-gate 		return NULL;
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 	i = 0;
2007c478bd9Sstevel@tonic-gate 	for ( s = strtok( str, brkstr ); s != NULL; s = strtok( NULL,
2017c478bd9Sstevel@tonic-gate 	    brkstr ) ) {
2027c478bd9Sstevel@tonic-gate 		res[i++] = nsldapi_strdup( s );
2037c478bd9Sstevel@tonic-gate 		if ( res[i - 1] == NULL ) {
2047c478bd9Sstevel@tonic-gate 			int	j;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 			for ( j = 0; j < (i - 1); j++ )
2077c478bd9Sstevel@tonic-gate 			    NSLDAPI_FREE( res[j] );
2087c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE( res );
2097c478bd9Sstevel@tonic-gate 			return NULL;
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	res[i] = NULL;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	return( res );
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate int
2187c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_charray_position(char ** a,char * s)2197c478bd9Sstevel@tonic-gate ldap_charray_position( char **a, char *s )
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	int     i;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	for ( i = 0; a[i] != NULL; i++ ) {
2247c478bd9Sstevel@tonic-gate 		if ( strcasecmp( s, a[i] ) == 0 ) {
2257c478bd9Sstevel@tonic-gate 			return( i );
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	return( -1 );
2307c478bd9Sstevel@tonic-gate }
231