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 ) );
124 * attr_merge_fast - merge the given type and value with the list of
125 * attributes in attrs. called from str2entry(), where we can make some
126 * assumptions to make things faster.
127 * returns 0 everything went ok
135 struct berval **vals,
143 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
144 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
151 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
152 (**a)->a_vals = NULL;
153 (**a)->a_type = attr_normalize( ch_strdup( type ) );
154 (**a)->a_syntax = attr_syntax( type );
155 (**a)->a_next = NULL;
158 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
165 * attr_merge - merge the given type and value with the list of
166 * attributes in attrs.
167 * returns 0 everything went ok
174 #ifdef SLAPD_SCHEMA_NOT_COMPAT
175 AttributeDescription *desc,
179 struct berval **vals )
183 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
184 #ifdef SLAPD_SCHEMA_NOT_COMPAT
185 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
187 if ( strcasecmp( (*a)->a_type, type ) == 0 )
195 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
196 #ifdef SLAPD_SCHEMA_NOT_COMPAT
197 (*a)->a_desc = ad_dup( desc );
199 (*a)->a_type = attr_normalize( ch_strdup( type ) );
200 (*a)->a_syntax = attr_syntax( type );
206 return( value_add( &(*a)->a_vals, vals ) );
209 #ifdef SLAPD_SCHEMA_NOT_COMPAT
211 * attrs_find - find attribute(s) by AttributeDescription
212 * returns next attribute which is subtype of provided description.
218 AttributeDescription *desc
221 for ( ; a != NULL; a = a->a_next ) {
222 if ( is_ad_subtype( a->a_desc, desc ) ) {
232 * attr_find - find attribute by type
238 #ifdef SLAPD_SCHEMA_NOT_COMPAT
239 AttributeDescription *desc
245 for ( ; a != NULL; a = a->a_next ) {
246 #ifdef SLAPD_SCHEMA_NOT_COMPAT
247 if ( ad_cmp( a->a_desc, desc ) == 0 )
249 if ( strcasecmp( a->a_type, type ) == 0 )
260 * attr_delete - delete the attribute type in list pointed to by attrs
261 * return 0 deleted ok
262 * 1 not found in list a
263 * -1 something bad happened
269 #ifdef SLAPD_SCHEMA_NOT_COMPAT
270 AttributeDescription *desc
278 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
279 #ifdef SLAPD_SCHEMA_NOT_COMPAT
280 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
282 if ( strcasecmp( (*a)->a_type, type ) == 0 )
285 Attribute *save = *a;
293 return LDAP_NO_SUCH_ATTRIBUTE;