crossroads

Git mirror of https://crossroads.e-tunity.com/
git clone git://git.finwo.net/app/crossroads
Log | Files | Refs | LICENSE

strnstr.cc (605B)


      1 #include "sys"
      2 
      3 #ifndef HAVE_STRNSTR
      4 // [KK 2008-10-13] Got this from
      5 // http://opengrok.creo.hu/dragonfly/xref/src/lib/libc/string/strnstr.c
      6 // and it's of course for systems that don't have this lib function
      7 // themselves
      8 char *strnstr(const char *s, const char *find, size_t slen) {
      9     char c, sc;
     10     size_t len;
     11 
     12     if ((c = *find++) != '\0') {
     13 	len = strlen(find);
     14 	do {
     15 	    do {
     16 		if (slen < 1 || (sc = *s) == '\0')
     17 		    return (0);
     18 		--slen;
     19 		++s;
     20 	    } while (sc != c);
     21 	    if (len > slen)
     22 		return (0);
     23 	} while (strncmp(s, find, len) != 0);
     24 	s--;
     25     }
     26     return ( (char*) s);
     27 }
     28 #endif