summaryrefslogtreecommitdiff
path: root/lib/libc/string/memmem.c
blob: 4f55cf4162fba7899816a9c1d081fb3f1e9284a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stddef.h" // for NULL

#include <string.h> // for memcmp, size_t, memmem

void *memmem(const void *haystack, size_t haystacklen, const void *needle,
	     size_t needlelen)
{
	const unsigned char *p1 = haystack;
	const unsigned char *p2 = needle;
	while (haystacklen >= needlelen) {
		if (!memcmp(p1, p2, needlelen))
			return (void *)p1;
		p1++;
		haystacklen--;
	}
	return NULL;
}