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));
68 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
69 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
70 if( tmp->a_vals[i].bv_val == NULL ) break;
72 tmp->a_vals[i].bv_val = NULL;
75 if( a->a_nvals != NULL ) {
76 tmp->a_nvals = ch_malloc((i+1) * sizeof(struct berval));
77 for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
78 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
79 if( tmp->a_nvals[i].bv_val == NULL ) break;
81 tmp->a_nvals[i].bv_val = NULL;
95 tmp->a_desc = a->a_desc;
102 Attribute *attrs_dup( Attribute *a )
104 Attribute *tmp, **next;
106 if( a == NULL ) return NULL;
111 for( ; a != NULL ; a = a->a_next ) {
112 *next = attr_dup( a );
113 next = &((*next)->a_next);
123 * attr_merge - merge the given type and value with the list of
124 * attributes in attrs.
125 * returns 0 everything went ok
132 AttributeDescription *desc,
142 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
143 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
149 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
153 (*a)->a_nvals = NULL;
159 rc = value_add( &(*a)->a_vals, vals );
162 if( !rc && nvals ) rc = value_add( &(*a)->a_nvals, nvals );
171 AttributeDescription *desc,
180 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
181 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
187 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
191 (*a)->a_nvals = NULL;
197 rc = value_add_one( &(*a)->a_vals, val );
200 if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval );
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;