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>
33 static int (*et_cmp_fn) LDAP_P(( char *a, char *b ));
34 static int et_cmp LDAP_P(( void *aa, void *bb));
43 return( strcasecmp( *a, *b ) );
53 struct entrything *a = (struct entrything *)aa;
54 struct entrything *b = (struct entrything *)bb;
56 if ( a->et_vals == NULL && b->et_vals == NULL )
58 if ( a->et_vals == NULL )
60 if ( b->et_vals == NULL )
63 for ( i = 0; a->et_vals[i] && b->et_vals[i]; i++ ) {
64 if ( (rc = (*et_cmp_fn)( a->et_vals[i], b->et_vals[i] ))
70 if ( a->et_vals[i] == NULL && b->et_vals[i] == NULL )
72 if ( a->et_vals[i] == NULL )
81 char *attr, /* NULL => sort by DN */
86 struct entrything *et;
87 LDAPMessage *e, *last;
90 count = ldap_count_entries( ld, *chain );
92 if ( (et = (struct entrything *) malloc( count *
93 sizeof(struct entrything) )) == NULL ) {
94 ld->ld_errno = LDAP_NO_MEMORY;
99 for ( i = 0; i < count; i++ ) {
101 if ( attr == NULL ) {
104 dn = ldap_get_dn( ld, e );
105 et[i].et_vals = ldap_explode_dn( dn, 1 );
108 et[i].et_vals = ldap_get_values( ld, e, attr );
116 qsort( et, count, sizeof(struct entrything), (void *) et_cmp );
119 for ( i = 0; i < count; i++ ) {
121 ep = &(*ep)->lm_chain;
123 ldap_value_free( et[i].et_vals );
135 int (*cmp) LDAP_P((const void *, const void *))
140 for ( nel = 0; vals[nel] != NULL; nel++ )
143 qsort( vals, nel, sizeof(char *), (void *) cmp );