]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
Add search no-op support.
[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                         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                 (*a)->a_flags = 0;
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                         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                         Attribute       *save = *a;
191                         *a = (*a)->a_next;
192                         attr_free( save );
193
194                         return LDAP_SUCCESS;
195                 }
196         }
197
198         return LDAP_NO_SUCH_ATTRIBUTE;
199 }
200