#include <string.h>
#include <malloc.h>
#include "util.h"

unsigned long malloc_allocated_bytes = 0;
unsigned long strdup_allocated_bytes = 0;

void *
mymalloc (size_t size)
{
  malloc_allocated_bytes += size;
  return malloc(size);
}

char *
mystrdup (char *s)
{
  int len;
  char *ns;

  len = strlen(s) + 1;
  if ((ns = (char*) malloc (len)) != 0)
  {
    memcpy(ns, s, len);
    strdup_allocated_bytes += len;
  }
  return ns;
}

/*
 * Same as strstr(3C), but not case sensitive
 */

char *
strcasestr (const char *s1, const char *s2)
{
  int i = strlen(s2);
  while (*s1)
  {
    if (!strncasecmp(s1, s2, i))
      return (char*)s1;
    s1++;
  }
  return 0;
}

/*
 * Similar to memchr(3C), but it goes backwards from p to s
 */

char *
memlchr (const char *s, const char *p, char c)
{
  while (p >= s)
  {
    if (*p == c)
      return (char*)p;
    p--;
  }
  return 0;
}
