X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;ds=inline;f=libraries%2Flibldap%2Fsort.c;h=2e5c09e0211186a9f3066588a6c6d5a7d2d4d8bb;hb=4d29df5bd1fabcdc50975651c746365686b62b53;hp=d35eec1b40d41640018847836964a0eb9ad46a0e;hpb=0919e503cdb370d54b6d4a0caa15b6605980c34e;p=openldap
diff --git a/libraries/libldap/sort.c b/libraries/libldap/sort.c
index d35eec1b40..2e5c09e021 100644
--- a/libraries/libldap/sort.c
+++ b/libraries/libldap/sort.c
@@ -1,10 +1,19 @@
+/* sort.c -- LDAP library entry and value sort routines */
/* $OpenLDAP$ */
-/*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software .
+ *
+ * Copyright 1998-2003 The OpenLDAP Foundation.
+ * 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
+ * .
*/
-/* Portions
- * Copyright (c) 1994 Regents of the University of Michigan.
+/* Portions Copyright (c) 1994 Regents of the University of Michigan.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
@@ -13,8 +22,6 @@
* 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.
- *
- * sort.c: LDAP library entry and value sort routines
*/
#include "portable.h"
@@ -85,22 +92,36 @@ ldap_sort_entries(
int (*cmp) (LDAP_CONST char *, LDAP_CONST char *)
)
{
- int i, count;
+ int i, count = 0;
struct entrything *et;
- LDAPMessage *e, *last;
+ LDAPMessage *e, *ehead = NULL, *etail = NULL;
+ LDAPMessage *ohead = NULL, *otail = NULL;
LDAPMessage **ep;
- count = ldap_count_entries( ld, *chain );
+ assert( ld != NULL );
-
- if ( count < 0 ) {
- if( ld != NULL ) {
- ld->ld_errno = LDAP_PARAM_ERROR;
+ /* 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;
}
- return -1;
+ }
- } else if ( count < 2 ) {
+ if ( count < 2 ) {
/* zero or one entries -- already sorted! */
+ if ( ehead ) {
+ etail->lm_chain = ohead;
+ *chain = ehead;
+ } else {
+ *chain = ohead;
+ }
return 0;
}
@@ -110,7 +131,7 @@ ldap_sort_entries(
return( -1 );
}
- e = *chain;
+ e = ehead;
for ( i = 0; i < count; i++ ) {
et[i].et_cmp_fn = cmp;
et[i].et_msg = e;
@@ -126,7 +147,6 @@ ldap_sort_entries(
e = e->lm_chain;
}
- last = e;
qsort( et, count, sizeof(struct entrything), et_cmp );
@@ -135,9 +155,10 @@ ldap_sort_entries(
*ep = et[i].et_msg;
ep = &(*ep)->lm_chain;
- ldap_value_free( et[i].et_vals );
+ LDAP_VFREE( et[i].et_vals );
}
- *ep = last;
+ *ep = ohead;
+
LDAP_FREE( (char *) et );
return( 0 );