xref: /dflybsd-src/lib/libc/string/wcsncasecmp.c (revision 7a2a491fdf72a1648e6b138a8eefd05ab81f65a0)
1*7a2a491fSJohn Marino /*-
2*7a2a491fSJohn Marino  * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
3*7a2a491fSJohn Marino  * All rights reserved.
4*7a2a491fSJohn Marino  *
5*7a2a491fSJohn Marino  * Redistribution and use in source and binary forms, with or without
6*7a2a491fSJohn Marino  * modification, are permitted provided that the following conditions
7*7a2a491fSJohn Marino  * are met:
8*7a2a491fSJohn Marino  * 1. Redistributions of source code must retain the above copyright
9*7a2a491fSJohn Marino  *    notice, this list of conditions and the following disclaimer.
10*7a2a491fSJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
11*7a2a491fSJohn Marino  *    notice, this list of conditions and the following disclaimer in the
12*7a2a491fSJohn Marino  *    documentation and/or other materials provided with the distribution.
13*7a2a491fSJohn Marino  *
14*7a2a491fSJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*7a2a491fSJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*7a2a491fSJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*7a2a491fSJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*7a2a491fSJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*7a2a491fSJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*7a2a491fSJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*7a2a491fSJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*7a2a491fSJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*7a2a491fSJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*7a2a491fSJohn Marino  * SUCH DAMAGE.
25*7a2a491fSJohn Marino  */
26*7a2a491fSJohn Marino 
27*7a2a491fSJohn Marino #include <wchar.h>
28*7a2a491fSJohn Marino #include <wctype.h>
29*7a2a491fSJohn Marino 
30*7a2a491fSJohn Marino int
31*7a2a491fSJohn Marino wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n)
32*7a2a491fSJohn Marino {
33*7a2a491fSJohn Marino 	wchar_t c1, c2;
34*7a2a491fSJohn Marino 
35*7a2a491fSJohn Marino 	if (n == 0)
36*7a2a491fSJohn Marino 		return (0);
37*7a2a491fSJohn Marino 	for (; *s1; s1++, s2++) {
38*7a2a491fSJohn Marino 		c1 = towlower(*s1);
39*7a2a491fSJohn Marino 		c2 = towlower(*s2);
40*7a2a491fSJohn Marino 		if (c1 != c2)
41*7a2a491fSJohn Marino 			return ((int)c1 - c2);
42*7a2a491fSJohn Marino 		if (--n == 0)
43*7a2a491fSJohn Marino 			return (0);
44*7a2a491fSJohn Marino 	}
45*7a2a491fSJohn Marino 	return (-*s2);
46*7a2a491fSJohn Marino }
47