]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
d5b9e3a0780acf2afe41d2cc18c4103426becd48
[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         ber_bvecfree( 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] != 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] != NULL; i++ ) {
65                         tmp->a_vals[i] = ber_bvdup( a->a_vals[i] );
66
67                         if( tmp->a_vals[i] == NULL ) break;
68                 }
69
70                 tmp->a_vals[i] = NULL;
71
72         } else {
73                 tmp->a_vals = NULL;
74         }
75
76         tmp->a_desc = a->a_desc;
77         tmp->a_next = NULL;
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         struct berval   **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         }
130
131         return( value_add( &(*a)->a_vals, vals ) );
132 }
133
134 /*
135  * attrs_find - find attribute(s) by AttributeDescription
136  * returns next attribute which is subtype of provided description.
137  */
138
139 Attribute *
140 attrs_find(
141     Attribute   *a,
142         AttributeDescription *desc
143 )
144 {
145         for ( ; a != NULL; a = a->a_next ) {
146                 if ( is_ad_subtype( a->a_desc, desc ) ) {
147                         return( a );
148                 }
149         }
150
151         return( NULL );
152 }
153
154 /*
155  * attr_find - find attribute by type
156  */
157
158 Attribute *
159 attr_find(
160     Attribute   *a,
161         AttributeDescription *desc
162 )
163 {
164         for ( ; a != NULL; a = a->a_next ) {
165                 if ( ad_cmp( a->a_desc, desc ) == 0 )
166                 {
167                         return( a );
168                 }
169         }
170
171         return( NULL );
172 }
173
174 /*
175  * attr_delete - delete the attribute type in list pointed to by attrs
176  * return       0       deleted ok
177  *              1       not found in list a
178  *              -1      something bad happened
179  */
180
181 int
182 attr_delete(
183     Attribute   **attrs,
184         AttributeDescription *desc
185 )
186 {
187         Attribute       **a;
188
189         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
190                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
191                 {
192                         Attribute       *save = *a;
193                         *a = (*a)->a_next;
194                         attr_free( save );
195
196                         return LDAP_SUCCESS;
197                 }
198         }
199
200         return LDAP_NO_SUCH_ATTRIBUTE;
201 }
202