1 /* sort.c -- LDAP library entry and value sort routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1994 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
30 #include <ac/stdlib.h>
33 #include <ac/string.h>
42 int (*et_cmp_fn) LDAP_P((const char *a, const char *b));
45 static int et_cmp LDAP_P(( const void *aa, const void *bb));
54 return( strcasecmp( *(char *const *)a, *(char *const *)b ) );
64 const struct entrything *a = (const struct entrything *)aa;
65 const struct entrything *b = (const struct entrything *)bb;
67 if ( a->et_vals == NULL && b->et_vals == NULL )
69 if ( a->et_vals == NULL )
71 if ( b->et_vals == NULL )
74 for ( i = 0; a->et_vals[i] && b->et_vals[i]; i++ ) {
75 if ( (rc = a->et_cmp_fn( a->et_vals[i], b->et_vals[i] )) != 0 ) {
80 if ( a->et_vals[i] == NULL && b->et_vals[i] == NULL )
82 if ( a->et_vals[i] == NULL )
91 LDAP_CONST char *attr, /* NULL => sort by DN */
92 int (*cmp) (LDAP_CONST char *, LDAP_CONST char *)
96 struct entrything *et;
97 LDAPMessage *e, *last;
100 assert( ld != NULL );
102 count = ldap_count_entries( ld, *chain );
107 } else if ( count < 2 ) {
108 /* zero or one entries -- already sorted! */
112 if ( (et = (struct entrything *) LDAP_MALLOC( count *
113 sizeof(struct entrything) )) == NULL ) {
114 ld->ld_errno = LDAP_NO_MEMORY;
119 for ( i = 0; i < count; i++ ) {
120 et[i].et_cmp_fn = cmp;
122 if ( attr == NULL ) {
125 dn = ldap_get_dn( ld, e );
126 et[i].et_vals = ldap_explode_dn( dn, 1 );
129 et[i].et_vals = ldap_get_values( ld, e, attr );
136 qsort( et, count, sizeof(struct entrything), et_cmp );
139 for ( i = 0; i < count; i++ ) {
141 ep = &(*ep)->lm_chain;
143 LDAP_VFREE( et[i].et_vals );
146 LDAP_FREE( (char *) et );
155 int (*cmp) (LDAP_CONST void *, LDAP_CONST void *)
160 for ( nel = 0; vals[nel] != NULL; nel++ )
163 qsort( vals, nel, sizeof(char *), cmp );