1 /*
2 FUNCTION
3 	<<strncasecmp>>---case-insensitive character string compare
4 
5 INDEX
6 	strncasecmp
7 
8 SYNOPSIS
9 	#include <strings.h>
10 	int strncasecmp(const char *<[a]>, const char * <[b]>, size_t <[length]>);
11 
12 DESCRIPTION
13 	<<strncasecmp>> compares up to <[length]> characters
14 	from the string at <[a]> to the string at <[b]> in a
15 	case-insensitive manner.
16 
17 RETURNS
18 
19 	If <<*<[a]>>> sorts lexicographically after <<*<[b]>>> (after
20 	both are converted to lowercase), <<strncasecmp>> returns a
21 	number greater than zero.  If the two strings are equivalent,
22 	<<strncasecmp>> returns zero.  If <<*<[a]>>> sorts
23 	lexicographically before <<*<[b]>>>, <<strncasecmp>> returns a
24 	number less than zero.
25 
26 PORTABILITY
27 <<strncasecmp>> is in the Berkeley Software Distribution.
28 
29 <<strncasecmp>> requires no supporting OS subroutines. It uses
30 tolower() from elsewhere in this library.
31 
32 QUICKREF
33 	strncasecmp
34 */
35 
36 #include <strings.h>
37 #include <ctype.h>
38 
39 int
strncasecmp(const char * s1,const char * s2,size_t n)40 strncasecmp (const char *s1,
41 	const char *s2,
42 	size_t n)
43 {
44   int d = 0;
45   for ( ; n != 0; n--)
46     {
47       const int c1 = tolower(*s1++);
48       const int c2 = tolower(*s2++);
49       if (((d = c1 - c2) != 0) || (c2 == '\0'))
50         break;
51     }
52   return d;
53 }
54