3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* attr.c - routines for dealing with attributes */
18 #include <ac/socket.h>
19 #include <ac/string.h>
26 attr_free( Attribute *a )
28 ber_bvarray_free( a->a_vals );
30 if (a->a_nvals != a->a_vals) ber_bvarray_free( a->a_nvals );
36 attrs_free( Attribute *a )
40 for( ; a != NULL ; a = next ) {
46 Attribute *attr_dup( Attribute *a )
50 if( a == NULL) return NULL;
52 tmp = ch_malloc( sizeof(Attribute) );
54 if( a->a_vals != NULL ) {
57 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
61 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval));
62 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
63 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
64 if( tmp->a_vals[i].bv_val == NULL ) break;
66 tmp->a_vals[i].bv_val = NULL;
69 if( a->a_nvals != a->a_vals ) {
70 tmp->a_nvals = ch_malloc((i+1) * sizeof(struct berval));
71 for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
72 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
73 if( tmp->a_nvals[i].bv_val == NULL ) break;
75 tmp->a_nvals[i].bv_val = NULL;
78 tmp->a_nvals = tmp->a_vals;
89 tmp->a_desc = a->a_desc;
96 Attribute *attrs_dup( Attribute *a )
98 Attribute *tmp, **next;
100 if( a == NULL ) return NULL;
105 for( ; a != NULL ; a = a->a_next ) {
106 *next = attr_dup( a );
107 next = &((*next)->a_next);
117 * attr_merge - merge the given type and value with the list of
118 * attributes in attrs.
120 * For SLAP_NVALUES: nvals must be NULL if the attribute has no
121 * normalizer. In this case, a->a_nvals will be set equal to a->a_vals.
123 * returns 0 everything went ok
130 AttributeDescription *desc,
140 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
141 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
147 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
151 (*a)->a_nvals = NULL;
157 rc = value_add( &(*a)->a_vals, vals );
160 if( !rc && nvals ) rc = value_add( &(*a)->a_nvals, nvals );
161 else (*a)->a_nvals = (*a)->a_vals;
170 AttributeDescription *desc,
173 , struct berval *nval
179 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
180 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
186 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
190 (*a)->a_nvals = NULL;
196 rc = value_add_one( &(*a)->a_vals, val );
199 if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval );
200 else (*a)->a_nvals = (*a)->a_vals;
206 * attrs_find - find attribute(s) by AttributeDescription
207 * returns next attribute which is subtype of provided description.
213 AttributeDescription *desc
216 for ( ; a != NULL; a = a->a_next ) {
217 if ( is_ad_subtype( a->a_desc, desc ) ) {
226 * attr_find - find attribute by type
232 AttributeDescription *desc
235 for ( ; a != NULL; a = a->a_next ) {
236 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
245 * attr_delete - delete the attribute type in list pointed to by attrs
246 * return 0 deleted ok
247 * 1 not found in list a
248 * -1 something bad happened
254 AttributeDescription *desc
259 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
260 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
261 Attribute *save = *a;
269 return LDAP_NO_SUCH_ATTRIBUTE;