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 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 != NULL ) {
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;
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.
119 * returns 0 everything went ok
126 AttributeDescription *desc,
136 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
137 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
143 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
147 (*a)->a_nvals = NULL;
153 rc = value_add( &(*a)->a_vals, vals );
156 if( !rc && nvals ) rc = value_add( &(*a)->a_nvals, nvals );
165 AttributeDescription *desc,
168 , struct berval *nval
174 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
175 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
181 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
185 (*a)->a_nvals = NULL;
191 rc = value_add_one( &(*a)->a_vals, val );
194 if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval );
200 * attrs_find - find attribute(s) by AttributeDescription
201 * returns next attribute which is subtype of provided description.
207 AttributeDescription *desc
210 for ( ; a != NULL; a = a->a_next ) {
211 if ( is_ad_subtype( a->a_desc, desc ) ) {
220 * attr_find - find attribute by type
226 AttributeDescription *desc
229 for ( ; a != NULL; a = a->a_next ) {
230 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
239 * attr_delete - delete the attribute type in list pointed to by attrs
240 * return 0 deleted ok
241 * 1 not found in list a
242 * -1 something bad happened
248 AttributeDescription *desc
253 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
254 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
255 Attribute *save = *a;
263 return LDAP_NO_SUCH_ATTRIBUTE;