Blame lib/strncasecmp.c

Packit 33f14e
/* strncasecmp.c -- case insensitive string comparator
Packit 33f14e
   Copyright (C) 1998-1999, 2005-2007, 2009-2017 Free Software Foundation, Inc.
Packit 33f14e
Packit 33f14e
   This program is free software; you can redistribute it and/or modify
Packit 33f14e
   it under the terms of the GNU General Public License as published by
Packit 33f14e
   the Free Software Foundation; either version 3, or (at your option)
Packit 33f14e
   any later version.
Packit 33f14e
Packit 33f14e
   This program is distributed in the hope that it will be useful,
Packit 33f14e
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 33f14e
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 33f14e
   GNU General Public License for more details.
Packit 33f14e
Packit 33f14e
   You should have received a copy of the GNU General Public License
Packit 33f14e
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit 33f14e
Packit 33f14e
#include <config.h>
Packit 33f14e
Packit 33f14e
/* Specification.  */
Packit 33f14e
#include <string.h>
Packit 33f14e
Packit 33f14e
#include <ctype.h>
Packit 33f14e
#include <limits.h>
Packit 33f14e
Packit 33f14e
#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
Packit 33f14e
Packit 33f14e
/* Compare no more than N bytes of strings S1 and S2, ignoring case,
Packit 33f14e
   returning less than, equal to or greater than zero if S1 is
Packit 33f14e
   lexicographically less than, equal to or greater than S2.
Packit 33f14e
   Note: This function cannot work correctly in multibyte locales.  */
Packit 33f14e
Packit 33f14e
int
Packit 33f14e
strncasecmp (const char *s1, const char *s2, size_t n)
Packit 33f14e
{
Packit 33f14e
  register const unsigned char *p1 = (const unsigned char *) s1;
Packit 33f14e
  register const unsigned char *p2 = (const unsigned char *) s2;
Packit 33f14e
  unsigned char c1, c2;
Packit 33f14e
Packit 33f14e
  if (p1 == p2 || n == 0)
Packit 33f14e
    return 0;
Packit 33f14e
Packit 33f14e
  do
Packit 33f14e
    {
Packit 33f14e
      c1 = TOLOWER (*p1);
Packit 33f14e
      c2 = TOLOWER (*p2);
Packit 33f14e
Packit 33f14e
      if (--n == 0 || c1 == '\0')
Packit 33f14e
        break;
Packit 33f14e
Packit 33f14e
      ++p1;
Packit 33f14e
      ++p2;
Packit 33f14e
    }
Packit 33f14e
  while (c1 == c2);
Packit 33f14e
Packit 33f14e
  if (UCHAR_MAX <= INT_MAX)
Packit 33f14e
    return c1 - c2;
Packit 33f14e
  else
Packit 33f14e
    /* On machines where 'char' and 'int' are types of the same size, the
Packit 33f14e
       difference of two 'unsigned char' values - including the sign bit -
Packit 33f14e
       doesn't fit in an 'int'.  */
Packit 33f14e
    return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
Packit 33f14e
}