]> git.sur5r.net Git - openldap/commitdiff
ITS#2494 sort entries ahead of everything else
authorHoward Chu <hyc@openldap.org>
Sun, 7 Dec 2003 11:15:22 +0000 (11:15 +0000)
committerHoward Chu <hyc@openldap.org>
Sun, 7 Dec 2003 11:15:22 +0000 (11:15 +0000)
libraries/libldap/sort.c
tests/data/referrals.out

index 579cb35e3225449cb1151d04843e2b377a20d7ed..2e5c09e0211186a9f3066588a6c6d5a7d2d4d8bb 100644 (file)
@@ -92,20 +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;
 
        assert( ld != NULL );
 
-       count = ldap_count_entries( ld, *chain );
-
-       if ( count < 0 ) {
-               return -1;
+       /* 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;
+               }
+       }
 
-       } 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;
        }
 
@@ -115,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;
@@ -131,7 +147,6 @@ ldap_sort_entries(
 
                e = e->lm_chain;
        }
-       last = e;
 
        qsort( et, count, sizeof(struct entrything), et_cmp );
 
@@ -142,7 +157,8 @@ ldap_sort_entries(
 
                LDAP_VFREE( et[i].et_vals );
        }
-       *ep = last;
+       *ep = ohead;
+               
        LDAP_FREE( (char *) et );
 
        return( 0 );
index 1ed2e49b5d6733746476ca86d1afdffc572a806f..4fac149bb5cab4bbbe2758f3cad6fbe9d20ab758 100644 (file)
@@ -93,6 +93,9 @@ result: 0 Success
 # requesting: 1.1 
 #
 
+# University of Michigan, US
+dn: o=University of Michigan,c=US
+
 # search reference
 ref: ldap://hostA/o=abc,c=us??base
 ref: ldap://hostB/o=ABC,c=US??base
@@ -100,9 +103,6 @@ ref: ldap://hostB/o=ABC,c=US??base
 # search reference
 ref: ldap://hostC/o=xyz,c=us??base
 
-# University of Michigan, US
-dn: o=University of Michigan,c=US
-
 # search result
 search: 2
 result: 0 Success
@@ -118,21 +118,21 @@ result: 0 Success
 # requesting: 1.1 
 #
 
-# search reference
-ref: ldap://hostA/o=abc,c=us??sub
-ref: ldap://hostB/o=ABC,c=US??sub
+# Manager, University of Michigan, US
+dn: cn=Manager,o=University of Michigan,c=US
 
-# search reference
-ref: ldap://hostC/o=xyz,c=us??sub
+# University of Michigan, US
+dn: o=University of Michigan,c=US
 
 # US
 dn: c=US
 
-# University of Michigan, US
-dn: o=University of Michigan,c=US
+# search reference
+ref: ldap://hostA/o=abc,c=us??sub
+ref: ldap://hostB/o=ABC,c=US??sub
 
-# Manager, University of Michigan, US
-dn: cn=Manager,o=University of Michigan,c=US
+# search reference
+ref: ldap://hostC/o=xyz,c=us??sub
 
 # search result
 search: 2