Source code of Windows XP (NT5)
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.

80 lines
2.3 KiB

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