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 tmp->a_nvals = ch_malloc((i+1) * sizeof(struct berval));
72 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
73 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
74 if( tmp->a_vals[i].bv_val == NULL ) break;
76 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
77 if( tmp->a_nvals[i].bv_val == NULL ) break;
81 tmp->a_vals[i].bv_val = NULL;
83 tmp->a_nvals[i].bv_val = NULL;
93 tmp->a_desc = a->a_desc;
100 Attribute *attrs_dup( Attribute *a )
102 Attribute *tmp, **next;
104 if( a == NULL ) return NULL;
109 for( ; a != NULL ; a = a->a_next ) {
110 *next = attr_dup( a );
111 next = &((*next)->a_next);
121 * attr_merge - merge the given type and value with the list of
122 * attributes in attrs.
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 );
169 AttributeDescription *desc,
178 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
179 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
185 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
189 (*a)->a_nvals = NULL;
195 rc = value_add_one( &(*a)->a_vals, val );
198 if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval );
204 * attrs_find - find attribute(s) by AttributeDescription
205 * returns next attribute which is subtype of provided description.
211 AttributeDescription *desc
214 for ( ; a != NULL; a = a->a_next ) {
215 if ( is_ad_subtype( a->a_desc, desc ) ) {
224 * attr_find - find attribute by type
230 AttributeDescription *desc
233 for ( ; a != NULL; a = a->a_next ) {
234 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
243 * attr_delete - delete the attribute type in list pointed to by attrs
244 * return 0 deleted ok
245 * 1 not found in list a
246 * -1 something bad happened
252 AttributeDescription *desc
257 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
258 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
259 Attribute *save = *a;
267 return LDAP_NO_SUCH_ATTRIBUTE;