strnstr.c (569B)
1 #include "string.h" 2 3 #include "strnstr.h" 4 5 // Origin: https://stackoverflow.com/a/25705264/2928176 6 // Author: Chris Dodd (https://stackoverflow.com/users/16406/chris-dodd) 7 char *strnstr(const char *haystack, const char *needle, size_t len) { 8 int i; 9 size_t needle_len; 10 11 if (0 == (needle_len = strnlen(needle, len))) { 12 return NULL; 13 } 14 15 for (i=0; i<=(int)(len-needle_len); i++) { 16 if ( 17 (haystack[0] == needle[0]) && 18 (0 == strncmp(haystack, needle, needle_len)) 19 ) { 20 return (char *)haystack; 21 } 22 haystack++; 23 } 24 25 return NULL; 26 }