1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* String in string (limited search length) |
6 |
*/ |
7 |
|
8 |
#include "copyright.h" |
9 |
#include "rtio.h" |
10 |
|
11 |
char * |
12 |
strnstr(const char *haystack, const char *needle, size_t len) |
13 |
{ |
14 |
const size_t nlen = strlen(needle); |
15 |
const char *spos = haystack; |
16 |
int i; |
17 |
|
18 |
if (!nlen) { /* special case */ |
19 |
while (len--) { |
20 |
if (!*spos) |
21 |
return((char *)spos); |
22 |
++spos; |
23 |
} |
24 |
return(NULL); /* haystack is longer than len */ |
25 |
} |
26 |
if (len < nlen) /* not long enough for match! */ |
27 |
return(NULL); |
28 |
len -= nlen; |
29 |
do { |
30 |
for (i = 0; i < nlen; i++) |
31 |
if (spos[i] != needle[i]) |
32 |
break; |
33 |
|
34 |
if (i == nlen) /* found match? */ |
35 |
return((char *)spos); |
36 |
|
37 |
} while (((spos++)[i] != '\0') & (len-- != 0)); |
38 |
|
39 |
return(NULL); /* no match found */ |
40 |
} |