1 /*
2 Copyright (c) 2016 Corinna Vinschen <corinna@vinschen.de>
3  */
4 /*
5 FUNCTION
6 	<<strncasecmp_l>>---case-insensitive character string compare
7 
8 INDEX
9 	strncasecmp_l
10 
11 SYNOPSIS
12 	#include <strings.h>
13 	int strncasecmp_l(const char *<[a]>, const char * <[b]>,
14 			  size_t <[length]>, locale_t <[locale]>);
15 
16 DESCRIPTION
17 	<<strncasecmp_l>> compares up to <[length]> characters
18 	from the string at <[a]> to the string at <[b]> in a
19 	case-insensitive manner.
20 
21 	if <[locale]> is LC_GLOBAL_LOCALE or not a valid locale object, the
22 	behaviour is undefined.
23 
24 RETURNS
25 
26 	If <<*<[a]>>> sorts lexicographically after <<*<[b]>>> (after
27 	both are converted to lowercase), <<strncasecmp_l>> returns a
28 	number greater than zero.  If the two strings are equivalent,
29 	<<strncasecmp_l>> returns zero.  If <<*<[a]>>> sorts
30 	lexicographically before <<*<[b]>>>, <<strncasecmp_l>> returns a
31 	number less than zero.
32 
33 PORTABILITY
34 <<strncasecmp_l>> is POSIX-1.2008.
35 
36 <<strncasecmp_l>> requires no supporting OS subroutines. It uses
37 tolower_l() from elsewhere in this library.
38 
39 QUICKREF
40 	strncasecmp_l
41 */
42 
43 #define _DEFAULT_SOURCE
44 #include <strings.h>
45 #include <ctype.h>
46 
47 int
strncasecmp_l(const char * s1,const char * s2,size_t n,struct __locale_t * locale)48 strncasecmp_l (const char *s1, const char *s2, size_t n,
49 	       struct __locale_t *locale)
50 {
51   int d = 0;
52   for ( ; n != 0; n--)
53     {
54       const int c1 = tolower_l (*s1++, locale);
55       const int c2 = tolower_l (*s2++, locale);
56       if (((d = c1 - c2) != 0) || (c2 == '\0'))
57         break;
58     }
59   return d;
60 }
61