xref: /netbsd-src/lib/libc/string/wcscasecmp.c (revision af56d1fe9956bd7c616e18c1b7f025f464618471)
1 /*	$NetBSD: wcscasecmp.c,v 1.3 2013/04/18 23:24:27 joerg Exp $	*/
2 
3 /*
4  * Copyright (C) 2006 Aleksey Cheusov
5  *
6  * This material is provided "as is", with absolutely no warranty expressed
7  * or implied. Any use is at your own risk.
8  *
9  * Permission to use or copy this software for any purpose is hereby granted
10  * without fee. Permission to modify the code and to distribute modified
11  * code is also granted without any restrictions.
12  */
13 
14 #include <sys/cdefs.h>
15 #if defined(LIBC_SCCS) && !defined(lint)
16 __RCSID("$NetBSD: wcscasecmp.c,v 1.3 2013/04/18 23:24:27 joerg Exp $");
17 #endif /* LIBC_SCCS and not lint */
18 
19 #include "namespace.h"
20 #include <assert.h>
21 #include <wchar.h>
22 #include <wctype.h>
23 #include <locale.h>
24 #include "setlocale_local.h"
25 
26 __weak_alias(wcscasecmp,_wcscasecmp)
27 __weak_alias(wcscasecmp_l,_wcscasecmp_l)
28 
29 int
30 wcscasecmp_l(const wchar_t *s1, const wchar_t *s2, locale_t loc)
31 {
32 	int lc1  = 0;
33 	int lc2  = 0;
34 	int diff = 0;
35 
36 	if (loc == NULL)
37 		loc = _C_locale;
38 
39 	_DIAGASSERT(s1);
40 	_DIAGASSERT(s2);
41 
42 	for (;;) {
43 		lc1 = towlower_l(*s1, loc);
44 		lc2 = towlower_l(*s2, loc);
45 
46 		diff = lc1 - lc2;
47 		if (diff)
48 			return diff;
49 
50 		if (!lc1)
51 			return 0;
52 
53 		++s1;
54 		++s2;
55 	}
56 }
57 
58 int
59 wcscasecmp(const wchar_t *s1, const wchar_t *s2)
60 {
61 	return wcscasecmp_l(s1, s2, *_current_locale());
62 }
63