kpathsea from texlive (#6463)
[mplib] / src / texk / kpathsea / strcasecmp.c
1 /* strcasecmp.c - case-insensitive strcmp
2
3    Copyright 2008 Karl Berry.
4    Copyright 1991, 1992, 1995 Free Software Foundation, Inc.
5    This file was part of the GNU C Library.
6    Modified by Karl Berry for kpathsea.
7
8    This library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public
10    License as published by the Free Software Foundation; either
11    version 2.1 of the License, or (at your option) any later version.
12
13    This library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Lesser General Public License for more details.
17
18    You should have received a copy of the GNU Lesser General Public License
19    along with this library; if not, see <http://www.gnu.org/licenses/>.  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #if !defined (__STDC__) || !__STDC__
26 /* This is a separate conditional since some stdc systems
27    reject `defined (const)'.  */
28 #ifndef const
29 #define const
30 #endif
31 #endif
32
33 #include <ctype.h>
34
35 /* Compare S1 and S2, ignoring case, returning less than, equal to or
36    greater than zero if S1 is lexiographically less than,
37    equal to or greater than S2.  */
38 int
39 strcasecmp (s1, s2)
40     const char *s1;
41     const char *s2;
42 {
43   register const unsigned char *p1 = (const unsigned char *) s1;
44   register const unsigned char *p2 = (const unsigned char *) s2;
45   unsigned char c1, c2;
46
47   if (p1 == p2)
48     return 0;
49
50   do
51     {
52       c1 = tolower (*p1++);
53       c2 = tolower (*p2++);
54       if (c1 == '\0')
55         break;
56     }
57   while (c1 == c2);
58
59   return c1 - c2;
60 }
61
62 int
63 strncasecmp (s1, s2, n)
64     const char *s1;
65     const char *s2;
66     unsigned n;
67 {
68   register const unsigned char *p1 = (const unsigned char *) s1;
69   register const unsigned char *p2 = (const unsigned char *) s2;
70   unsigned char c1, c2;
71
72   if (p1 == p2 || n == 0)
73     return 0;
74
75   do
76     {
77       c1 = tolower (*p1++);
78       c2 = tolower (*p2++);
79       if (c1 == '\0' || c1 != c2)
80         return c1 - c2;
81     } while (--n > 0);
82
83   return c1 - c2;
84 }