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
38 static int (*et_cmp_fn)();
40 #else /* !NEEDPROTOS */
41 static int (*et_cmp_fn)( char *a, char *b );
42 static int et_cmp( void *aa, void *bb);
43 #endif /* !NEEDPROTOS */
51 return( strcasecmp( *a, *b ) );
61 struct entrything *a = (struct entrything *)aa;
62 struct entrything *b = (struct entrything *)bb;
64 if ( a->et_vals == NULL && b->et_vals == NULL )
66 if ( a->et_vals == NULL )
68 if ( b->et_vals == NULL )
71 for ( i = 0; a->et_vals[i] && b->et_vals[i]; i++ ) {
72 if ( (rc = (*et_cmp_fn)( a->et_vals[i], b->et_vals[i] ))
78 if ( a->et_vals[i] == NULL && b->et_vals[i] == NULL )
80 if ( a->et_vals[i] == NULL )
89 char *attr, /* NULL => sort by DN */
94 struct entrything *et;
95 LDAPMessage *e, *last;
98 count = ldap_count_entries( ld, *chain );
100 if ( (et = (struct entrything *) malloc( count *
101 sizeof(struct entrything) )) == NULL ) {
102 ld->ld_errno = LDAP_NO_MEMORY;
107 for ( i = 0; i < count; i++ ) {
109 if ( attr == NULL ) {
112 dn = ldap_get_dn( ld, e );
113 et[i].et_vals = ldap_explode_dn( dn, 1 );
116 et[i].et_vals = ldap_get_values( ld, e, attr );
124 qsort( et, count, sizeof(struct entrything), (void *) et_cmp );
127 for ( i = 0; i < count; i++ ) {
129 ep = &(*ep)->lm_chain;
131 ldap_value_free( et[i].et_vals );
148 for ( nel = 0; vals[nel] != NULL; nel++ )
151 qsort( vals, nel, sizeof(char *), cmp );