xref: /openbsd-src/sys/lib/libkern/strncasecmp.c (revision b5be37d2a6fbef83b90ca99497343fa8dbb0f3fc)
1*b5be37d2Sderaadt /*	$OpenBSD: strncasecmp.c,v 1.6 2014/06/10 04:16:57 deraadt Exp $	*/
29f13aad2Sniklas 
39f13aad2Sniklas /*
49f13aad2Sniklas  * Copyright (c) 1994 Christian E. Hopps
59f13aad2Sniklas  * All rights reserved.
69f13aad2Sniklas  *
79f13aad2Sniklas  * Redistribution and use in source and binary forms, with or without
89f13aad2Sniklas  * modification, are permitted provided that the following conditions
99f13aad2Sniklas  * are met:
109f13aad2Sniklas  * 1. Redistributions of source code must retain the above copyright
119f13aad2Sniklas  *    notice, this list of conditions and the following disclaimer.
129f13aad2Sniklas  * 2. Redistributions in binary form must reproduce the above copyright
139f13aad2Sniklas  *    notice, this list of conditions and the following disclaimer in the
149f13aad2Sniklas  *    documentation and/or other materials provided with the distribution.
159f13aad2Sniklas  * 3. All advertising materials mentioning features or use of this software
169f13aad2Sniklas  *    must display the following acknowledgement:
179f13aad2Sniklas  *      This product includes software developed by Christian E. Hopps.
189f13aad2Sniklas  * 4. The name of the author may not be used to endorse or promote products
199f13aad2Sniklas  *    derived from this software without specific prior written permission
209f13aad2Sniklas  *
219f13aad2Sniklas  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
229f13aad2Sniklas  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
239f13aad2Sniklas  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
249f13aad2Sniklas  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
259f13aad2Sniklas  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
269f13aad2Sniklas  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
279f13aad2Sniklas  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
289f13aad2Sniklas  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
299f13aad2Sniklas  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
309f13aad2Sniklas  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
319f13aad2Sniklas  */
32*b5be37d2Sderaadt 
33cf4fa783Sderaadt #include <lib/libkern/libkern.h>
349f13aad2Sniklas 
359f13aad2Sniklas int
strncasecmp(const char * s1,const char * s2,size_t n)36a26aa419Sderaadt strncasecmp(const char *s1, const char *s2, size_t n)
379f13aad2Sniklas {
389f13aad2Sniklas 	if (n == 0)
399f13aad2Sniklas 		return 0;
409f13aad2Sniklas 
419f13aad2Sniklas 	do {
429f13aad2Sniklas 		unsigned char c1 = (unsigned char) *s1++;
439f13aad2Sniklas 		unsigned char c2 = (unsigned char) *s2++;
449f13aad2Sniklas 
459f13aad2Sniklas 		if (c1 != c2) {
469f13aad2Sniklas 			if (c1 >= 'A' && c1 <= 'Z' &&
479f13aad2Sniklas 			    c2 >= 'a' && c2 <= 'z')
489f13aad2Sniklas 				c1 += 'a' - 'A';
499f13aad2Sniklas 			else if (c1 >= 'a' && c1 <= 'z' &&
509f13aad2Sniklas 				 c2 >= 'A' && c2 <= 'Z')
519f13aad2Sniklas 				c2 += 'a' - 'A';
529f13aad2Sniklas 			if (c1 != c2)
539f13aad2Sniklas 				return c1 - c2;
549f13aad2Sniklas 		}
559f13aad2Sniklas 		if (c1 == 0)
569f13aad2Sniklas 			break;
579f13aad2Sniklas 	} while (--n != 0);
589f13aad2Sniklas 
599f13aad2Sniklas 	return 0;
609f13aad2Sniklas }
61