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