about summary refs log tree commit diff
path: root/src/libposixplz/strcasestr.c
blob: 23472679e4b63e567bb93babd11330bfd3058db0 (plain) (blame)
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
/* ISC license. */

#include <skalibs/sysdeps.h>

#ifndef SKALIBS_HASSTRCASESTR

#include <string.h>

#include <skalibs/bytestr.h>

 /* XXX: copies strings on the stack, careful with long strings */

char *strcasestr (char const *haystack, char const *needle)
{
  size_t nlen = strlen(needle) ;
  size_t hlen = strlen(haystack) ;
  char *p ;
  char lneedle[nlen + 1] ;
  char lhaystack[hlen + 1] ;
  memcpy(lneedle, needle, nlen + 1) ;
  memcpy(lhaystack, haystack, hlen + 1) ;
  case_lowerb(lneedle, nlen) ;
  case_lowerb(lhaystack, hlen) ;
  p = strstr(lhaystack, lneedle) ;
  return p ? haystack + (p - lhaystack) : 0 ;
}

#endif