openldap/libraries/libldap/sort.c

184 lines
3.9 KiB
C
Raw Normal View History

2003-11-26 15:16:36 +08:00
/* sort.c -- LDAP library entry and value sort routines */
/* $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.
*/
#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>
#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-08-09 08:43:13 +08:00
struct entrything {
char **et_vals;
LDAPMessage *et_msg;
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-08-09 08:43:13 +08:00
int
ldap_sort_strcasecmp(
LDAP_CONST void *a,
LDAP_CONST void *b
1998-08-09 08:43:13 +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++ ) {
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,
Vienna Bulk Commit This commit includes many changes. All changes compile under NT but have not been tested under UNIX. A Summary of changes (likely incomplete): NT changes: Removed lint. Clean up configuration support for "Debug", "Release", "SDebug", and "SRelease" configurations. Share output directories for clients, libraries, and slapd. (maybe they should be combined further and moved to build/{,S}{Debug,Release}). Enable threading when _MT is defined. Enable debuging when _DEBUG is defined. Disable setting of NDEBUG under Release/SRelease. Asserts are disabled in <ac/assert.h> when LDAP_DEBUG is not defined. Added 'build/main.dsp' Master project. Removed non-slapd projects from slapd.dsp (see main.dsp). Removed replaced many uses of _WIN32 macro with feature based macros. ldap_cdefs.h changes #define LDAP_CONST const (see below) #define LDAP_F(type) LDAP_F_PRE type LDAP_F_POST To allow specifiers to be added before and after the type declaration. (For DLL handling) LBER/LDAP changes Namespace changes: s/lber_/ber_/ for here and there. s/NAME_ERROR/LDAP_NAME_ERROR/g Deleted NULLMSG and other NULL* macros for namespace reasons. "const" libraries. Installed headers (ie: lber.h, ldap.h) use LDAP_CONST macro. Normally set to 'const' when __STDC__. Can be set externally to enable/disable 'constification' of external interface. Internal interface always uses 'const'. Did not fix warnings in -lldif (in lieu of new LDIF parser). Added _ext API implementations (excepting search and bind). Need to implement ldap_int_get_controls() for reponses with controls. Added numberous assert() checks. LDAP_R _MT defines HAVE_NT_THREADS Added numberous assert() checks. Changed ldap_pthread_t back to unsigned long. Used cast to HANDLE in _join(). LDBM Replaced _WIN32 with HAVE_SYSLOG ud Added version string if MKVERSION is not defined. (MKVERSION needs to be set under UNIX). slapd Made connection sockbuf field a pointer to a sockbuf. This removed slap.h dependency on lber-int.h. lber-int.h now only included by those files needing to mess with the sockbuf. Used ber_* functions/macros to access sockbuf internals whenever possible. Added version string if MKVERSION is not defined. (MKVERSION needs to be set under UNIX). Removed FD_SET unsigned lint slapd/tools Used EXEEXT to added ".exe" to routines. Need to define EXEEXT under UNIX. ldappasswd Added ldappasswd.dsp. Ported to NT. Used getpid() to seed rand(). nt_debug Minor cleanup. Added "portable.h" include and used <ac/*.h> where appropriate. Added const to char* format argument.
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
)
{
int i, count = 0;
1998-08-09 08:43:13 +08:00
struct entrything *et;
LDAPMessage *e, *ehead = NULL, *etail = NULL;
LDAPMessage *ohead = NULL, *otail = NULL;
1998-08-09 08:43:13 +08:00
LDAPMessage **ep;
assert( ld != NULL );
1998-08-09 08:43:13 +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;
}
}
if ( count < 2 ) {
/* zero or one entries -- already sorted! */
if ( ehead ) {
etail->lm_chain = ohead;
*chain = ehead;
} else {
*chain = ohead;
}
return 0;
}
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 );
}
e = ehead;
1998-08-09 08:43:13 +08:00
for ( i = 0; i < count; i++ ) {
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 );
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;
}
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;
LDAP_VFREE( et[i].et_vals );
1998-08-09 08:43:13 +08:00
}
*ep = ohead;
(*chain)->lm_chain_tail = otail ? otail : etail;
LDAP_FREE( (char *) et );
1998-08-09 08:43:13 +08:00
return( 0 );
}
int
ldap_sort_values(
LDAP *ld,
char **vals,
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 );
}