1 /*-
2  * Copyright (c) 2006, 2007, 2010
3  *	Thorsten Glaser <tg@mirbsd.org>
4  *
5  * Provided that these terms and disclaimer and all copyright notices
6  * are retained or reproduced in an accompanying document, permission
7  * is granted to deal in this work without restriction, including un-
8  * limited rights to use, publicly perform, distribute, sell, modify,
9  * merge, give away, or sublicence.
10  *
11  * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
12  * the utmost extent permitted by applicable law, neither express nor
13  * implied; without malicious intent or gross negligence. In no event
14  * may a licensor, author or contributor be held liable for indirect,
15  * direct, other damage, loss, or other issues arising in any way out
16  * of dealing in the work, even if advised of the possibility of such
17  * damage or existence of a defect, except proven that it results out
18  * of said person's immediate fault when using the work as intended.
19  */
20 
21 #include <sys/types.h>
22 #include <libckern.h>
23 
24 __RCSID("$MirOS: src/kern/c/strcasecmpfun.c,v 1.2 2010/01/11 01:27:07 tg Exp $");
25 
26 #undef c_size
27 #undef c_wide
28 #ifdef WCSNCASECMP
29 #define c_size
30 #define c_wide
31 #endif
32 #ifdef WCSCASECMP
33 #define c_wide
34 #endif
35 #ifdef STRNCASECMP
36 #define c_size
37 #endif
38 
39 #ifdef c_wide
40 #undef abs
41 #include <wctype.h>
42 #define CHAR		wchar_t
43 #define	LC(x)		L ## x
44 #define x_tolower	towlower
45 #define strcasecmp	wcscasecmp
46 #define strncasecmp	wcsncasecmp
47 #else
48 #define	CHAR		char
49 #define	LC(x)		x
50 #define x_tolower(c)	__extension__({			\
51 	int __CKERN_Tl = (c);				\
52 							\
53 	(__CKERN_Tl >= 'A') && (__CKERN_Tl <= 'Z') ?	\
54 	    __CKERN_Tl - 'A' + 'a' : __CKERN_Tl;	\
55 })
56 #endif
57 
58 __RCSID("$MirOS: src/kern/c/strcasecmpfun.c,v 1.2 2010/01/11 01:27:07 tg Exp $");
59 
60 #ifndef c_size
61 int
strcasecmp(const CHAR * s1,const CHAR * s2)62 strcasecmp(const CHAR *s1, const CHAR *s2)
63 {
64 	while (x_tolower(*s1) == x_tolower(*s2))
65 		if (*s1++ == LC('\0'))
66 			return (0);
67 		else
68 			s2++;
69 	return (x_tolower(*s1) - x_tolower(*s2));
70 }
71 #else
72 int
strncasecmp(const CHAR * s1,const CHAR * s2,size_t n)73 strncasecmp(const CHAR *s1, const CHAR *s2, size_t n)
74 {
75 	while (n--) {
76 		if (x_tolower(*s1) != x_tolower(*s2))
77 			return (x_tolower(*s1) - x_tolower(*s2));
78 		if (*s1++ == LC('\0'))
79 			break;
80 		s2++;
81 	}
82 	return (0);
83 }
84 #endif
85