Blame gl/strncasecmp.c

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