*** empty log message ***
[gnulib.git] / lib / strncasecmp.c
1 #if HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include <sys/types.h>
6 #include <ctype.h>
7
8 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
9
10 /* Compare no more than N characters of S1 and S2,
11    ignoring case, returning less than, equal to or
12    greater than zero if S1 is lexicographically less
13    than, equal to or greater than S2.  */
14
15 int
16 strncasecmp (const char *s1, const char *s2, size_t n)
17 {
18   register const unsigned char *p1 = (const unsigned char *) s1;
19   register const unsigned char *p2 = (const unsigned char *) s2;
20   unsigned char c1, c2;
21
22   if (p1 == p2 || n == 0)
23     return 0;
24
25   do
26     {
27       c1 = TOLOWER (*p1++);
28       c2 = TOLOWER (*p2++);
29       if (c1 == '\0' || c1 != c2)
30         return c1 - c2;
31     } while (--n > 0);
32
33   return c1 - c2;
34 }