/* Search string BIG (length BLEN) for an occurrence of
string SMALL (length SLEN). Return a pointer to the
beginning of the first occurrence, or return nil if none found.
If blen (or slen) is 0 then the whole string BIG (or SMALL) is considered */ 

char *
sindex (big, blen, small, slen)
const char *big;
unsigned int blen;
const char *small;
unsigned int slen;
{
  char* saved_big = 0;
  char* saved_small = 0;
  const char* haystack;
  const char* needle;

  if(blen) {
    saved_big = savestring (big, blen);
    haystack = saved_big;
  }
  else
    haystack = big;

  if(slen) {
    saved_small = savestring (small, slen);
    needle = saved_small;
  }
  else
    needle = small;

  char* pos = strstr(haystack, needle);

  if( saved_small )
    free(saved_small);

  if( saved_big )
    free(saved_big);

  if(pos==NULL)
    return NULL;

  int offset = pos - haystack;

  return (char*)(big+offset); /* casting because "big" had a const qualifier */
} 
