[PATCH] msvcrt: Add _lfind_s

Daniel Lehman dlehman25 at gmail.com
Tue Nov 22 09:36:19 CST 2016


Signed-off-by: Daniel Lehman <dlehman25 at gmail.com>
---
 dlls/msvcrt/misc.c       | 26 +++++++++++++++++
 dlls/msvcrt/msvcrt.spec  |  2 +-
 dlls/msvcrt/tests/misc.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 102 insertions(+), 1 deletion(-)

diff --git a/dlls/msvcrt/misc.c b/dlls/msvcrt/misc.c
index c1510ef..23a089a 100644
--- a/dlls/msvcrt/misc.c
+++ b/dlls/msvcrt/misc.c
@@ -103,6 +103,32 @@ void* CDECL _lfind(const void* match, const void* start,
 }
 
 /*********************************************************************
+ *		_lfind_s (MSVCRT.@)
+ */
+void* CDECL _lfind_s(const void* match, const void* start,
+                   unsigned int* array_size, unsigned int elem_size,
+                   int (CDECL *cf)(void*,const void*,const void*),
+                   void* context)
+{
+  unsigned int size;
+  if (!MSVCRT_CHECK_PMT(match != NULL)) return NULL;
+  if (!MSVCRT_CHECK_PMT(array_size != NULL)) return NULL;
+  if (!MSVCRT_CHECK_PMT(start != NULL || *array_size == 0)) return NULL;
+  if (!MSVCRT_CHECK_PMT(cf != NULL)) return NULL;
+  if (!MSVCRT_CHECK_PMT(elem_size != 0)) return NULL;
+
+  size = *array_size;
+  if (size)
+    do
+    {
+      if (cf(context, match, start) == 0)
+        return (void *)start; /* found */
+      start = (const char *)start + elem_size;
+    } while (--size);
+  return NULL;
+}
+
+/*********************************************************************
  *		_lsearch (MSVCRT.@)
  */
 void* CDECL _lsearch(const void* match, void* start,
diff --git a/dlls/msvcrt/msvcrt.spec b/dlls/msvcrt/msvcrt.spec
index 0c1d4ad..d38b0fa 100644
--- a/dlls/msvcrt/msvcrt.spec
+++ b/dlls/msvcrt/msvcrt.spec
@@ -655,7 +655,7 @@
 @ cdecl _jn(long double) MSVCRT__jn
 @ cdecl _kbhit()
 @ cdecl _lfind(ptr ptr ptr long ptr)
-# stub _lfind_s(ptr ptr ptr long ptr ptr)
+@ cdecl _lfind_s(ptr ptr ptr long ptr ptr)
 @ cdecl _loaddll(str)
 @ cdecl -arch=x86_64 _local_unwind(ptr ptr)
 @ cdecl -arch=i386 _local_unwind2(ptr long)
diff --git a/dlls/msvcrt/tests/misc.c b/dlls/msvcrt/tests/misc.c
index 77f2873..73cf1a0 100644
--- a/dlls/msvcrt/tests/misc.c
+++ b/dlls/msvcrt/tests/misc.c
@@ -59,6 +59,8 @@ static void (__cdecl *p_qsort_s)(void*, MSVCRT_size_t, MSVCRT_size_t,
 static double (__cdecl *p_atan)(double);
 static double (__cdecl *p_exp)(double);
 static double (__cdecl *p_tanh)(double);
+static void *(__cdecl *p_lfind_s)(const void*, const void*, unsigned int*,
+        size_t, int (__cdecl *)(void*, const void*, const void*), void*);
 
 static void init(void)
 {
@@ -76,6 +78,7 @@ static void init(void)
     p_atan = (void *)GetProcAddress(hmod, "atan");
     p_exp = (void *)GetProcAddress(hmod, "exp");
     p_tanh = (void *)GetProcAddress(hmod, "tanh");
+    p_lfind_s = (void *)GetProcAddress(hmod, "_lfind_s");
 }
 
 static void test_rand_s(void)
@@ -597,6 +600,77 @@ static void test_thread_handle_close(void)
     ok(ret, "ret = %d\n", ret);
 }
 
+static int __cdecl _lfind_s_comp(void *ctx, const void *l, const void *r)
+{
+    *(int *)ctx = 0xdeadc0de;
+    return *(int *)l - *(int *)r;
+}
+
+static void test__lfind_s(void)
+{
+    static const int tests[] = {9000, 8001, 7002, 6003, 1003, 5004, 4005, 3006, 2007};
+    unsigned int num;
+    void *found;
+    int ctx;
+    int key;
+
+    if (!p_lfind_s)
+    {
+        win_skip("_lfind_s is not available\n");
+        return;
+    }
+
+    key = 1234;
+    num = sizeof(tests)/sizeof(tests[0]);
+
+    /* invalid args */
+    errno = 0xdeadbeef;
+    found = p_lfind_s(NULL, tests, &num, sizeof(int), _lfind_s_comp, NULL);
+    ok(errno == EINVAL, "errno = %d\n", errno);
+    ok(!found, "Expected NULL, got %p\n", found);
+
+    errno = 0xdeadbeef;
+    found = p_lfind_s(&key, NULL, &num, sizeof(int), _lfind_s_comp, NULL);
+    ok(errno == EINVAL, "errno = %d\n", errno);
+    ok(!found, "Expected NULL, got %p\n", found);
+
+    errno = 0xdeadbeef;
+    found = p_lfind_s(&key, tests, &num, 0, _lfind_s_comp, NULL);
+    ok(errno == EINVAL, "errno = %d\n", errno);
+    ok(!found, "Expected NULL, got %p\n", found);
+
+    errno = 0xdeadbeef;
+    found = p_lfind_s(&key, tests, &num, sizeof(int), NULL, NULL);
+    ok(errno == EINVAL, "errno = %d\n", errno);
+    ok(!found, "Expected NULL, got %p\n", found);
+
+    /* simple samples */
+    ctx = -1;
+    key = 9000;
+    errno = 0xdeadbeef;
+    found = p_lfind_s(&key, tests, &num, sizeof(int), _lfind_s_comp, &ctx);
+    ok(errno == 0xdeadbeef, "errno = %d\n", errno);
+    ok(found == tests, "Expected %p, got %p\n", tests, found);
+    ok(ctx == 0xdeadc0de, "Expected 0xdeadc0de, got %x\n", ctx);
+
+    ctx = -1;
+    key = 2007;
+    errno = 0xdeadbeef;
+    found = p_lfind_s(&key, tests, &num, sizeof(int), _lfind_s_comp, &ctx);
+    ok(errno == 0xdeadbeef, "errno = %d\n", errno);
+    ok(found == tests+8, "Expected %p, got %p\n", tests+8, found);
+    ok(ctx == 0xdeadc0de, "Expected 0xdeadc0de, got %x\n", ctx);
+
+    /* simple sample - not found */
+    ctx = -1;
+    key = 1234;
+    errno = 0xdeadbeef;
+    found = p_lfind_s(&key, tests, &num, sizeof(int), _lfind_s_comp, &ctx);
+    ok(errno == 0xdeadbeef, "errno = %d\n", errno);
+    ok(!found, "Expected NULL, got %p\n", found);
+    ok(ctx == 0xdeadc0de, "Expected 0xdeadc0de, got %x\n", ctx);
+}
+
 START_TEST(misc)
 {
     int arg_c;
@@ -626,4 +700,5 @@ START_TEST(misc)
     test_qsort_s();
     test_math_functions();
     test_thread_handle_close();
+    test__lfind_s();
 }
-- 
2.7.4




More information about the wine-patches mailing list