change script variable name to avoid collision with PATH in windows
[git/mingw/4msysgit/gitPS1fix.git] / compat / memmem.c
blob56bcb4277f47c295993c853a84edfe45e6aa3911
1 #include "../git-compat-util.h"
3 void *gitmemmem(const void *haystack, size_t haystack_len,
4 const void *needle, size_t needle_len)
6 const char *begin = haystack;
7 const char *last_possible = begin + haystack_len - needle_len;
8 const char *tail = needle;
9 char point;
12 * The first occurrence of the empty string is deemed to occur at
13 * the beginning of the string.
15 if (needle_len == 0)
16 return (void *)begin;
19 * Sanity check, otherwise the loop might search through the whole
20 * memory.
22 if (haystack_len < needle_len)
23 return NULL;
25 point = *tail++;
26 for (; begin <= last_possible; begin++) {
27 if (*begin == point && !memcmp(begin + 1, tail, needle_len - 1))
28 return (void *)begin;
31 return NULL;