xref: /netbsd-src/external/gpl2/gettext/dist/gettext-runtime/gnulib-lib/c-strncasecmp.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1*946379e7Schristos /* c-strncasecmp.c -- case insensitive string comparator in C locale
2*946379e7Schristos    Copyright (C) 1998-1999, 2005-2006 Free Software Foundation, Inc.
3*946379e7Schristos 
4*946379e7Schristos    This program is free software; you can redistribute it and/or modify
5*946379e7Schristos    it under the terms of the GNU General Public License as published by
6*946379e7Schristos    the Free Software Foundation; either version 2, or (at your option)
7*946379e7Schristos    any later version.
8*946379e7Schristos 
9*946379e7Schristos    This program is distributed in the hope that it will be useful,
10*946379e7Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*946379e7Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*946379e7Schristos    GNU General Public License for more details.
13*946379e7Schristos 
14*946379e7Schristos    You should have received a copy of the GNU General Public License
15*946379e7Schristos    along with this program; if not, write to the Free Software Foundation,
16*946379e7Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17*946379e7Schristos 
18*946379e7Schristos #include <config.h>
19*946379e7Schristos 
20*946379e7Schristos /* Specification.  */
21*946379e7Schristos #include "c-strcase.h"
22*946379e7Schristos 
23*946379e7Schristos #include <limits.h>
24*946379e7Schristos 
25*946379e7Schristos #include "c-ctype.h"
26*946379e7Schristos 
27*946379e7Schristos int
c_strncasecmp(const char * s1,const char * s2,size_t n)28*946379e7Schristos c_strncasecmp (const char *s1, const char *s2, size_t n)
29*946379e7Schristos {
30*946379e7Schristos   register const unsigned char *p1 = (const unsigned char *) s1;
31*946379e7Schristos   register const unsigned char *p2 = (const unsigned char *) s2;
32*946379e7Schristos   unsigned char c1, c2;
33*946379e7Schristos 
34*946379e7Schristos   if (p1 == p2 || n == 0)
35*946379e7Schristos     return 0;
36*946379e7Schristos 
37*946379e7Schristos   do
38*946379e7Schristos     {
39*946379e7Schristos       c1 = c_tolower (*p1);
40*946379e7Schristos       c2 = c_tolower (*p2);
41*946379e7Schristos 
42*946379e7Schristos       if (--n == 0 || c1 == '\0')
43*946379e7Schristos 	break;
44*946379e7Schristos 
45*946379e7Schristos       ++p1;
46*946379e7Schristos       ++p2;
47*946379e7Schristos     }
48*946379e7Schristos   while (c1 == c2);
49*946379e7Schristos 
50*946379e7Schristos   if (UCHAR_MAX <= INT_MAX)
51*946379e7Schristos     return c1 - c2;
52*946379e7Schristos   else
53*946379e7Schristos     /* On machines where 'char' and 'int' are types of the same size, the
54*946379e7Schristos        difference of two 'unsigned char' values - including the sign bit -
55*946379e7Schristos        doesn't fit in an 'int'.  */
56*946379e7Schristos     return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
57*946379e7Schristos }
58