2 * Copyright (c) 1994 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
12 * sort.c: LDAP library entry and value sort routines
21 #include <ac/string.h>
30 int (*et_cmp_fn) LDAP_P((const char *a, const char *b));
33 static int et_cmp LDAP_P(( const void *aa, const void *bb));
42 return( strcasecmp( *(char *const *)a, *(char *const *)b ) );
52 const struct entrything *a = (const struct entrything *)aa;
53 const struct entrything *b = (const struct entrything *)bb;
55 if ( a->et_vals == NULL && b->et_vals == NULL )
57 if ( a->et_vals == NULL )
59 if ( b->et_vals == NULL )
62 for ( i = 0; a->et_vals[i] && b->et_vals[i]; i++ ) {
63 if ( (rc = a->et_cmp_fn( a->et_vals[i], b->et_vals[i] )) != 0 ) {
68 if ( a->et_vals[i] == NULL && b->et_vals[i] == NULL )
70 if ( a->et_vals[i] == NULL )
79 char *attr, /* NULL => sort by DN */
80 int (*cmp) (const char *, const char *)
84 struct entrything *et;
85 LDAPMessage *e, *last;
88 count = ldap_count_entries( ld, *chain );
90 if ( (et = (struct entrything *) malloc( count *
91 sizeof(struct entrything) )) == NULL ) {
92 ld->ld_errno = LDAP_NO_MEMORY;
97 for ( i = 0; i < count; i++ ) {
98 et[i].et_cmp_fn = cmp;
100 if ( attr == NULL ) {
103 dn = ldap_get_dn( ld, e );
104 et[i].et_vals = ldap_explode_dn( dn, 1 );
107 et[i].et_vals = ldap_get_values( ld, e, attr );
114 qsort( et, count, sizeof(struct entrything), et_cmp );
117 for ( i = 0; i < count; i++ ) {
119 ep = &(*ep)->lm_chain;
121 ldap_value_free( et[i].et_vals );
133 int (*cmp) (const void *, const void *)
138 for ( nel = 0; vals[nel] != NULL; nel++ )
141 qsort( vals, nel, sizeof(char *), cmp );