Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
  28. *               DESCRIBE IT HERE!
  29. *
  30. ****************************************************************************/
  31.  
  32.  
  33. #include "variety.h"
  34. #undef __INLINE_FUNCTIONS__
  35. #include <stddef.h>
  36. #include <stdlib.h>
  37. #include "extfunc.h"
  38.  
  39. /*
  40.     The old bsearch could SIGSEGV if the compare is done on elements
  41.     which are not within the given segment.  The error occurs if the search
  42.     key is larger than the 'high' element (or lower than the 'low' member)
  43.     and 'low','mid' and 'high' are all equal (either from the start or
  44.     after several iterations).
  45. */
  46.  
  47. typedef int bcomp();
  48. #if defined(_M_IX86)
  49.     #pragma aux (__outside_CLIB) bcomp;
  50. #endif
  51.  
  52. _WCRTLINK void * bsearch( const void * key,
  53.                 const void * base,
  54.                 size_t nmemb,
  55.                 size_t size,
  56.                 int (*cmp)() )
  57.     {
  58.         char *low;
  59.         char *high;
  60.         char *mid;
  61.         int cond;
  62.         bcomp *compar = cmp;
  63.  
  64.         if( nmemb == 0 ) {
  65.             return( NULL );
  66.         }
  67.         low = (char *) base;
  68.         high = low + (nmemb-1) * size;
  69. // JBS  while( low <= high ) {
  70.         while( low < high ) {
  71.             mid = low + ( (high-low)/size/2 ) * size;
  72.             cond = (*compar)( key, mid );
  73.             if( cond == 0 ) return( mid );
  74.             if( cond < 0 ) {    /* key < mid */
  75. // JBS          high = mid - size;
  76.                 high = mid;
  77.             } else {            /* key > mid */
  78.                 low = mid + size;
  79.             }
  80.         }
  81.         if (low == high) return( (*compar)(key, low) ? NULL : low );
  82.         return( NULL );
  83.     }
  84.