xref: /dflybsd-src/sys/libkern/strcasecmp.c (revision dc71b7ab81c4f5270d3668e1625d94a58895fa7a)
117f09ef2SRui Paulo /*
217f09ef2SRui Paulo  * Copyright (c) 1987, 1993
317f09ef2SRui Paulo  *	The Regents of the University of California.  All rights reserved.
417f09ef2SRui Paulo  *
517f09ef2SRui Paulo  * Redistribution and use in source and binary forms, with or without
617f09ef2SRui Paulo  * modification, are permitted provided that the following conditions
717f09ef2SRui Paulo  * are met:
817f09ef2SRui Paulo  * 1. Redistributions of source code must retain the above copyright
917f09ef2SRui Paulo  *    notice, this list of conditions and the following disclaimer.
1017f09ef2SRui Paulo  * 2. Redistributions in binary form must reproduce the above copyright
1117f09ef2SRui Paulo  *    notice, this list of conditions and the following disclaimer in the
1217f09ef2SRui Paulo  *    documentation and/or other materials provided with the distribution.
13*dc71b7abSJustin C. Sherrill  * 3. Neither the name of the University nor the names of its contributors
1417f09ef2SRui Paulo  *    may be used to endorse or promote products derived from this software
1517f09ef2SRui Paulo  *    without specific prior written permission.
1617f09ef2SRui Paulo  *
1717f09ef2SRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1817f09ef2SRui Paulo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1917f09ef2SRui Paulo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2017f09ef2SRui Paulo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2117f09ef2SRui Paulo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2217f09ef2SRui Paulo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2317f09ef2SRui Paulo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2417f09ef2SRui Paulo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2517f09ef2SRui Paulo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2617f09ef2SRui Paulo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2717f09ef2SRui Paulo  * SUCH DAMAGE.
2817f09ef2SRui Paulo  *
2917f09ef2SRui Paulo  * $FreeBSD: head/sys/libkern/strcasecmp.c 148865 2005-08-08 19:38:00Z pjd $
3017f09ef2SRui Paulo  */
3117f09ef2SRui Paulo 
3217f09ef2SRui Paulo #include <sys/param.h>
3317f09ef2SRui Paulo #include <sys/ctype.h>
3417f09ef2SRui Paulo #include <sys/libkern.h>
3517f09ef2SRui Paulo 
3617f09ef2SRui Paulo int
strcasecmp(const char * s1,const char * s2)3717f09ef2SRui Paulo strcasecmp(const char *s1, const char *s2)
3817f09ef2SRui Paulo {
3917f09ef2SRui Paulo 	const u_char *us1 = (const u_char *)s1, *us2 = (const u_char *)s2;
4017f09ef2SRui Paulo 
4117f09ef2SRui Paulo 	while (tolower(*us1) == tolower(*us2)) {
4217f09ef2SRui Paulo 		if (*us1++ == '\0')
4317f09ef2SRui Paulo 			return (0);
4417f09ef2SRui Paulo 		us2++;
4517f09ef2SRui Paulo 	}
4617f09ef2SRui Paulo 	return (tolower(*us1) - tolower(*us2));
4717f09ef2SRui Paulo }
4817f09ef2SRui Paulo 
4917f09ef2SRui Paulo int
strncasecmp(const char * s1,const char * s2,size_t n)5017f09ef2SRui Paulo strncasecmp(const char *s1, const char *s2, size_t n)
5117f09ef2SRui Paulo {
5217f09ef2SRui Paulo 
5317f09ef2SRui Paulo 	if (n != 0) {
5417f09ef2SRui Paulo 		const u_char *us1 = (const u_char *)s1;
5517f09ef2SRui Paulo 		const u_char *us2 = (const u_char *)s2;
5617f09ef2SRui Paulo 
5717f09ef2SRui Paulo 		do {
5817f09ef2SRui Paulo 			if (tolower(*us1) != tolower(*us2))
5917f09ef2SRui Paulo 				return (tolower(*us1) - tolower(*us2));
6017f09ef2SRui Paulo 			if (*us1++ == '\0')
6117f09ef2SRui Paulo 				break;
6217f09ef2SRui Paulo 			us2++;
6317f09ef2SRui Paulo 		} while (--n != 0);
6417f09ef2SRui Paulo 	}
6517f09ef2SRui Paulo 	return (0);
6617f09ef2SRui Paulo }
67