/*
 *    Implementation of strcasecmp and strncasecmp for platforms
 *    that do not have them. Adapted from intl/localealias.c.
 *    29-Oct-03 MW
 */

#ifndef HAVE_STRCASECMP

#include <ctype.h>
#include <stdlib.h>

int strcasecmp(const char *s1, const char *s2)
{
  unsigned char c1, c2;

  if (s1 == s2)
    return 0;

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

  return c1 - c2;
}

int strncasecmp(const char *s1, const char *s2, size_t n)
{
  unsigned char c1, c2;

  if (s1 == s2 || !n)
    return 0;

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

  return c1 - c2;
}

#endif
