]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
add function prototypes
[openldap] / servers / slapd / attr.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* attr.c - routines for dealing with attributes */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #ifdef HAVE_FCNTL_H
13 #include <fcntl.h>
14 #endif
15
16 #include <ac/ctype.h>
17 #include <ac/errno.h>
18 #include <ac/socket.h>
19 #include <ac/string.h>
20 #include <ac/time.h>
21
22 #include "ldap_pvt.h"
23 #include "slap.h"
24
25 void
26 attr_free( Attribute *a )
27 {
28         ber_bvarray_free( a->a_vals );
29 #ifdef SLAP_NVALUES
30         if (a->a_nvals != a->a_vals) ber_bvarray_free( a->a_nvals );
31 #endif
32         free( a );
33 }
34
35 void
36 attrs_free( Attribute *a )
37 {
38         Attribute *next;
39
40         for( ; a != NULL ; a = next ) {
41                 next = a->a_next;
42                 attr_free( a );
43         }
44 }
45
46 Attribute *attr_dup( Attribute *a )
47 {
48         Attribute *tmp;
49
50         if( a == NULL) return NULL;
51
52         tmp = ch_malloc( sizeof(Attribute) );
53
54         if( a->a_vals != NULL ) {
55                 int i;
56
57                 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
58                         /* EMPTY */ ;
59                 }
60
61                 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval));
62                 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
63                         ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
64                         if( tmp->a_vals[i].bv_val == NULL ) break;
65                 }
66                 tmp->a_vals[i].bv_val = NULL;
67
68 #ifdef SLAP_NVALUES
69                 if( a->a_nvals != a->a_vals ) {
70                         tmp->a_nvals = ch_malloc((i+1) * sizeof(struct berval));
71                         for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
72                                 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
73                                 if( tmp->a_nvals[i].bv_val == NULL ) break;
74                         }
75                         tmp->a_nvals[i].bv_val = NULL;
76
77                 } else {
78                         tmp->a_nvals = tmp->a_vals;
79                 }
80 #endif
81
82         } else {
83                 tmp->a_vals = NULL;
84 #ifdef SLAP_NVALUES
85                 tmp->a_nvals = NULL;
86 #endif
87         }
88
89         tmp->a_desc = a->a_desc;
90         tmp->a_next = NULL;
91         tmp->a_flags = 0;
92
93         return tmp;
94 }
95
96 Attribute *attrs_dup( Attribute *a )
97 {
98         Attribute *tmp, **next;
99
100         if( a == NULL ) return NULL;
101
102         tmp = NULL;
103         next = &tmp;
104
105         for( ; a != NULL ; a = a->a_next ) {
106                 *next = attr_dup( a );
107                 next = &((*next)->a_next);
108         }
109         *next = NULL;
110
111         return tmp;
112 }
113
114
115
116 /*
117  * attr_merge - merge the given type and value with the list of
118  * attributes in attrs.
119  *
120  * For SLAP_NVALUES: nvals must be NULL if the attribute has no
121  * normalizer. In this case, a->a_nvals will be set equal to a->a_vals.
122  *
123  * returns      0       everything went ok
124  *              -1      trouble
125  */
126
127 int
128 attr_merge(
129         Entry           *e,
130         AttributeDescription *desc,
131         BerVarray       vals
132 #ifdef SLAP_NVALUES
133         , BerVarray     nvals
134 #endif
135 ) {
136         int rc;
137
138         Attribute       **a;
139
140         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
141                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
142                         break;
143                 }
144         }
145
146         if ( *a == NULL ) {
147                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
148                 (*a)->a_desc = desc;
149                 (*a)->a_vals = NULL;
150 #ifdef SLAP_NVALUES
151                 (*a)->a_nvals = NULL;
152 #endif
153                 (*a)->a_next = NULL;
154                 (*a)->a_flags = 0;
155         }
156
157         rc = value_add( &(*a)->a_vals, vals );
158
159 #ifdef SLAP_NVALUES
160         if( !rc && nvals ) rc = value_add( &(*a)->a_nvals, nvals );
161         else (*a)->a_nvals = (*a)->a_vals;
162 #endif
163
164         return rc;
165 }
166
167 int
168 attr_merge_one(
169         Entry           *e,
170         AttributeDescription *desc,
171         struct berval   *val
172 #ifdef SLAP_NVALUES
173         , struct berval *nval
174 #endif
175 ) {
176         int rc;
177         Attribute       **a;
178
179         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
180                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
181                         break;
182                 }
183         }
184
185         if ( *a == NULL ) {
186                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
187                 (*a)->a_desc = desc;
188                 (*a)->a_vals = NULL;
189 #ifdef SLAP_NVALUES
190                 (*a)->a_nvals = NULL;
191 #endif
192                 (*a)->a_next = NULL;
193                 (*a)->a_flags = 0;
194         }
195
196         rc = value_add_one( &(*a)->a_vals, val );
197
198 #ifdef SLAP_NVALUES
199         if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval );
200         else (*a)->a_nvals = (*a)->a_vals;
201 #endif
202         return rc;
203 }
204
205 /*
206  * attrs_find - find attribute(s) by AttributeDescription
207  * returns next attribute which is subtype of provided description.
208  */
209
210 Attribute *
211 attrs_find(
212     Attribute   *a,
213         AttributeDescription *desc
214 )
215 {
216         for ( ; a != NULL; a = a->a_next ) {
217                 if ( is_ad_subtype( a->a_desc, desc ) ) {
218                         return( a );
219                 }
220         }
221
222         return( NULL );
223 }
224
225 /*
226  * attr_find - find attribute by type
227  */
228
229 Attribute *
230 attr_find(
231     Attribute   *a,
232         AttributeDescription *desc
233 )
234 {
235         for ( ; a != NULL; a = a->a_next ) {
236                 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
237                         return( a );
238                 }
239         }
240
241         return( NULL );
242 }
243
244 /*
245  * attr_delete - delete the attribute type in list pointed to by attrs
246  * return       0       deleted ok
247  *              1       not found in list a
248  *              -1      something bad happened
249  */
250
251 int
252 attr_delete(
253     Attribute   **attrs,
254         AttributeDescription *desc
255 )
256 {
257         Attribute       **a;
258
259         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
260                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
261                         Attribute       *save = *a;
262                         *a = (*a)->a_next;
263                         attr_free( save );
264
265                         return LDAP_SUCCESS;
266                 }
267         }
268
269         return LDAP_NO_SUCH_ATTRIBUTE;
270 }
271