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
37 static int (*et_cmp_fn)();
39 #else /* !NEEDPROTOS */
40 static int (*et_cmp_fn)( char *a, char *b );
41 static int et_cmp( void *aa, void *bb);
42 #endif /* !NEEDPROTOS */
50 return( strcasecmp( *a, *b ) );
60 struct entrything *a = (struct entrything *)aa;
61 struct entrything *b = (struct entrything *)bb;
63 if ( a->et_vals == NULL && b->et_vals == NULL )
65 if ( a->et_vals == NULL )
67 if ( b->et_vals == NULL )
70 for ( i = 0; a->et_vals[i] && b->et_vals[i]; i++ ) {
71 if ( (rc = (*et_cmp_fn)( a->et_vals[i], b->et_vals[i] ))
77 if ( a->et_vals[i] == NULL && b->et_vals[i] == NULL )
79 if ( a->et_vals[i] == NULL )
88 char *attr, /* NULL => sort by DN */
93 struct entrything *et;
94 LDAPMessage *e, *last;
97 count = ldap_count_entries( ld, *chain );
99 if ( (et = (struct entrything *) malloc( count *
100 sizeof(struct entrything) )) == NULL ) {
101 ld->ld_errno = LDAP_NO_MEMORY;
106 for ( i = 0; i < count; i++ ) {
108 if ( attr == NULL ) {
111 dn = ldap_get_dn( ld, e );
112 et[i].et_vals = ldap_explode_dn( dn, 1 );
115 et[i].et_vals = ldap_get_values( ld, e, attr );
123 qsort( et, count, sizeof(struct entrything), (void *) et_cmp );
126 for ( i = 0; i < count; i++ ) {
128 ep = &(*ep)->lm_chain;
130 ldap_value_free( et[i].et_vals );
147 for ( nel = 0; vals[nel] != NULL; nel++ )
150 qsort( vals, nel, sizeof(char *), cmp );