1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
/** BEGIN COPYRIGHT BLOCK
* This Program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; version 2 of the License.
*
* This Program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA.
*
* In addition, as a special exception, Red Hat, Inc. gives You the additional
* right to link the code of this Program with code not covered under the GNU
* General Public License ("Non-GPL Code") and to distribute linked combinations
* including the two, subject to the limitations in this paragraph. Non-GPL Code
* permitted under this exception must only link to the code of this Program
* through those well defined interfaces identified in the file named EXCEPTION
* found in the source code files (the "Approved Interfaces"). The files of
* Non-GPL Code may instantiate templates or use macros or inline functions from
* the Approved Interfaces without causing the resulting work to be covered by
* the GNU General Public License. Only Red Hat, Inc. may make changes or
* additions to the list of Approved Interfaces. You must obey the GNU General
* Public License in all respects for all of the Program code and other code used
* in conjunction with the Program except the Non-GPL Code covered by this
* exception. If you modify this file, you may extend this exception to your
* version of the file, but you are not obligated to do so. If you do not wish to
* provide this exception without modification, you must delete this exception
* statement from your version and license this file solely under the GPL without
* exception.
*
*
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
/* charray.c - routines for dealing with char * arrays */
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#ifndef _WIN32
#include <sys/socket.h>
#endif
#include "slap.h"
void
charray_add(
char ***a,
char *s
)
{
int n;
if ( *a == NULL ) {
*a = (char **) slapi_ch_malloc( 2 * sizeof(char *) );
n = 0;
} else {
for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
; /* NULL */
}
*a = (char **) slapi_ch_realloc( (char *) *a,
(n + 2) * sizeof(char *) );
}
/* At this point, *a may be different from the value it had when this
* function is called. Furthermore, *a[n] may contain an arbitrary
* value, such as a pointer to the middle of a unallocated area.
*/
#ifdef TEST_BELLATON
(*a)[n+1] = NULL;
(*a)[n] = s;
#endif
/* Putting code back so that thread conflict can be made visible */
(*a)[n++] = s;
(*a)[n] = NULL;
}
void
charray_merge(
char ***a,
char **s,
int copy_strs
)
{
int i, n, nn;
if ( (s == NULL) || (s[0] == NULL) )
return;
for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
; /* NULL */
}
for ( nn = 0; s[nn] != NULL; nn++ ) {
; /* NULL */
}
*a = (char **) slapi_ch_realloc( (char *) *a, (n + nn + 1) * sizeof(char *) );
for ( i = 0; i < nn; i++ ) {
if ( copy_strs ) {
(*a)[n + i] = slapi_ch_strdup( s[i] );
} else {
(*a)[n + i] = s[i];
}
}
(*a)[n + nn] = NULL;
}
/* Routines which don't pound on malloc. Don't interchange the arrays with the
* regular calls---they can end up freeing non-heap memory, which is wrong */
void
cool_charray_free( char **array )
{
slapi_ch_free((void**)&array);
}
/* Like strcpy, but returns a pointer to the next byte after the last one written to */
static char *strcpy_len(char *dest, char *source)
{
if('\0' == (*source)) {
return(dest);
}
do {
*dest++ = *source++;
} while (*source);
return dest;
}
char **
cool_charray_dup( char **a )
{
int i,size, num_strings;
char **newa;
char *p;
if ( a == NULL ) {
return( NULL );
}
for ( i = 0; a[i] != NULL; i++ )
; /* NULL */
num_strings = i;
size = (i + 1) * sizeof(char *);
for ( i = 0; a[i] != NULL; i++ ) {
size += strlen( a[i] ) + 1;
}
newa = (char **) slapi_ch_malloc( size );
p = (char *) &(newa[num_strings + 1]);
for ( i = 0; a[i] != NULL; i++ ) {
newa[i] = p;
p = strcpy_len(p, a[i] );
*p++ = '\0';
}
newa[i] = NULL;
return( newa );
}
void
charray_free( char **array )
{
char **a;
if ( array == NULL ) {
return;
}
for ( a = array; *a != NULL; a++ )
{
char *tmp= *a;
slapi_ch_free((void**)&tmp);
}
slapi_ch_free( (void**)&array );
}
/*
* charray_free version for plugins: there is a need for plugins to free
* the ch_arrays returned by functions like:
* slapi_get_supported_extended_ops_copy
* slapi_get_supported_saslmechanisms_copy
* slapi_get_supported_controls_copy
*/
void
slapi_ch_array_free( char **array )
{
charray_free (array);
}
/* case insensitive search */
int
charray_inlist(
char **a,
char *s
)
{
int i;
if ( a == NULL ) {
return( 0 );
}
for ( i = 0; a[i] != NULL; i++ ) {
if ( strcasecmp( s, a[i] ) == 0 ) {
return( 1 );
}
}
return( 0 );
}
/* case insensitive search covering non-ascii */
int
charray_utf8_inlist(
char **a,
char *s
)
{
int i;
if ( a == NULL ) {
return( 0 );
}
for ( i = 0; a[i] != NULL; i++ ) {
if (!slapi_UTF8CASECMP(a[i], s)) {
return( 1 );
}
}
return( 0 );
}
char **
charray_dup( char **a )
{
int i;
char **newa;
if ( a == NULL ) {
return( NULL );
}
for ( i = 0; a[i] != NULL; i++ )
; /* NULL */
newa = (char **) slapi_ch_malloc( (i + 1) * sizeof(char *) );
for ( i = 0; a[i] != NULL; i++ ) {
newa[i] = slapi_ch_strdup( a[i] );
}
newa[i] = NULL;
return( newa );
}
char **
str2charray( char *str, char *brkstr )
{
return( str2charray_ext( str, brkstr, 1 ));
}
/*
* extended version of str2charray lets you disallow
* duplicate values into the array.
*/
char **
str2charray_ext( char *str, char *brkstr, int allow_dups )
{
char **res;
char *s;
int i, j;
int dup_found = 0;
char * iter = NULL;
i = 1;
for ( s = str; *s; s++ ) {
if ( strchr( brkstr, *s ) != NULL ) {
i++;
}
}
res = (char **) slapi_ch_malloc( (i + 1) * sizeof(char *) );
i = 0;
for ( s = ldap_utf8strtok_r( str, brkstr , &iter); s != NULL;
s = ldap_utf8strtok_r( NULL, brkstr , &iter) ) {
dup_found = 0;
/* Always copy the first value into the array */
if ( (!allow_dups) && (i != 0) ) {
/* Check for duplicates */
for ( j = 0; j < i; j++ ) {
if ( strncmp( res[j], s, strlen( s ) ) == 0 ) {
dup_found = 1;
break;
}
}
}
if ( !dup_found )
res[i++] = slapi_ch_strdup( s );
}
res[i] = NULL;
return( res );
}
void
charray_print( char **a )
{
int i;
printf( "charray_print:\n");
for ( i = 0; a!= NULL && a[i] != NULL; i++ ) {
printf( "\t%s\n", a[i]);
}
}
/*
* Remove the char string from the array of char strings.
* Performs a case *insensitive* comparison!
* Just shunts the strings down to cover the deleted string.
* Doesn't free up the unused memory.
* Returns 1 if the entry found and removed, 0 if not.
*/
int
charray_remove(
char **a,
const char *s
)
{
int i;
int found= 0;
for ( i=0; a!= NULL && a[i] != NULL; i++ )
{
if ( !found && strcasecmp (a[i],s) == 0 )
{
found= 1;
}
if (found)
{
a[i]= a[i+1];
}
}
return found;
}
/*
* if c == NULL, a = a - b
* if c != NULL, *c = a - b
*/
#define SUBTRACT_DEL (char *)(-1)
void
charray_subtract(char **a, char **b, char ***c)
{
char **bp, **cp, **tmp;
char **p;
if (c)
tmp = *c = cool_charray_dup(a);
else
tmp = a;
for (cp = tmp; cp && *cp; cp++) {
for (bp = b; bp && *bp; bp++) {
if (!slapi_UTF8CASECMP(*cp, *bp)) {
slapi_ch_free((void **)&*cp);
*cp = SUBTRACT_DEL;
break;
}
}
}
for (cp = tmp; cp && *cp; cp++) {
if (*cp == SUBTRACT_DEL) {
for (p = cp+1; *p && *p == (char *)SUBTRACT_DEL; p++)
;
*cp = *p;
if (*p == NULL)
break;
else
*p = SUBTRACT_DEL;
}
}
}
int
charray_get_index(char **array, char *s)
{
int i;
for (i = 0; array && array[i]; i++)
{
if (!slapi_UTF8CASECMP(array[i], s))
return i;
}
return -1;
}
|