1LDAP_SORT(3) Library Functions Manual LDAP_SORT(3)
2
3
4
6 ldap_sort_entries, ldap_sort_values, ldap_sort_strcasecmp - LDAP sort‐
7 ing routines
8
10 OpenLDAP LDAP (libldap, -lldap)
11
13 #include <ldap.h>
14
15 ldap_sort_entries(ld, chain, attr, cmp)
16 LDAP *ld;
17 LDAPMessage **chain;
18 char *attr;
19 int (*cmp)();
20
21 ldap_sort_values(ld, vals, cmp)
22 LDAP *ld;
23 char **vals;
24 int (*cmp)();
25
26 ldap_sort_strcasecmp(a, b)
27 char *a;
28 char *b;
29
31 These routines are used to sort lists of entries and values retrieved
32 from an LDAP server. ldap_sort_entries() is used to sort a chain of
33 entries retrieved from an LDAP search call either by DN or by some
34 arbitrary attribute in the entries. It takes ld, the LDAP structure,
35 which is only used for error reporting, chain, the list of entries as
36 returned by ldap_search_s(3) or ldap_result(3). attr is the attribute
37 to use as a key in the sort or NULL to sort by DN, and cmp is the com‐
38 parison function to use when comparing values (or individual DN compo‐
39 nents if sorting by DN). In this case, cmp should be a function taking
40 two single values of the attr to sort by, and returning a value less
41 than zero, equal to zero, or greater than zero, depending on whether
42 the first argument is less than, equal to, or greater than the second
43 argument. The convention is the same as used by qsort(3), which is
44 called to do the actual sorting.
45
46 ldap_sort_values() is used to sort an array of values from an entry, as
47 returned by ldap_get_values(3). It takes the LDAP connection structure
48 ld, the array of values to sort vals, and cmp, the comparison function
49 to use during the sort. Note that cmp will be passed a pointer to each
50 element in the vals array, so if you pass the normal char ** for this
51 parameter, cmp should take two char **'s as arguments (i.e., you cannot
52 pass strcasecmp or its friends for cmp). You can, however, pass the
53 function ldap_sort_strcasecmp() for this purpose.
54
55 For example:
56
57 LDAP *ld;
58 LDAPMessage *res;
59
60 /*
61 * ... call to ldap_search_s(), fill in res,
62 * retrieve sn attr ...
63 */
64
65 /* now sort the entries on surname attribute */
66 if ( ldap_sort_entries( ld, &res, "sn",
67 ldap_sort_strcasecmp ) != 0 )
68 ldap_perror( ld, "ldap_sort_entries" );
69
71 The ldap_sort_entries() routine applies the comparison function to each
72 value of the attribute in the array as returned by a call to
73 ldap_get_values(3), until a mismatch is found. This works fine for
74 single-valued attributes, but may produce unexpected results for multi-
75 valued attributes. When sorting by DN, the comparison function is
76 applied to an exploded version of the DN, without types. The return
77 values for all of these functions are declared in the <ldap.h> header
78 file. Some routines may dynamically allocate memory. Callers are
79 responsible for freeing such memory using the supplied deallocation
80 routines.
81
83 ldap(3), ldap_search(3), ldap_result(3), qsort(3)
84
86 OpenLDAP is developed and maintained by The OpenLDAP Project
87 (http://www.openldap.org/). OpenLDAP is derived from University of
88 Michigan LDAP 3.3 Release.
89
90
91
92OpenLDAP 2.3.34 2007/2/16 LDAP_SORT(3)