1*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
2*7c478bd9Sstevel@tonic-gate
3*7c478bd9Sstevel@tonic-gate /*
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public
5*7c478bd9Sstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file
6*7c478bd9Sstevel@tonic-gate * except in compliance with the License. You may obtain a copy of
7*7c478bd9Sstevel@tonic-gate * the License at http://www.mozilla.org/NPL/
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * Software distributed under the License is distributed on an "AS
10*7c478bd9Sstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11*7c478bd9Sstevel@tonic-gate * implied. See the License for the specific language governing
12*7c478bd9Sstevel@tonic-gate * rights and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released
15*7c478bd9Sstevel@tonic-gate * March 31, 1998.
16*7c478bd9Sstevel@tonic-gate *
17*7c478bd9Sstevel@tonic-gate * The Initial Developer of the Original Code is Netscape
18*7c478bd9Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are
19*7c478bd9Sstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All
20*7c478bd9Sstevel@tonic-gate * Rights Reserved.
21*7c478bd9Sstevel@tonic-gate *
22*7c478bd9Sstevel@tonic-gate * Contributor(s):
23*7c478bd9Sstevel@tonic-gate */
24*7c478bd9Sstevel@tonic-gate /* charray.c - routines for dealing with char * arrays */
25*7c478bd9Sstevel@tonic-gate
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #include "ldap-int.h"
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate * Add s at the end of the array of strings *a.
31*7c478bd9Sstevel@tonic-gate * Return 0 for success, -1 for failure.
32*7c478bd9Sstevel@tonic-gate */
33*7c478bd9Sstevel@tonic-gate int
34*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_charray_add(char *** a,char * s)35*7c478bd9Sstevel@tonic-gate ldap_charray_add(
36*7c478bd9Sstevel@tonic-gate char ***a,
37*7c478bd9Sstevel@tonic-gate char *s
38*7c478bd9Sstevel@tonic-gate )
39*7c478bd9Sstevel@tonic-gate {
40*7c478bd9Sstevel@tonic-gate int n;
41*7c478bd9Sstevel@tonic-gate
42*7c478bd9Sstevel@tonic-gate if ( *a == NULL ) {
43*7c478bd9Sstevel@tonic-gate *a = (char **)NSLDAPI_MALLOC( 2 * sizeof(char *) );
44*7c478bd9Sstevel@tonic-gate if ( *a == NULL ) {
45*7c478bd9Sstevel@tonic-gate return -1;
46*7c478bd9Sstevel@tonic-gate }
47*7c478bd9Sstevel@tonic-gate n = 0;
48*7c478bd9Sstevel@tonic-gate } else {
49*7c478bd9Sstevel@tonic-gate for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
50*7c478bd9Sstevel@tonic-gate ; /* NULL */
51*7c478bd9Sstevel@tonic-gate }
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate *a = (char **)NSLDAPI_REALLOC( (char *) *a,
54*7c478bd9Sstevel@tonic-gate (n + 2) * sizeof(char *) );
55*7c478bd9Sstevel@tonic-gate if ( *a == NULL ) {
56*7c478bd9Sstevel@tonic-gate return -1;
57*7c478bd9Sstevel@tonic-gate }
58*7c478bd9Sstevel@tonic-gate }
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate (*a)[n++] = s;
61*7c478bd9Sstevel@tonic-gate (*a)[n] = NULL;
62*7c478bd9Sstevel@tonic-gate return 0;
63*7c478bd9Sstevel@tonic-gate }
64*7c478bd9Sstevel@tonic-gate
65*7c478bd9Sstevel@tonic-gate /*
66*7c478bd9Sstevel@tonic-gate * Add array of strings s at the end of the array of strings *a.
67*7c478bd9Sstevel@tonic-gate * Return 0 for success, -1 for failure.
68*7c478bd9Sstevel@tonic-gate */
69*7c478bd9Sstevel@tonic-gate int
70*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_charray_merge(char *** a,char ** s)71*7c478bd9Sstevel@tonic-gate ldap_charray_merge(
72*7c478bd9Sstevel@tonic-gate char ***a,
73*7c478bd9Sstevel@tonic-gate char **s
74*7c478bd9Sstevel@tonic-gate )
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate int i, n, nn;
77*7c478bd9Sstevel@tonic-gate
78*7c478bd9Sstevel@tonic-gate if ( (s == NULL) || (s[0] == NULL) )
79*7c478bd9Sstevel@tonic-gate return 0;
80*7c478bd9Sstevel@tonic-gate
81*7c478bd9Sstevel@tonic-gate for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
82*7c478bd9Sstevel@tonic-gate ; /* NULL */
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate for ( nn = 0; s[nn] != NULL; nn++ ) {
85*7c478bd9Sstevel@tonic-gate ; /* NULL */
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate
88*7c478bd9Sstevel@tonic-gate *a = (char **)NSLDAPI_REALLOC( (char *) *a,
89*7c478bd9Sstevel@tonic-gate (n + nn + 1) * sizeof(char *) );
90*7c478bd9Sstevel@tonic-gate if ( *a == NULL ) {
91*7c478bd9Sstevel@tonic-gate return -1;
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate
94*7c478bd9Sstevel@tonic-gate for ( i = 0; i < nn; i++ ) {
95*7c478bd9Sstevel@tonic-gate (*a)[n + i] = s[i];
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate (*a)[n + nn] = NULL;
98*7c478bd9Sstevel@tonic-gate return 0;
99*7c478bd9Sstevel@tonic-gate }
100*7c478bd9Sstevel@tonic-gate
101*7c478bd9Sstevel@tonic-gate void
102*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_charray_free(char ** array)103*7c478bd9Sstevel@tonic-gate ldap_charray_free( char **array )
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate char **a;
106*7c478bd9Sstevel@tonic-gate
107*7c478bd9Sstevel@tonic-gate if ( array == NULL ) {
108*7c478bd9Sstevel@tonic-gate return;
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate
111*7c478bd9Sstevel@tonic-gate for ( a = array; *a != NULL; a++ ) {
112*7c478bd9Sstevel@tonic-gate if ( *a != NULL ) {
113*7c478bd9Sstevel@tonic-gate NSLDAPI_FREE( *a );
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate NSLDAPI_FREE( (char *) array );
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate
119*7c478bd9Sstevel@tonic-gate int
120*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_charray_inlist(char ** a,char * s)121*7c478bd9Sstevel@tonic-gate ldap_charray_inlist(
122*7c478bd9Sstevel@tonic-gate char **a,
123*7c478bd9Sstevel@tonic-gate char *s
124*7c478bd9Sstevel@tonic-gate )
125*7c478bd9Sstevel@tonic-gate {
126*7c478bd9Sstevel@tonic-gate int i;
127*7c478bd9Sstevel@tonic-gate
128*7c478bd9Sstevel@tonic-gate if ( a == NULL )
129*7c478bd9Sstevel@tonic-gate return( 0 );
130*7c478bd9Sstevel@tonic-gate
131*7c478bd9Sstevel@tonic-gate for ( i = 0; a[i] != NULL; i++ ) {
132*7c478bd9Sstevel@tonic-gate if ( strcasecmp( s, a[i] ) == 0 ) {
133*7c478bd9Sstevel@tonic-gate return( 1 );
134*7c478bd9Sstevel@tonic-gate }
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate return( 0 );
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate
140*7c478bd9Sstevel@tonic-gate /*
141*7c478bd9Sstevel@tonic-gate * Duplicate the array of strings a, return NULL upon any memory failure.
142*7c478bd9Sstevel@tonic-gate */
143*7c478bd9Sstevel@tonic-gate char **
144*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_charray_dup(char ** a)145*7c478bd9Sstevel@tonic-gate ldap_charray_dup( char **a )
146*7c478bd9Sstevel@tonic-gate {
147*7c478bd9Sstevel@tonic-gate int i;
148*7c478bd9Sstevel@tonic-gate char **new;
149*7c478bd9Sstevel@tonic-gate
150*7c478bd9Sstevel@tonic-gate for ( i = 0; a[i] != NULL; i++ )
151*7c478bd9Sstevel@tonic-gate ; /* NULL */
152*7c478bd9Sstevel@tonic-gate
153*7c478bd9Sstevel@tonic-gate new = (char **)NSLDAPI_MALLOC( (i + 1) * sizeof(char *) );
154*7c478bd9Sstevel@tonic-gate if ( new == NULL ) {
155*7c478bd9Sstevel@tonic-gate return NULL;
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate
158*7c478bd9Sstevel@tonic-gate for ( i = 0; a[i] != NULL; i++ ) {
159*7c478bd9Sstevel@tonic-gate new[i] = nsldapi_strdup( a[i] );
160*7c478bd9Sstevel@tonic-gate if ( new[i] == NULL ) {
161*7c478bd9Sstevel@tonic-gate int j;
162*7c478bd9Sstevel@tonic-gate
163*7c478bd9Sstevel@tonic-gate for ( j = 0; j < i; j++ )
164*7c478bd9Sstevel@tonic-gate NSLDAPI_FREE( new[j] );
165*7c478bd9Sstevel@tonic-gate NSLDAPI_FREE( new );
166*7c478bd9Sstevel@tonic-gate return NULL;
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate new[i] = NULL;
170*7c478bd9Sstevel@tonic-gate
171*7c478bd9Sstevel@tonic-gate return( new );
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate
174*7c478bd9Sstevel@tonic-gate /*
175*7c478bd9Sstevel@tonic-gate * Tokenize the string str, return NULL upon any memory failure.
176*7c478bd9Sstevel@tonic-gate * XXX: on many platforms this function is not thread safe because it
177*7c478bd9Sstevel@tonic-gate * uses strtok().
178*7c478bd9Sstevel@tonic-gate */
179*7c478bd9Sstevel@tonic-gate char **
180*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_str2charray(char * str,char * brkstr)181*7c478bd9Sstevel@tonic-gate ldap_str2charray( char *str, char *brkstr )
182*7c478bd9Sstevel@tonic-gate /* This implementation fails if brkstr contains multibyte characters.
183*7c478bd9Sstevel@tonic-gate But it works OK if str is UTF-8 and brkstr is 7-bit ASCII.
184*7c478bd9Sstevel@tonic-gate */
185*7c478bd9Sstevel@tonic-gate {
186*7c478bd9Sstevel@tonic-gate char **res;
187*7c478bd9Sstevel@tonic-gate char *s;
188*7c478bd9Sstevel@tonic-gate int i;
189*7c478bd9Sstevel@tonic-gate
190*7c478bd9Sstevel@tonic-gate i = 1;
191*7c478bd9Sstevel@tonic-gate for ( s = str; *s; s++ ) {
192*7c478bd9Sstevel@tonic-gate if ( strchr( brkstr, *s ) != NULL ) {
193*7c478bd9Sstevel@tonic-gate i++;
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate }
196*7c478bd9Sstevel@tonic-gate
197*7c478bd9Sstevel@tonic-gate res = (char **)NSLDAPI_MALLOC( (i + 1) * sizeof(char *) );
198*7c478bd9Sstevel@tonic-gate if ( res == NULL ) {
199*7c478bd9Sstevel@tonic-gate return NULL;
200*7c478bd9Sstevel@tonic-gate }
201*7c478bd9Sstevel@tonic-gate i = 0;
202*7c478bd9Sstevel@tonic-gate for ( s = strtok( str, brkstr ); s != NULL; s = strtok( NULL,
203*7c478bd9Sstevel@tonic-gate brkstr ) ) {
204*7c478bd9Sstevel@tonic-gate res[i++] = nsldapi_strdup( s );
205*7c478bd9Sstevel@tonic-gate if ( res[i - 1] == NULL ) {
206*7c478bd9Sstevel@tonic-gate int j;
207*7c478bd9Sstevel@tonic-gate
208*7c478bd9Sstevel@tonic-gate for ( j = 0; j < (i - 1); j++ )
209*7c478bd9Sstevel@tonic-gate NSLDAPI_FREE( res[j] );
210*7c478bd9Sstevel@tonic-gate NSLDAPI_FREE( res );
211*7c478bd9Sstevel@tonic-gate return NULL;
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate }
214*7c478bd9Sstevel@tonic-gate res[i] = NULL;
215*7c478bd9Sstevel@tonic-gate
216*7c478bd9Sstevel@tonic-gate return( res );
217*7c478bd9Sstevel@tonic-gate }
218*7c478bd9Sstevel@tonic-gate
219*7c478bd9Sstevel@tonic-gate int
220*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_charray_position(char ** a,char * s)221*7c478bd9Sstevel@tonic-gate ldap_charray_position( char **a, char *s )
222*7c478bd9Sstevel@tonic-gate {
223*7c478bd9Sstevel@tonic-gate int i;
224*7c478bd9Sstevel@tonic-gate
225*7c478bd9Sstevel@tonic-gate for ( i = 0; a[i] != NULL; i++ ) {
226*7c478bd9Sstevel@tonic-gate if ( strcasecmp( s, a[i] ) == 0 ) {
227*7c478bd9Sstevel@tonic-gate return( i );
228*7c478bd9Sstevel@tonic-gate }
229*7c478bd9Sstevel@tonic-gate }
230*7c478bd9Sstevel@tonic-gate
231*7c478bd9Sstevel@tonic-gate return( -1 );
232*7c478bd9Sstevel@tonic-gate }
233