1e15c021cad127399dea379ca85c57f744d8ea1a
[platform/upstream/bash.git] / lib / sh / strcasecmp.c
1 /* strcasecmp.c - functions for case-insensitive string comparison. */
2
3 /* Copyright (C) 1995 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6
7    Bash is free software; you can redistribute it and/or modify it under
8    the terms of the GNU General Public License as published by the Free
9    Software Foundation; either version 2, or (at your option) any later
10    version.
11
12    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13    WARRANTY; without even the implied warranty of MERCHANTABILITY or
14    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15    for more details.
16
17    You should have received a copy of the GNU General Public License along
18    with Bash; see the file COPYING.  If not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA */
20    
21 #include <config.h>
22
23 #if !defined (HAVE_STRCASECMP)
24
25 #include <stdc.h>
26 #include <bashansi.h>
27 #include <ctype.h>
28
29 #if !defined (to_lower)
30 #  define to_lower(c) (islower(c) ? (c) : tolower(c))
31 #endif /* to_lower */
32
33 /* Compare at most COUNT characters from string1 to string2.  Case
34    doesn't matter. */
35 int
36 strncasecmp (string1, string2, count)
37      const char *string1;
38      const char *string2;
39      int count;
40 {
41   register const char *s1;
42   register const char *s2;
43   register int r;
44
45   if (count <= 0 || (string1 == string2))
46     return 0;
47
48   s1 = string1;
49   s2 = string2;
50   do
51     {
52       if ((r = to_lower (*s1) - to_lower (*s2)) != 0)
53         return r;
54       if (*s1++ == '\0')
55         break;
56       s2++;
57     }
58   while (--count != 0);
59
60   return (0);
61 }
62
63 /* strcmp (), but caseless. */
64 int
65 strcasecmp (string1, string2)
66      const char *string1;
67      const char *string2;
68 {
69   register const char *s1;
70   register const char *s2;
71   register int r;
72
73   s1 = string1;
74   s2 = string2;
75
76   if (s1 == s2)
77     return (0);
78
79   while ((r = to_lower (*s1) - to_lower (*s2)) == 0)
80     {
81       if (*s1++ == '\0')
82         return 0;
83       s2++;
84     }
85
86   return (r);
87 }
88 #endif /* !HAVE_STRCASECMP */