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 );
29 if (a->a_nvals != a->a_vals) ber_bvarray_free( a->a_nvals );
34 attrs_free( Attribute *a )
38 for( ; a != NULL ; a = next ) {
44 Attribute *attr_dup( Attribute *a )
48 if( a == NULL) return NULL;
50 tmp = ch_malloc( sizeof(Attribute) );
52 if( a->a_vals != NULL ) {
55 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
59 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval));
60 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
61 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
62 if( tmp->a_vals[i].bv_val == NULL ) break;
64 tmp->a_vals[i].bv_val = NULL;
66 if( a->a_nvals != a->a_vals ) {
67 tmp->a_nvals = ch_malloc((i+1) * sizeof(struct berval));
68 for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
69 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
70 if( tmp->a_nvals[i].bv_val == NULL ) break;
72 tmp->a_nvals[i].bv_val = NULL;
75 tmp->a_nvals = tmp->a_vals;
83 tmp->a_desc = a->a_desc;
90 Attribute *attrs_dup( Attribute *a )
92 Attribute *tmp, **next;
94 if( a == NULL ) return NULL;
99 for( ; a != NULL ; a = a->a_next ) {
100 *next = attr_dup( a );
101 next = &((*next)->a_next);
111 * attr_merge - merge the given type and value with the list of
112 * attributes in attrs.
114 * For SLAP_NVALUES: nvals must be NULL if the attribute has no
115 * normalizer. In this case, a->a_nvals will be set equal to a->a_vals.
117 * returns 0 everything went ok
124 AttributeDescription *desc,
132 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
133 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
139 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
142 (*a)->a_nvals = NULL;
147 rc = value_add( &(*a)->a_vals, vals );
149 if( !rc && nvals ) rc = value_add( &(*a)->a_nvals, nvals );
150 else (*a)->a_nvals = (*a)->a_vals;
158 AttributeDescription *desc,
160 , struct berval *nval
165 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
166 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
172 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
175 (*a)->a_nvals = NULL;
180 rc = value_add_one( &(*a)->a_vals, val );
182 if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval );
183 else (*a)->a_nvals = (*a)->a_vals;
188 * attrs_find - find attribute(s) by AttributeDescription
189 * returns next attribute which is subtype of provided description.
195 AttributeDescription *desc
198 for ( ; a != NULL; a = a->a_next ) {
199 if ( is_ad_subtype( a->a_desc, desc ) ) {
208 * attr_find - find attribute by type
214 AttributeDescription *desc
217 for ( ; a != NULL; a = a->a_next ) {
218 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
227 * attr_delete - delete the attribute type in list pointed to by attrs
228 * return 0 deleted ok
229 * 1 not found in list a
230 * -1 something bad happened
236 AttributeDescription *desc
241 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
242 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
243 Attribute *save = *a;
251 return LDAP_NO_SUCH_ATTRIBUTE;