skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

str_strn.c (408B)


      1 /* ISC license. */
      2 
      3 #include <string.h>
      4 #include <skalibs/bytestr.h>
      5 
      6 size_t str_strn (char const *haystack, size_t hlen, char const *needle, size_t nlen)
      7 {
      8   char haystack2[hlen+1] ;
      9   char needle2[nlen+1] ;
     10   char *p ;
     11   memcpy(haystack2, haystack, hlen) ; haystack2[hlen] = 0 ;
     12   memcpy(needle2, needle, nlen) ; needle2[nlen] = 0 ;
     13   p = strstr(haystack2, needle2) ;
     14   return p ? p - haystack2 : hlen ;
     15 }