sort.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* sort.c -- LDAP library entry and value sort routines */
  2. /* $OpenLDAP$ */
  3. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  4. *
  5. * Copyright 1998-2024 The OpenLDAP Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted only as authorized by the OpenLDAP
  10. * Public License.
  11. *
  12. * A copy of this license is available in the file LICENSE in the
  13. * top-level directory of the distribution or, alternatively, at
  14. * <http://www.OpenLDAP.org/license.html>.
  15. */
  16. /* Portions Copyright (c) 1994 Regents of the University of Michigan.
  17. * All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms are permitted
  20. * provided that this notice is preserved and that due credit is given
  21. * to the University of Michigan at Ann Arbor. The name of the University
  22. * may not be used to endorse or promote products derived from this
  23. * software without specific prior written permission. This software
  24. * is provided ``as is'' without express or implied warranty.
  25. */
  26. #include "portable.h"
  27. #include <stdio.h>
  28. #include <ac/stdlib.h>
  29. #include <ac/ctype.h>
  30. #include <ac/string.h>
  31. #include <ac/time.h>
  32. #include "ldap-int.h"
  33. struct entrything {
  34. char **et_vals;
  35. LDAPMessage *et_msg;
  36. int (*et_cmp_fn) LDAP_P((const char *a, const char *b));
  37. };
  38. static int et_cmp LDAP_P(( const void *aa, const void *bb));
  39. int
  40. ldap_sort_strcasecmp(
  41. LDAP_CONST void *a,
  42. LDAP_CONST void *b
  43. )
  44. {
  45. return( strcasecmp( *(char *const *)a, *(char *const *)b ) );
  46. }
  47. static int
  48. et_cmp(
  49. const void *aa,
  50. const void *bb
  51. )
  52. {
  53. int i, rc;
  54. const struct entrything *a = (const struct entrything *)aa;
  55. const struct entrything *b = (const struct entrything *)bb;
  56. if ( a->et_vals == NULL && b->et_vals == NULL )
  57. return( 0 );
  58. if ( a->et_vals == NULL )
  59. return( -1 );
  60. if ( b->et_vals == NULL )
  61. return( 1 );
  62. for ( i = 0; a->et_vals[i] && b->et_vals[i]; i++ ) {
  63. if ( (rc = a->et_cmp_fn( a->et_vals[i], b->et_vals[i] )) != 0 ) {
  64. return( rc );
  65. }
  66. }
  67. if ( a->et_vals[i] == NULL && b->et_vals[i] == NULL )
  68. return( 0 );
  69. if ( a->et_vals[i] == NULL )
  70. return( -1 );
  71. return( 1 );
  72. }
  73. int
  74. ldap_sort_entries(
  75. LDAP *ld,
  76. LDAPMessage **chain,
  77. LDAP_CONST char *attr, /* NULL => sort by DN */
  78. int (*cmp) (LDAP_CONST char *, LDAP_CONST char *)
  79. )
  80. {
  81. int i, count = 0;
  82. struct entrything *et;
  83. LDAPMessage *e, *ehead = NULL, *etail = NULL;
  84. LDAPMessage *ohead = NULL, *otail = NULL;
  85. LDAPMessage **ep;
  86. assert( ld != NULL );
  87. /* Separate entries from non-entries */
  88. for ( e = *chain; e; e=e->lm_chain ) {
  89. if ( e->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
  90. count++;
  91. if ( !ehead ) ehead = e;
  92. if ( etail ) etail->lm_chain = e;
  93. etail = e;
  94. } else {
  95. if ( !ohead ) ohead = e;
  96. if ( otail ) otail->lm_chain = e;
  97. otail = e;
  98. }
  99. }
  100. if ( count < 2 ) {
  101. /* zero or one entries -- already sorted! */
  102. if ( ehead ) {
  103. etail->lm_chain = ohead;
  104. *chain = ehead;
  105. } else {
  106. *chain = ohead;
  107. }
  108. return 0;
  109. }
  110. if ( (et = (struct entrything *) LDAP_MALLOC( count *
  111. sizeof(struct entrything) )) == NULL ) {
  112. ld->ld_errno = LDAP_NO_MEMORY;
  113. return( -1 );
  114. }
  115. e = ehead;
  116. for ( i = 0; i < count; i++ ) {
  117. et[i].et_cmp_fn = cmp;
  118. et[i].et_msg = e;
  119. if ( attr == NULL ) {
  120. char *dn;
  121. dn = ldap_get_dn( ld, e );
  122. et[i].et_vals = ldap_explode_dn( dn, 1 );
  123. LDAP_FREE( dn );
  124. } else {
  125. et[i].et_vals = ldap_get_values( ld, e, attr );
  126. }
  127. e = e->lm_chain;
  128. }
  129. qsort( et, count, sizeof(struct entrything), et_cmp );
  130. ep = chain;
  131. for ( i = 0; i < count; i++ ) {
  132. *ep = et[i].et_msg;
  133. ep = &(*ep)->lm_chain;
  134. LDAP_VFREE( et[i].et_vals );
  135. }
  136. *ep = ohead;
  137. (*chain)->lm_chain_tail = otail ? otail : etail;
  138. LDAP_FREE( (char *) et );
  139. return( 0 );
  140. }
  141. int
  142. ldap_sort_values(
  143. LDAP *ld,
  144. char **vals,
  145. int (*cmp) (LDAP_CONST void *, LDAP_CONST void *)
  146. )
  147. {
  148. int nel;
  149. for ( nel = 0; vals[nel] != NULL; nel++ )
  150. ; /* NULL */
  151. qsort( vals, nel, sizeof(char *), cmp );
  152. return( 0 );
  153. }