Windows NT 4.0 source code leak
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.

87 lines
2.5 KiB

4 years ago
  1. /***
  2. *lsearch.c - linear search of an array
  3. *
  4. * Copyright (c) 1985-1991, 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. *
  29. *******************************************************************************/
  30. #include <cruntime.h>
  31. #include <stddef.h>
  32. #include <search.h>
  33. #include <memory.h>
  34. /***
  35. *char *_lsearch(key, base, num, width, compare) - do a linear search
  36. *
  37. *Purpose:
  38. * Performs a linear search on the array, looking for the value key
  39. * in an array of num elements of width bytes in size. Returns
  40. * a pointer to the array value if found; otherwise adds the
  41. * key to the end of the list.
  42. *
  43. *Entry:
  44. * char *key - key to search for
  45. * char *base - base of array to search
  46. * unsigned *num - number of elements in array
  47. * int width - number of bytes in each array element
  48. * int (*compare)() - pointer to function that compares two
  49. * array values, returning 0 if they are equal and non-0
  50. * if they are different. Two pointers to array elements
  51. * are passed to this function.
  52. *
  53. *Exit:
  54. * if key found:
  55. * returns pointer to array element
  56. * if key not found:
  57. * adds the key to the end of the list, and increments
  58. * *num.
  59. * returns pointer to new element.
  60. *
  61. *Exceptions:
  62. *
  63. *******************************************************************************/
  64. void * _CALLTYPE1 _lsearch (
  65. REG2 const void *key,
  66. REG1 void *base,
  67. REG3 unsigned int *num,
  68. unsigned int width,
  69. int (_CALLTYPE1 *compare)(const void *, const void *)
  70. )
  71. {
  72. unsigned int place = 0;
  73. while (place < *num )
  74. if (!(*compare)(key,base))
  75. return(base);
  76. else
  77. {
  78. base = (char *)base + width;
  79. place++;
  80. }
  81. (void) memcpy( base, key, width );
  82. (*num)++;
  83. return( base );
  84. }