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