2003-11-26 15:16:36 +08:00
|
|
|
/* sort.c -- LDAP library entry and value sort routines */
|
1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 15:16:36 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2020-01-10 00:50:21 +08:00
|
|
|
* Copyright 1998-2020 The OpenLDAP Foundation.
|
2003-11-26 15:16:36 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
1998-12-29 04:53:15 +08:00
|
|
|
*/
|
2003-11-26 15:16:36 +08:00
|
|
|
/* Portions Copyright (c) 1994 Regents of the University of Michigan.
|
1998-08-09 08:43:13 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that this notice is preserved and that due credit is given
|
|
|
|
* to the University of Michigan at Ann Arbor. The name of the University
|
|
|
|
* may not be used to endorse or promote products derived from this
|
|
|
|
* software without specific prior written permission. This software
|
|
|
|
* is provided ``as is'' without express or implied warranty.
|
|
|
|
*/
|
|
|
|
|
1998-10-25 11:13:43 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
1999-06-03 08:37:44 +08:00
|
|
|
#include <ac/stdlib.h>
|
1998-10-25 11:13:43 +08:00
|
|
|
|
|
|
|
#include <ac/ctype.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/time.h>
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
|
1998-11-11 13:50:51 +08:00
|
|
|
#include "ldap-int.h"
|
1998-10-25 11:13:43 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
struct entrything {
|
|
|
|
char **et_vals;
|
|
|
|
LDAPMessage *et_msg;
|
1998-12-19 09:27:20 +08:00
|
|
|
int (*et_cmp_fn) LDAP_P((const char *a, const char *b));
|
1998-08-09 08:43:13 +08:00
|
|
|
};
|
|
|
|
|
1998-11-04 21:15:18 +08:00
|
|
|
static int et_cmp LDAP_P(( const void *aa, const void *bb));
|
1998-10-25 11:13:43 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
ldap_sort_strcasecmp(
|
2000-02-01 09:11:26 +08:00
|
|
|
LDAP_CONST void *a,
|
|
|
|
LDAP_CONST void *b
|
1998-08-09 08:43:13 +08:00
|
|
|
)
|
|
|
|
{
|
1999-09-25 02:16:41 +08:00
|
|
|
return( strcasecmp( *(char *const *)a, *(char *const *)b ) );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
et_cmp(
|
1998-11-04 21:15:18 +08:00
|
|
|
const void *aa,
|
|
|
|
const void *bb
|
1998-08-09 08:43:13 +08:00
|
|
|
)
|
|
|
|
{
|
|
|
|
int i, rc;
|
1998-11-04 21:15:18 +08:00
|
|
|
const struct entrything *a = (const struct entrything *)aa;
|
|
|
|
const struct entrything *b = (const struct entrything *)bb;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( a->et_vals == NULL && b->et_vals == NULL )
|
|
|
|
return( 0 );
|
|
|
|
if ( a->et_vals == NULL )
|
|
|
|
return( -1 );
|
|
|
|
if ( b->et_vals == NULL )
|
|
|
|
return( 1 );
|
|
|
|
|
|
|
|
for ( i = 0; a->et_vals[i] && b->et_vals[i]; i++ ) {
|
1998-12-19 09:27:20 +08:00
|
|
|
if ( (rc = a->et_cmp_fn( a->et_vals[i], b->et_vals[i] )) != 0 ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( a->et_vals[i] == NULL && b->et_vals[i] == NULL )
|
|
|
|
return( 0 );
|
|
|
|
if ( a->et_vals[i] == NULL )
|
|
|
|
return( -1 );
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldap_sort_entries(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAPMessage **chain,
|
1999-05-19 09:12:33 +08:00
|
|
|
LDAP_CONST char *attr, /* NULL => sort by DN */
|
|
|
|
int (*cmp) (LDAP_CONST char *, LDAP_CONST char *)
|
1998-08-09 08:43:13 +08:00
|
|
|
)
|
|
|
|
{
|
2003-12-07 19:15:22 +08:00
|
|
|
int i, count = 0;
|
1998-08-09 08:43:13 +08:00
|
|
|
struct entrything *et;
|
2003-12-07 19:15:22 +08:00
|
|
|
LDAPMessage *e, *ehead = NULL, *etail = NULL;
|
|
|
|
LDAPMessage *ohead = NULL, *otail = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
LDAPMessage **ep;
|
|
|
|
|
2002-06-05 23:46:26 +08:00
|
|
|
assert( ld != NULL );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2003-12-07 19:15:22 +08:00
|
|
|
/* Separate entries from non-entries */
|
|
|
|
for ( e = *chain; e; e=e->lm_chain ) {
|
|
|
|
if ( e->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
|
|
|
|
count++;
|
|
|
|
if ( !ehead ) ehead = e;
|
|
|
|
if ( etail ) etail->lm_chain = e;
|
|
|
|
etail = e;
|
|
|
|
} else {
|
|
|
|
if ( !ohead ) ohead = e;
|
|
|
|
if ( otail ) otail->lm_chain = e;
|
|
|
|
otail = e;
|
|
|
|
}
|
|
|
|
}
|
1999-01-29 11:56:18 +08:00
|
|
|
|
2003-12-07 19:15:22 +08:00
|
|
|
if ( count < 2 ) {
|
1999-01-29 11:56:18 +08:00
|
|
|
/* zero or one entries -- already sorted! */
|
2003-12-07 19:15:22 +08:00
|
|
|
if ( ehead ) {
|
|
|
|
etail->lm_chain = ohead;
|
|
|
|
*chain = ehead;
|
|
|
|
} else {
|
|
|
|
*chain = ohead;
|
|
|
|
}
|
1999-01-29 11:56:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
if ( (et = (struct entrything *) LDAP_MALLOC( count *
|
1998-08-09 08:43:13 +08:00
|
|
|
sizeof(struct entrything) )) == NULL ) {
|
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
2003-12-07 19:15:22 +08:00
|
|
|
e = ehead;
|
1998-08-09 08:43:13 +08:00
|
|
|
for ( i = 0; i < count; i++ ) {
|
1998-12-19 09:27:20 +08:00
|
|
|
et[i].et_cmp_fn = cmp;
|
1998-08-09 08:43:13 +08:00
|
|
|
et[i].et_msg = e;
|
|
|
|
if ( attr == NULL ) {
|
|
|
|
char *dn;
|
|
|
|
|
|
|
|
dn = ldap_get_dn( ld, e );
|
|
|
|
et[i].et_vals = ldap_explode_dn( dn, 1 );
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( dn );
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
|
|
|
et[i].et_vals = ldap_get_values( ld, e, attr );
|
|
|
|
}
|
|
|
|
|
|
|
|
e = e->lm_chain;
|
|
|
|
}
|
|
|
|
|
1998-10-25 11:13:43 +08:00
|
|
|
qsort( et, count, sizeof(struct entrything), et_cmp );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
ep = chain;
|
|
|
|
for ( i = 0; i < count; i++ ) {
|
|
|
|
*ep = et[i].et_msg;
|
|
|
|
ep = &(*ep)->lm_chain;
|
|
|
|
|
2000-06-07 13:17:29 +08:00
|
|
|
LDAP_VFREE( et[i].et_vals );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2003-12-07 19:15:22 +08:00
|
|
|
*ep = ohead;
|
2006-05-11 17:07:08 +08:00
|
|
|
(*chain)->lm_chain_tail = otail ? otail : etail;
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( (char *) et );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldap_sort_values(
|
|
|
|
LDAP *ld,
|
|
|
|
char **vals,
|
2000-02-01 09:11:26 +08:00
|
|
|
int (*cmp) (LDAP_CONST void *, LDAP_CONST void *)
|
1998-08-09 08:43:13 +08:00
|
|
|
)
|
|
|
|
{
|
|
|
|
int nel;
|
|
|
|
|
|
|
|
for ( nel = 0; vals[nel] != NULL; nel++ )
|
|
|
|
; /* NULL */
|
|
|
|
|
|
|
|
qsort( vals, nel, sizeof(char *), cmp );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|