Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * The memmem() function finds the start of the first occurrence of the
- * substring 'needle' of length 'nlen' in the memory area 'haystack' of
- * length 'hlen'.
- *
- * The return value is a pointer to the beginning of the sub-string, or
- * NULL if the substring is not found.
- */
- char* memmem(char* haystack, size_t hlen, char* needle, size_t nlen)
- {
- int needle_first;
- char *p = haystack;
- size_t plen = hlen;
- if (!nlen)
- return NULL;
- needle_first = *(unsigned char *)needle;
- while (plen >= nlen && (p = memchr(p, needle_first, plen - nlen + 1)) != NULL)
- {
- if (!memcmp(p, needle, nlen))
- return (char*)p;
- p++;
- plen = hlen - (p - haystack);
- }
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement