summaryrefslogtreecommitdiff
path: root/missing/strstr.c
blob: 1673518f064aec2f126c83f56ef77422c361838f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* public domain rewrite of strstr(3) */

char *
strstr(haystack, needle)
    char *haystack, *needle;
{
    char *hend;
    char *a, *b;

    if (*needle == 0) return haystack;
    hend = haystack + strlen(haystack) - strlen(needle) + 1;
    while (haystack < hend) {
	if (*haystack == *needle) {
	    a = haystack;
	    b = needle;
	    for (;;) {
		if (*b == 0) return haystack;
		if (*a++ != *b++) {
		    break;
		}
	    }
	}
	haystack++;
    }
    return 0;
}