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>
32 static int (*et_cmp_fn) LDAP_P(( char *a, char *b ));
33 static int et_cmp LDAP_P(( const void *aa, const void *bb));
42 return( strcasecmp( *a, *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 = (*et_cmp_fn)( a->et_vals[i], b->et_vals[i] ))
69 if ( a->et_vals[i] == NULL && b->et_vals[i] == NULL )
71 if ( a->et_vals[i] == NULL )
80 char *attr, /* NULL => sort by DN */
81 int (*cmp) LDAP_P((const char *, const char *))
85 struct entrything *et;
86 LDAPMessage *e, *last;
89 count = ldap_count_entries( ld, *chain );
91 if ( (et = (struct entrything *) malloc( count *
92 sizeof(struct entrything) )) == NULL ) {
93 ld->ld_errno = LDAP_NO_MEMORY;
98 for ( i = 0; i < count; i++ ) {
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 );
115 qsort( et, count, sizeof(struct entrything), et_cmp );
118 for ( i = 0; i < count; i++ ) {
120 ep = &(*ep)->lm_chain;
122 ldap_value_free( et[i].et_vals );
134 int (*cmp) LDAP_P((const void *, const void *))
139 for ( nel = 0; vals[nel] != NULL; nel++ )
142 qsort( vals, nel, sizeof(char *), cmp );