3 * Copyright 1998-2000 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 );
30 attr_free( Attribute *a )
32 #ifdef SLAPD_SCHEMA_NOT_COMPAT
33 ad_free( a->a_desc, 1 );
37 ber_bvecfree( a->a_vals );
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] != NULL; i++ ) {
67 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval*));
69 for( i=0; a->a_vals[i] != NULL; i++ ) {
70 tmp->a_vals[i] = ber_bvdup( a->a_vals[i] );
72 if( tmp->a_vals[i] == NULL ) break;
75 tmp->a_vals[i] = NULL;
81 #ifdef SLAPD_SCHEMA_NOT_COMPAT
82 tmp->a_desc = ad_dup( a->a_desc );
84 tmp->a_type = ch_strdup( a->a_type );
85 tmp->a_syntax = a->a_syntax;
92 Attribute *attrs_dup( Attribute *a )
94 Attribute *tmp, **next;
96 if( a == NULL ) return NULL;
101 for( ; a != NULL ; a = a->a_next ) {
102 *next = attr_dup( a );
103 next = &((*next)->a_next);
110 #ifndef SLAPD_SCHEMA_NOT_COMPAT
112 * attr_normalize - normalize an attribute name (make it all lowercase)
116 attr_normalize( char *s )
120 return( ldap_pvt_str2lower( s ) );
125 * attr_merge_fast - merge the given type and value with the list of
126 * attributes in attrs. called from str2entry(), where we can make some
127 * assumptions to make things faster.
128 * returns 0 everything went ok
132 #ifdef SLAPD_SCHEMA_NOT_COMPAT
139 struct berval **vals,
147 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
148 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
155 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
156 (**a)->a_vals = NULL;
157 (**a)->a_type = attr_normalize( ch_strdup( type ) );
158 (**a)->a_syntax = attr_syntax( type );
159 (**a)->a_next = NULL;
162 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
169 * attr_merge - merge the given type and value with the list of
170 * attributes in attrs.
171 * returns 0 everything went ok
178 #ifdef SLAPD_SCHEMA_NOT_COMPAT
179 AttributeDescription *desc,
183 struct berval **vals )
187 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
188 #ifdef SLAPD_SCHEMA_NOT_COMPAT
189 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
191 if ( strcasecmp( (*a)->a_type, type ) == 0 )
199 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
200 #ifdef SLAPD_SCHEMA_NOT_COMPAT
201 (*a)->a_desc = ad_dup( desc );
203 (*a)->a_type = attr_normalize( ch_strdup( type ) );
204 (*a)->a_syntax = attr_syntax( type );
210 return( value_add( &(*a)->a_vals, vals ) );
213 #ifdef SLAPD_SCHEMA_NOT_COMPAT
215 * attrs_find - find attribute(s) by AttributeDescription
216 * returns next attribute which is subtype of provided description.
222 AttributeDescription *desc
225 for ( ; a != NULL; a = a->a_next ) {
226 if ( is_ad_subtype( a->a_desc, desc ) ) {
236 * attr_find - find attribute by type
242 #ifdef SLAPD_SCHEMA_NOT_COMPAT
243 AttributeDescription *desc
249 for ( ; a != NULL; a = a->a_next ) {
250 #ifdef SLAPD_SCHEMA_NOT_COMPAT
251 if ( ad_cmp( a->a_desc, desc ) == 0 )
253 if ( strcasecmp( a->a_type, type ) == 0 )
264 * attr_delete - delete the attribute type in list pointed to by attrs
265 * return 0 deleted ok
266 * 1 not found in list a
267 * -1 something bad happened
273 #ifdef SLAPD_SCHEMA_NOT_COMPAT
274 AttributeDescription *desc
282 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
283 #ifdef SLAPD_SCHEMA_NOT_COMPAT
284 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
286 if ( strcasecmp( (*a)->a_type, type ) == 0 )
289 Attribute *save = *a;
297 return LDAP_NO_SUCH_ATTRIBUTE;