]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
19358e8948af6f6816e4845712f34b9158e651e9
[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         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
78         return tmp;
79 }
80
81 Attribute *attrs_dup( Attribute *a )
82 {
83         Attribute *tmp, **next;
84
85         if( a == NULL ) return NULL;
86
87         tmp = NULL;
88         next = &tmp;
89
90         for( ; a != NULL ; a = a->a_next ) {
91                 *next = attr_dup( a );
92                 next = &((*next)->a_next);
93         }
94         *next = NULL;
95
96         return tmp;
97 }
98
99
100
101 /*
102  * attr_merge - merge the given type and value with the list of
103  * attributes in attrs.
104  * returns      0       everything went ok
105  *              -1      trouble
106  */
107
108 int
109 attr_merge(
110         Entry           *e,
111         AttributeDescription *desc,
112         BVarray vals )
113 {
114         Attribute       **a;
115
116         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
117                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
118                 {
119                         break;
120                 }
121         }
122
123         if ( *a == NULL ) {
124                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
125                 (*a)->a_desc = desc;
126                 (*a)->a_vals = NULL;
127                 (*a)->a_next = NULL;
128         }
129
130         return( value_add( &(*a)->a_vals, vals ) );
131 }
132
133 /*
134  * attrs_find - find attribute(s) by AttributeDescription
135  * returns next attribute which is subtype of provided description.
136  */
137
138 Attribute *
139 attrs_find(
140     Attribute   *a,
141         AttributeDescription *desc
142 )
143 {
144         for ( ; a != NULL; a = a->a_next ) {
145                 if ( is_ad_subtype( a->a_desc, desc ) ) {
146                         return( a );
147                 }
148         }
149
150         return( NULL );
151 }
152
153 /*
154  * attr_find - find attribute by type
155  */
156
157 Attribute *
158 attr_find(
159     Attribute   *a,
160         AttributeDescription *desc
161 )
162 {
163         for ( ; a != NULL; a = a->a_next ) {
164                 if ( ad_cmp( a->a_desc, desc ) == 0 )
165                 {
166                         return( a );
167                 }
168         }
169
170         return( NULL );
171 }
172
173 /*
174  * attr_delete - delete the attribute type in list pointed to by attrs
175  * return       0       deleted ok
176  *              1       not found in list a
177  *              -1      something bad happened
178  */
179
180 int
181 attr_delete(
182     Attribute   **attrs,
183         AttributeDescription *desc
184 )
185 {
186         Attribute       **a;
187
188         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
189                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
190                 {
191                         Attribute       *save = *a;
192                         *a = (*a)->a_next;
193                         attr_free( save );
194
195                         return LDAP_SUCCESS;
196                 }
197         }
198
199         return LDAP_NO_SUCH_ATTRIBUTE;
200 }
201