Blame lib/strncasecmp.c

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