]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
Renamed BVarray to BerVarray. Moved slapd:bvarray_{add,free} to
[openldap] / servers / slapd / attr.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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 #ifdef LDAP_DEBUG
26 static void at_index_print( void ) {};
27 #endif
28
29 void
30 attr_free( Attribute *a )
31 {
32         ber_bvarray_free( a->a_vals );
33         free( a );
34 }
35
36 void
37 attrs_free( Attribute *a )
38 {
39         Attribute *next;
40
41         for( ; a != NULL ; a = next ) {
42                 next = a->a_next;
43                 attr_free( a );
44         }
45 }
46
47 Attribute *attr_dup( Attribute *a )
48 {
49         Attribute *tmp;
50
51         if( a == NULL) return NULL;
52
53         tmp = ch_malloc( sizeof(Attribute) );
54
55         if( a->a_vals != NULL ) {
56                 int i;
57
58                 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
59                         /* EMPTY */ ;
60                 }
61
62                 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval));
63
64                 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
65                         ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
66                         if( tmp->a_vals[i].bv_val == NULL ) break;
67                 }
68
69                 tmp->a_vals[i].bv_val = NULL;
70
71         } else {
72                 tmp->a_vals = NULL;
73         }
74
75         tmp->a_desc = a->a_desc;
76         tmp->a_next = NULL;
77         tmp->a_flags = 0;
78
79         return tmp;
80 }
81
82 Attribute *attrs_dup( Attribute *a )
83 {
84         Attribute *tmp, **next;
85
86         if( a == NULL ) return NULL;
87
88         tmp = NULL;
89         next = &tmp;
90
91         for( ; a != NULL ; a = a->a_next ) {
92                 *next = attr_dup( a );
93                 next = &((*next)->a_next);
94         }
95         *next = NULL;
96
97         return tmp;
98 }
99
100
101
102 /*
103  * attr_merge - merge the given type and value with the list of
104  * attributes in attrs.
105  * returns      0       everything went ok
106  *              -1      trouble
107  */
108
109 int
110 attr_merge(
111         Entry           *e,
112         AttributeDescription *desc,
113         BerVarray       vals )
114 {
115         Attribute       **a;
116
117         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
118                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
119                 {
120                         break;
121                 }
122         }
123
124         if ( *a == NULL ) {
125                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
126                 (*a)->a_desc = desc;
127                 (*a)->a_vals = NULL;
128                 (*a)->a_next = NULL;
129                 (*a)->a_flags = 0;
130         }
131
132         return( value_add( &(*a)->a_vals, vals ) );
133 }
134
135 /*
136  * attrs_find - find attribute(s) by AttributeDescription
137  * returns next attribute which is subtype of provided description.
138  */
139
140 Attribute *
141 attrs_find(
142     Attribute   *a,
143         AttributeDescription *desc
144 )
145 {
146         for ( ; a != NULL; a = a->a_next ) {
147                 if ( is_ad_subtype( a->a_desc, desc ) ) {
148                         return( a );
149                 }
150         }
151
152         return( NULL );
153 }
154
155 /*
156  * attr_find - find attribute by type
157  */
158
159 Attribute *
160 attr_find(
161     Attribute   *a,
162         AttributeDescription *desc
163 )
164 {
165         for ( ; a != NULL; a = a->a_next ) {
166                 if ( ad_cmp( a->a_desc, desc ) == 0 )
167                 {
168                         return( a );
169                 }
170         }
171
172         return( NULL );
173 }
174
175 /*
176  * attr_delete - delete the attribute type in list pointed to by attrs
177  * return       0       deleted ok
178  *              1       not found in list a
179  *              -1      something bad happened
180  */
181
182 int
183 attr_delete(
184     Attribute   **attrs,
185         AttributeDescription *desc
186 )
187 {
188         Attribute       **a;
189
190         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
191                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
192                 {
193                         Attribute       *save = *a;
194                         *a = (*a)->a_next;
195                         attr_free( save );
196
197                         return LDAP_SUCCESS;
198                 }
199         }
200
201         return LDAP_NO_SUCH_ATTRIBUTE;
202 }
203