xref: /netbsd-src/external/gpl2/diffutils/dist/lib/strcasecmp.c (revision 75f6d617e282811cb173c2ccfbf5df0dd71f7045)
1*75f6d617Schristos /*	$NetBSD: strcasecmp.c,v 1.1.1.1 2016/01/13 03:15:30 christos Exp $	*/
2*75f6d617Schristos 
3*75f6d617Schristos /* strcasecmp.c -- case insensitive string comparator
4*75f6d617Schristos    Copyright (C) 1998, 1999 Free Software Foundation, Inc.
5*75f6d617Schristos 
6*75f6d617Schristos    This program is free software; you can redistribute it and/or modify
7*75f6d617Schristos    it under the terms of the GNU General Public License as published by
8*75f6d617Schristos    the Free Software Foundation; either version 2, or (at your option)
9*75f6d617Schristos    any later version.
10*75f6d617Schristos 
11*75f6d617Schristos    This program is distributed in the hope that it will be useful,
12*75f6d617Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*75f6d617Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*75f6d617Schristos    GNU General Public License for more details.
15*75f6d617Schristos 
16*75f6d617Schristos    You should have received a copy of the GNU General Public License
17*75f6d617Schristos    along with this program; if not, write to the Free Software Foundation,
18*75f6d617Schristos    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19*75f6d617Schristos 
20*75f6d617Schristos #if HAVE_CONFIG_H
21*75f6d617Schristos # include <config.h>
22*75f6d617Schristos #endif
23*75f6d617Schristos 
24*75f6d617Schristos #ifdef LENGTH_LIMIT
25*75f6d617Schristos # define STRXCASECMP_FUNCTION strncasecmp
26*75f6d617Schristos # define STRXCASECMP_DECLARE_N , size_t n
27*75f6d617Schristos # define LENGTH_LIMIT_EXPR(Expr) Expr
28*75f6d617Schristos #else
29*75f6d617Schristos # define STRXCASECMP_FUNCTION strcasecmp
30*75f6d617Schristos # define STRXCASECMP_DECLARE_N /* empty */
31*75f6d617Schristos # define LENGTH_LIMIT_EXPR(Expr) 0
32*75f6d617Schristos #endif
33*75f6d617Schristos 
34*75f6d617Schristos #include <sys/types.h>
35*75f6d617Schristos #include <ctype.h>
36*75f6d617Schristos 
37*75f6d617Schristos #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
38*75f6d617Schristos 
39*75f6d617Schristos /* Compare {{no more than N characters of }}strings S1 and S2,
40*75f6d617Schristos    ignoring case, returning less than, equal to or
41*75f6d617Schristos    greater than zero if S1 is lexicographically less
42*75f6d617Schristos    than, equal to or greater than S2.  */
43*75f6d617Schristos 
44*75f6d617Schristos int
STRXCASECMP_FUNCTION(const char * s1,const char * s2 STRXCASECMP_DECLARE_N)45*75f6d617Schristos STRXCASECMP_FUNCTION (const char *s1, const char *s2 STRXCASECMP_DECLARE_N)
46*75f6d617Schristos {
47*75f6d617Schristos   register const unsigned char *p1 = (const unsigned char *) s1;
48*75f6d617Schristos   register const unsigned char *p2 = (const unsigned char *) s2;
49*75f6d617Schristos   unsigned char c1, c2;
50*75f6d617Schristos 
51*75f6d617Schristos   if (p1 == p2 || LENGTH_LIMIT_EXPR (n == 0))
52*75f6d617Schristos     return 0;
53*75f6d617Schristos 
54*75f6d617Schristos   do
55*75f6d617Schristos     {
56*75f6d617Schristos       c1 = TOLOWER (*p1);
57*75f6d617Schristos       c2 = TOLOWER (*p2);
58*75f6d617Schristos 
59*75f6d617Schristos       if (LENGTH_LIMIT_EXPR (--n == 0) || c1 == '\0')
60*75f6d617Schristos 	break;
61*75f6d617Schristos 
62*75f6d617Schristos       ++p1;
63*75f6d617Schristos       ++p2;
64*75f6d617Schristos     }
65*75f6d617Schristos   while (c1 == c2);
66*75f6d617Schristos 
67*75f6d617Schristos   return c1 - c2;
68*75f6d617Schristos }
69