summaryrefslogtreecommitdiff
path: root/missing/strstr.c
blob: cdb6bc9a5520b824e8f2f05597ae877fb5b67c50 (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
27
28
/* public domain rewrite of strstr(3) */

#include "missing.h"

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;
}