
#include <ctype.h>
#include <stdio.h>
#include <sys/types.h>

int
strcasecmp (p1, p2)
     const char *p1;
     const char *p2;
{
  unsigned char c1, c2;

  if (p1 == p2)
    return 0;

  do
    {
      /* I know this seems to be odd but the tolower() function in
	 some systems libc cannot handle nonalpha characters.  */
      c1 = isupper (*p1) ? tolower (*p1) : *p1;
      c2 = isupper (*p2) ? tolower (*p2) : *p2;
      if (c1 == '\0')
	break;
      ++p1;
      ++p2;
    }
  while (c1 == c2);

  return c1 - c2;
}

int strncasecmp(const char *p1, const char *p2, int n)
{
    if (n == 0)
        return 0;
    while(n-- != 0 && tolower(*p1) == tolower(*p2) )
    {
        if (n == 0 || *p1 == '\0' || *p2 == '\0' )
            break;
        p1++;
        p2++;
    }
    return tolower(*(unsigned char *) p1) - tolower(*(unsigned char *) p2);
}

