11573Srgrimes/*
21573Srgrimes * Copyright (c) 1987, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
5235785Stheraven * Copyright (c) 2011 The FreeBSD Foundation
6235785Stheraven * All rights reserved.
7235785Stheraven * Portions of this software were developed by David Chisnall
8235785Stheraven * under sponsorship from the FreeBSD Foundation.
9235785Stheraven *
101573Srgrimes * Redistribution and use in source and binary forms, with or without
111573Srgrimes * modification, are permitted provided that the following conditions
121573Srgrimes * are met:
131573Srgrimes * 1. Redistributions of source code must retain the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer.
151573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161573Srgrimes *    notice, this list of conditions and the following disclaimer in the
171573Srgrimes *    documentation and/or other materials provided with the distribution.
181573Srgrimes * 4. Neither the name of the University nor the names of its contributors
191573Srgrimes *    may be used to endorse or promote products derived from this software
201573Srgrimes *    without specific prior written permission.
211573Srgrimes *
221573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321573Srgrimes * SUCH DAMAGE.
331573Srgrimes */
341573Srgrimes
3577117Sobrien#if defined(LIBC_SCCS) && !defined(lint)
3677117Sobrienstatic char sccsid[] = "@(#)strcasecmp.c	8.1 (Berkeley) 6/4/93";
3777117Sobrien#endif /* LIBC_SCCS and not lint */
3886170Sobrien#include <sys/cdefs.h>
3986170Sobrien__FBSDID("$FreeBSD$");
4077117Sobrien
41102624Srobert#include <strings.h>
4216250Sache#include <ctype.h>
43235785Stheraven#include "xlocale_private.h"
441573Srgrimes
451573Srgrimesint
46235785Stheravenstrcasecmp_l(const char *s1, const char *s2, locale_t locale)
471573Srgrimes{
4892889Sobrien	const u_char
491573Srgrimes			*us1 = (const u_char *)s1,
501573Srgrimes			*us2 = (const u_char *)s2;
51235785Stheraven	FIX_LOCALE(locale);
521573Srgrimes
53235785Stheraven	while (tolower_l(*us1, locale) == tolower_l(*us2++, locale))
541573Srgrimes		if (*us1++ == '\0')
551573Srgrimes			return (0);
56235785Stheraven	return (tolower_l(*us1, locale) - tolower_l(*--us2, locale));
571573Srgrimes}
58235785Stheravenint
59235785Stheravenstrcasecmp(const char *s1, const char *s2)
60235785Stheraven{
61235785Stheraven	return strcasecmp_l(s1, s2, __get_locale());
62235785Stheraven}
631573Srgrimes
641573Srgrimesint
65235785Stheravenstrncasecmp_l(const char *s1, const char *s2, size_t n, locale_t locale)
661573Srgrimes{
67235785Stheraven	FIX_LOCALE(locale);
681573Srgrimes	if (n != 0) {
6992889Sobrien		const u_char
701573Srgrimes				*us1 = (const u_char *)s1,
711573Srgrimes				*us2 = (const u_char *)s2;
721573Srgrimes
731573Srgrimes		do {
74235785Stheraven			if (tolower_l(*us1, locale) != tolower_l(*us2++, locale))
75235785Stheraven				return (tolower_l(*us1, locale) - tolower_l(*--us2, locale));
761573Srgrimes			if (*us1++ == '\0')
771573Srgrimes				break;
781573Srgrimes		} while (--n != 0);
791573Srgrimes	}
801573Srgrimes	return (0);
811573Srgrimes}
82235785Stheraven
83235785Stheravenint
84235785Stheravenstrncasecmp(const char *s1, const char *s2, size_t n)
85235785Stheraven{
86235785Stheraven	return strncasecmp_l(s1, s2, n, __get_locale());
87235785Stheraven}
88