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 static void at_index_print( void )
32 attr_free( Attribute *a )
34 ber_bvarray_free( a->a_vals );
36 ber_bvarray_free( a->a_nvals );
42 attrs_free( Attribute *a )
46 for( ; a != NULL ; a = next ) {
52 Attribute *attr_dup( Attribute *a )
56 if( a == NULL) return NULL;
58 tmp = ch_malloc( sizeof(Attribute) );
60 if( a->a_vals != NULL ) {
63 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
67 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval));
69 if( a->a_nvals != NULL ) {
70 tmp->a_nvals = ch_malloc((i+1) * sizeof(struct berval));
74 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
75 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
76 if( tmp->a_vals[i].bv_val == NULL ) break;
80 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
81 if( tmp->a_nvals[i].bv_val == NULL ) break;
88 tmp->a_vals[i].bv_val = NULL;
90 if( tmp->a_nvals != NULL ) {
91 tmp->a_nvals[i].bv_val = NULL;
102 tmp->a_desc = a->a_desc;
109 Attribute *attrs_dup( Attribute *a )
111 Attribute *tmp, **next;
113 if( a == NULL ) return NULL;
118 for( ; a != NULL ; a = a->a_next ) {
119 *next = attr_dup( a );
120 next = &((*next)->a_next);
130 * attr_merge - merge the given type and value with the list of
131 * attributes in attrs.
132 * returns 0 everything went ok
139 AttributeDescription *desc,
149 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
150 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
156 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
160 (*a)->a_nvals = NULL;
166 rc = value_add( &(*a)->a_vals, vals );
169 if( !rc && nvals ) rc = value_add( &(*a)->a_nvals, nvals );
178 AttributeDescription *desc,
187 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
188 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
194 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
198 (*a)->a_nvals = NULL;
204 rc = value_add_one( &(*a)->a_vals, val );
207 if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval );
213 * attrs_find - find attribute(s) by AttributeDescription
214 * returns next attribute which is subtype of provided description.
220 AttributeDescription *desc
223 for ( ; a != NULL; a = a->a_next ) {
224 if ( is_ad_subtype( a->a_desc, desc ) ) {
233 * attr_find - find attribute by type
239 AttributeDescription *desc
242 for ( ; a != NULL; a = a->a_next ) {
243 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
252 * attr_delete - delete the attribute type in list pointed to by attrs
253 * return 0 deleted ok
254 * 1 not found in list a
255 * -1 something bad happened
261 AttributeDescription *desc
266 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
267 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
268 Attribute *save = *a;
276 return LDAP_NO_SUCH_ATTRIBUTE;