Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / openldap / dist / libraries / libldap / getvalues.c
blobeedf900e7a2def95d8af7da7acdf79193bdb1c8d
1 /* $OpenLDAP: pkg/ldap/libraries/libldap/getvalues.c,v 1.26.2.3 2008/02/11 23:26:41 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 The OpenLDAP Foundation.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
9 * Public License.
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16 * All rights reserved.
19 #include "portable.h"
21 #include <stdio.h>
23 #include <ac/stdlib.h>
25 #include <ac/ctype.h>
26 #include <ac/socket.h>
27 #include <ac/string.h>
28 #include <ac/time.h>
30 #include "ldap-int.h"
32 char **
33 ldap_get_values( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target )
35 BerElement ber;
36 char *attr;
37 int found = 0;
38 char **vals;
40 assert( ld != NULL );
41 assert( LDAP_VALID( ld ) );
42 assert( entry != NULL );
43 assert( target != NULL );
45 Debug( LDAP_DEBUG_TRACE, "ldap_get_values\n", 0, 0, 0 );
47 ber = *entry->lm_ber;
49 /* skip sequence, dn, sequence of, and snag the first attr */
50 if ( ber_scanf( &ber, "{x{{a" /*}}}*/, &attr ) == LBER_ERROR ) {
51 ld->ld_errno = LDAP_DECODING_ERROR;
52 return( NULL );
55 if ( strcasecmp( target, attr ) == 0 )
56 found = 1;
58 /* break out on success, return out on error */
59 while ( ! found ) {
60 LDAP_FREE(attr);
61 attr = NULL;
63 if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) {
64 ld->ld_errno = LDAP_DECODING_ERROR;
65 return( NULL );
68 if ( strcasecmp( target, attr ) == 0 )
69 break;
73 LDAP_FREE(attr);
74 attr = NULL;
76 /*
77 * if we get this far, we've found the attribute and are sitting
78 * just before the set of values.
81 if ( ber_scanf( &ber, "[v]", &vals ) == LBER_ERROR ) {
82 ld->ld_errno = LDAP_DECODING_ERROR;
83 return( NULL );
86 return( vals );
89 struct berval **
90 ldap_get_values_len( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target )
92 BerElement ber;
93 char *attr;
94 int found = 0;
95 struct berval **vals;
97 assert( ld != NULL );
98 assert( LDAP_VALID( ld ) );
99 assert( entry != NULL );
100 assert( target != NULL );
102 Debug( LDAP_DEBUG_TRACE, "ldap_get_values_len\n", 0, 0, 0 );
104 ber = *entry->lm_ber;
106 /* skip sequence, dn, sequence of, and snag the first attr */
107 if ( ber_scanf( &ber, "{x{{a" /* }}} */, &attr ) == LBER_ERROR ) {
108 ld->ld_errno = LDAP_DECODING_ERROR;
109 return( NULL );
112 if ( strcasecmp( target, attr ) == 0 )
113 found = 1;
115 /* break out on success, return out on error */
116 while ( ! found ) {
117 LDAP_FREE( attr );
118 attr = NULL;
120 if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) {
121 ld->ld_errno = LDAP_DECODING_ERROR;
122 return( NULL );
125 if ( strcasecmp( target, attr ) == 0 )
126 break;
129 LDAP_FREE( attr );
130 attr = NULL;
133 * if we get this far, we've found the attribute and are sitting
134 * just before the set of values.
137 if ( ber_scanf( &ber, "[V]", &vals ) == LBER_ERROR ) {
138 ld->ld_errno = LDAP_DECODING_ERROR;
139 return( NULL );
142 return( vals );
146 ldap_count_values( char **vals )
148 int i;
150 if ( vals == NULL )
151 return( 0 );
153 for ( i = 0; vals[i] != NULL; i++ )
154 ; /* NULL */
156 return( i );
160 ldap_count_values_len( struct berval **vals )
162 return( ldap_count_values( (char **) vals ) );
165 void
166 ldap_value_free( char **vals )
168 LDAP_VFREE( vals );
171 void
172 ldap_value_free_len( struct berval **vals )
174 ber_bvecfree( vals );
177 char **
178 ldap_value_dup( char *const *vals )
180 char **new;
181 int i;
183 if( vals == NULL ) {
184 return NULL;
187 for( i=0; vals[i]; i++ ) {
188 ; /* Count the number of values */
191 if( i == 0 ) {
192 return NULL;
195 new = LDAP_MALLOC( (i+1)*sizeof(char *) ); /* Alloc array of pointers */
196 if( new == NULL ) {
197 return NULL;
200 for( i=0; vals[i]; i++ ) {
201 new[i] = LDAP_STRDUP( vals[i] ); /* Dup each value */
202 if( new[i] == NULL ) {
203 LDAP_VFREE( new );
204 return NULL;
207 new[i] = NULL;
209 return new;