]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
tools/nolibc: add strstr()
authorThomas Weißschuh <thomas.weissschuh@linutronix.de>
Mon, 28 Apr 2025 12:40:02 +0000 (14:40 +0200)
committerThomas Weißschuh <linux@weissschuh.net>
Wed, 21 May 2025 13:32:00 +0000 (15:32 +0200)
This is used in various selftests and will be handy when integrating
those with nolibc.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://lore.kernel.org/r/20250428-nolibc-misc-v2-1-3c043eeab06c@linutronix.de
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
tools/include/nolibc/string.h
tools/testing/selftests/nolibc/nolibc-test.c

index febfd69789666e2445b82423a5f4df5e18a1c774..163a17e7dd38b48ba8d9218e10733b3600710477 100644 (file)
@@ -292,6 +292,26 @@ char *strrchr(const char *s, int c)
        return (char *)ret;
 }
 
+static __attribute__((unused))
+char *strstr(const char *haystack, const char *needle)
+{
+       size_t len_haystack, len_needle;
+
+       len_needle = strlen(needle);
+       if (!len_needle)
+               return NULL;
+
+       len_haystack = strlen(haystack);
+       while (len_haystack >= len_needle) {
+               if (!memcmp(haystack, needle, len_needle))
+                       return (char *)haystack;
+               haystack++;
+               len_haystack--;
+       }
+
+       return NULL;
+}
+
 static __attribute__((unused))
 int tolower(int c)
 {
index 1ad0db92f0ed47f708363b2e558717fa0e686b8f..3e15a25ccddf5135db1f59bce1eefdff2ffe57f6 100644 (file)
@@ -1211,6 +1211,9 @@ int run_stdlib(int min, int max)
                CASE_TEST(strlcpy_2);          EXPECT_STRBUFEQ(is_nolibc, strlcpy(buf, "bar", 2), buf, 3, "b"); break;
                CASE_TEST(strlcpy_3);          EXPECT_STRBUFEQ(is_nolibc, strlcpy(buf, "bar", 3), buf, 3, "ba"); break;
                CASE_TEST(strlcpy_4);          EXPECT_STRBUFEQ(is_nolibc, strlcpy(buf, "bar", 4), buf, 3, "bar"); break;
+               CASE_TEST(strstr_foobar_foo);  EXPECT_STREQ(1, strstr("foobar", "foo"), "foobar"); break;
+               CASE_TEST(strstr_foobar_bar);  EXPECT_STREQ(1, strstr("foobar", "bar"), "bar"); break;
+               CASE_TEST(strstr_foobar_baz);  EXPECT_PTREQ(1, strstr("foobar", "baz"), NULL); break;
                CASE_TEST(memcmp_20_20);       EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20", 4), 0); break;
                CASE_TEST(memcmp_20_60);       EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60", 4), 0); break;
                CASE_TEST(memcmp_60_20);       EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20", 4), 0); break;