Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.7 KiB

  1. /***
  2. *lsearch.c - linear search of an array
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * contains the _lsearch() function - linear search of an array
  8. *
  9. *Revision History:
  10. * 06-19-85 TC initial version
  11. * 05-14-87 JMB added function pragma for memcpy in compact/large mode
  12. * for huge pointer support
  13. * include sizeptr.h for SIZED definition
  14. * 08-01-87 SKS Add include file for prototype of memcpy()
  15. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  16. * 01-21-88 JCR Backed out _LOAD_DS...
  17. * 10-30-89 JCR Added _cdecl to prototypes
  18. * 03-14-90 GJF Replaced _cdecl with _CALLTYPE1, added #include
  19. * <cruntime.h>, removed #include <register.h> and
  20. * fixed the copyright. Also, cleaned up the formatting
  21. * a bit.
  22. * 04-05-90 GJF Added #include <search.h> and fixed the resulting
  23. * compiler errors and warnings. Removed unreferenced
  24. * local variable. Also, removed #include <sizeptr.h>.
  25. * 07-25-90 SBM Replaced <stdio.h> by <stddef.h>
  26. * 10-04-90 GJF New-style function declarator.
  27. * 01-17-91 GJF ANSI naming.
  28. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  29. *
  30. *******************************************************************************/
  31. #include <cruntime.h>
  32. #include <stddef.h>
  33. #include <search.h>
  34. #include <memory.h>
  35. /***
  36. *char *_lsearch(key, base, num, width, compare) - do a linear search
  37. *
  38. *Purpose:
  39. * Performs a linear search on the array, looking for the value key
  40. * in an array of num elements of width bytes in size. Returns
  41. * a pointer to the array value if found; otherwise adds the
  42. * key to the end of the list.
  43. *
  44. *Entry:
  45. * char *key - key to search for
  46. * char *base - base of array to search
  47. * unsigned *num - number of elements in array
  48. * int width - number of bytes in each array element
  49. * int (*compare)() - pointer to function that compares two
  50. * array values, returning 0 if they are equal and non-0
  51. * if they are different. Two pointers to array elements
  52. * are passed to this function.
  53. *
  54. *Exit:
  55. * if key found:
  56. * returns pointer to array element
  57. * if key not found:
  58. * adds the key to the end of the list, and increments
  59. * *num.
  60. * returns pointer to new element.
  61. *
  62. *Exceptions:
  63. *
  64. *******************************************************************************/
  65. void * __cdecl _lsearch (
  66. REG2 const void *key,
  67. REG1 void *base,
  68. REG3 unsigned int *num,
  69. unsigned int width,
  70. int (__cdecl *compare)(const void *, const void *)
  71. )
  72. {
  73. unsigned int place = 0;
  74. while (place < *num )
  75. if (!(*compare)(key,base))
  76. return(base);
  77. else
  78. {
  79. base = (char *)base + width;
  80. place++;
  81. }
  82. (void) memcpy( base, key, width );
  83. (*num)++;
  84. return( base );
  85. }