xref: /dflybsd-src/contrib/grep/lib/c-strcasecmp.c (revision 91b9ed38d3db6a8a8ac5b66da1d43e6e331e259a)
195b7b453SJohn Marino /* c-strcasecmp.c -- case insensitive string comparator in C locale
2*09d4459fSDaniel Fojt    Copyright (C) 1998-1999, 2005-2006, 2009-2020 Free Software Foundation, Inc.
395b7b453SJohn Marino 
495b7b453SJohn Marino    This program is free software; you can redistribute it and/or modify
595b7b453SJohn Marino    it under the terms of the GNU General Public License as published by
695b7b453SJohn Marino    the Free Software Foundation; either version 3, or (at your option)
795b7b453SJohn Marino    any later version.
895b7b453SJohn Marino 
995b7b453SJohn Marino    This program is distributed in the hope that it will be useful,
1095b7b453SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
1195b7b453SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1295b7b453SJohn Marino    GNU General Public License for more details.
1395b7b453SJohn Marino 
1495b7b453SJohn Marino    You should have received a copy of the GNU General Public License
15*09d4459fSDaniel Fojt    along with this program; if not, see <https://www.gnu.org/licenses/>.  */
1695b7b453SJohn Marino 
1795b7b453SJohn Marino #include <config.h>
1895b7b453SJohn Marino 
1995b7b453SJohn Marino /* Specification.  */
2095b7b453SJohn Marino #include "c-strcase.h"
2195b7b453SJohn Marino 
2295b7b453SJohn Marino #include <limits.h>
2395b7b453SJohn Marino 
2495b7b453SJohn Marino #include "c-ctype.h"
2595b7b453SJohn Marino 
2695b7b453SJohn Marino int
c_strcasecmp(const char * s1,const char * s2)2795b7b453SJohn Marino c_strcasecmp (const char *s1, const char *s2)
2895b7b453SJohn Marino {
2995b7b453SJohn Marino   register const unsigned char *p1 = (const unsigned char *) s1;
3095b7b453SJohn Marino   register const unsigned char *p2 = (const unsigned char *) s2;
3195b7b453SJohn Marino   unsigned char c1, c2;
3295b7b453SJohn Marino 
3395b7b453SJohn Marino   if (p1 == p2)
3495b7b453SJohn Marino     return 0;
3595b7b453SJohn Marino 
3695b7b453SJohn Marino   do
3795b7b453SJohn Marino     {
3895b7b453SJohn Marino       c1 = c_tolower (*p1);
3995b7b453SJohn Marino       c2 = c_tolower (*p2);
4095b7b453SJohn Marino 
4195b7b453SJohn Marino       if (c1 == '\0')
4295b7b453SJohn Marino         break;
4395b7b453SJohn Marino 
4495b7b453SJohn Marino       ++p1;
4595b7b453SJohn Marino       ++p2;
4695b7b453SJohn Marino     }
4795b7b453SJohn Marino   while (c1 == c2);
4895b7b453SJohn Marino 
4995b7b453SJohn Marino   if (UCHAR_MAX <= INT_MAX)
5095b7b453SJohn Marino     return c1 - c2;
5195b7b453SJohn Marino   else
5295b7b453SJohn Marino     /* On machines where 'char' and 'int' are types of the same size, the
5395b7b453SJohn Marino        difference of two 'unsigned char' values - including the sign bit -
5495b7b453SJohn Marino        doesn't fit in an 'int'.  */
5595b7b453SJohn Marino     return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
5695b7b453SJohn Marino }
57