]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
More changes to let BDB build without LDBM.
[openldap] / servers / slapd / attr.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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         ad_free( a->a_desc, 1 );
33         ber_bvecfree( a->a_vals );
34         free( a );
35 }
36
37 void
38 attrs_free( Attribute *a )
39 {
40         Attribute *next;
41
42         for( ; a != NULL ; a = next ) {
43                 next = a->a_next;
44                 attr_free( a );
45         }
46 }
47
48 Attribute *attr_dup( Attribute *a )
49 {
50         Attribute *tmp;
51
52         if( a == NULL) return NULL;
53
54         tmp = ch_malloc( sizeof(Attribute) );
55
56         if( a->a_vals != NULL ) {
57                 int i;
58
59                 for( i=0; a->a_vals[i] != NULL; i++ ) {
60                         /* EMPTY */ ;
61                 }
62
63                 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval*));
64
65                 for( i=0; a->a_vals[i] != NULL; i++ ) {
66                         tmp->a_vals[i] = ber_bvdup( a->a_vals[i] );
67
68                         if( tmp->a_vals[i] == NULL ) break;
69                 }
70
71                 tmp->a_vals[i] = NULL;
72
73         } else {
74                 tmp->a_vals = NULL;
75         }
76
77         tmp->a_desc = ad_dup( a->a_desc );
78         tmp->a_next = NULL;
79
80         return tmp;
81 }
82
83 Attribute *attrs_dup( Attribute *a )
84 {
85         Attribute *tmp, **next;
86
87         if( a == NULL ) return NULL;
88
89         tmp = NULL;
90         next = &tmp;
91
92         for( ; a != NULL ; a = a->a_next ) {
93                 *next = attr_dup( a );
94                 next = &((*next)->a_next);
95         }
96         *next = NULL;
97
98         return tmp;
99 }
100
101
102
103 /*
104  * attr_merge - merge the given type and value with the list of
105  * attributes in attrs.
106  * returns      0       everything went ok
107  *              -1      trouble
108  */
109
110 int
111 attr_merge(
112         Entry           *e,
113         AttributeDescription *desc,
114         struct berval   **vals )
115 {
116         Attribute       **a;
117
118         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
119                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
120                 {
121                         break;
122                 }
123         }
124
125         if ( *a == NULL ) {
126                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
127                 (*a)->a_desc = ad_dup( desc );
128                 (*a)->a_vals = NULL;
129                 (*a)->a_next = NULL;
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