]> git.sur5r.net Git - openldap/blob - servers/slapd/charray.c
Import unprotected strtok fix from -devel. Yes, you have to edit 8 files
[openldap] / servers / slapd / charray.c
1 /* charray.c - routines for dealing with char * arrays */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9
10 #include "slap.h"
11
12 void
13 charray_add(
14     char        ***a,
15     char        *s
16 )
17 {
18         int     n;
19
20         if ( *a == NULL ) {
21                 *a = (char **) ch_malloc( 2 * sizeof(char *) );
22                 n = 0;
23         } else {
24                 for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
25                         ;       /* NULL */
26                 }
27
28                 *a = (char **) ch_realloc( (char *) *a,
29                     (n + 2) * sizeof(char *) );
30         }
31
32         (*a)[n++] = ch_strdup(s);
33         (*a)[n] = NULL;
34 }
35
36 void
37 charray_merge(
38     char        ***a,
39     char        **s
40 )
41 {
42         int     i, n, nn;
43
44         for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) {
45                 ;       /* NULL */
46         }
47         for ( nn = 0; s[nn] != NULL; nn++ ) {
48                 ;       /* NULL */
49         }
50
51         *a = (char **) ch_realloc( (char *) *a, (n + nn + 1) * sizeof(char *) );
52
53         for ( i = 0; i < nn; i++ ) {
54                 (*a)[n + i] = ch_strdup(s[i]);
55         }
56         (*a)[n + nn] = NULL;
57 }
58
59 void
60 charray_free( char **array )
61 {
62         char    **a;
63
64         if ( array == NULL ) {
65                 return;
66         }
67
68         for ( a = array; *a != NULL; a++ ) {
69                 if ( *a != NULL ) {
70                         free( *a );
71                 }
72         }
73         free( (char *) array );
74 }
75
76 int
77 charray_inlist(
78     char        **a,
79     char        *s
80 )
81 {
82         int     i;
83
84         for ( i = 0; a[i] != NULL; i++ ) {
85                 if ( strcasecmp( s, a[i] ) == 0 ) {
86                         return( 1 );
87                 }
88         }
89
90         return( 0 );
91 }
92
93 char **
94 charray_dup( char **a )
95 {
96         int     i;
97         char    **new;
98
99         for ( i = 0; a[i] != NULL; i++ )
100                 ;       /* NULL */
101
102         new = (char **) ch_malloc( (i + 1) * sizeof(char *) );
103
104         for ( i = 0; a[i] != NULL; i++ ) {
105                 new[i] = ch_strdup( a[i] );
106         }
107         new[i] = NULL;
108
109         return( new );
110 }
111
112 char **
113 str2charray( char *str, char *brkstr )
114 {
115         char    **res;
116         char    *s;
117         int     i;
118
119         /* protect the input string from strtok */
120         str = ch_strdup( str );
121
122         i = 1;
123         for ( s = str; *s; s++ ) {
124                 if ( strchr( brkstr, *s ) != NULL ) {
125                         i++;
126                 }
127         }
128
129         res = (char **) ch_malloc( (i + 1) * sizeof(char *) );
130         i = 0;
131
132         pthread_mutex_lock(&strtok_mutex);
133
134         for ( s = strtok( str, brkstr ); s != NULL; s = strtok( NULL,
135             brkstr ) ) {
136                 res[i++] = ch_strdup( s );
137         }
138
139         pthread_mutex_unlock(&strtok_mutex);
140
141         res[i] = NULL;
142
143         free( str );
144         return( res );
145 }