bbb26cbaec9c41c6d45b6ae02673d903149c6e52
[mmh] / sbr / strcasecmp.c
1
2 /*
3  * strcasecmp.c -- compare strings, ignoring case
4  *
5  * $Id$
6  */
7
8 #include <h/mh.h>
9
10 /*
11  * Our version of strcasecmp has to deal with NULL strings.
12  * Once that is fixed in the rest of the code, we can use the
13  * native version, instead of this one.
14  */
15
16 int
17 strcasecmp (const char *s1, const char *s2) 
18 {
19     const unsigned char *us1, *us2;
20
21     us1 = (const unsigned char *) s1,
22     us2 = (const unsigned char *) s2;
23
24     if (!us1)
25         us1 = "";
26     if (!us2)
27         us2 = "";
28  
29     while (tolower(*us1) == tolower(*us2++)) 
30         if (*us1++ == '\0')
31             return (0);
32     return (tolower(*us1) - tolower(*--us2));
33 }
34  
35
36 int
37 strncasecmp (const char *s1, const char *s2, size_t n)
38 {
39     const unsigned char *us1, *us2;
40
41     if (n != 0) { 
42         us1 = (const unsigned char *) s1,
43         us2 = (const unsigned char *) s2;
44
45         do {  
46             if (tolower(*us1) != tolower(*us2++))
47                 return (tolower(*us1) - tolower(*--us2));
48             if (*us1++ == '\0')
49                 break;  
50         } while (--n != 0);
51     } 
52     return (0);
53 }