xref: /netbsd-src/common/lib/libc/string/strncasecmp.c (revision 4496cdcb87311f0e271d926254da1a484c4878f4)
1*4496cdcbSchristos /*	$NetBSD: strncasecmp.c,v 1.3 2018/08/16 12:03:10 christos Exp $	*/
237c9f0a6Schristos 
337c9f0a6Schristos /*
437c9f0a6Schristos  * Copyright (c) 1987, 1993
537c9f0a6Schristos  *	The Regents of the University of California.  All rights reserved.
637c9f0a6Schristos  *
737c9f0a6Schristos  * Redistribution and use in source and binary forms, with or without
837c9f0a6Schristos  * modification, are permitted provided that the following conditions
937c9f0a6Schristos  * are met:
1037c9f0a6Schristos  * 1. Redistributions of source code must retain the above copyright
1137c9f0a6Schristos  *    notice, this list of conditions and the following disclaimer.
1237c9f0a6Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1337c9f0a6Schristos  *    notice, this list of conditions and the following disclaimer in the
1437c9f0a6Schristos  *    documentation and/or other materials provided with the distribution.
1537c9f0a6Schristos  * 3. Neither the name of the University nor the names of its contributors
1637c9f0a6Schristos  *    may be used to endorse or promote products derived from this software
1737c9f0a6Schristos  *    without specific prior written permission.
1837c9f0a6Schristos  *
1937c9f0a6Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2037c9f0a6Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2137c9f0a6Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2237c9f0a6Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2337c9f0a6Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2437c9f0a6Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2537c9f0a6Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2637c9f0a6Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2737c9f0a6Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2837c9f0a6Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2937c9f0a6Schristos  * SUCH DAMAGE.
3037c9f0a6Schristos  */
3137c9f0a6Schristos 
32*4496cdcbSchristos #if HAVE_NBTOOL_CONFIG_H
33*4496cdcbSchristos #include "nbtool_config.h"
34*4496cdcbSchristos #endif
35*4496cdcbSchristos 
3637c9f0a6Schristos #include <sys/cdefs.h>
3737c9f0a6Schristos #if defined(LIBC_SCCS) && !defined(lint)
3837c9f0a6Schristos #if 0
3937c9f0a6Schristos static char sccsid[] = "@(#)strcasecmp.c	8.1 (Berkeley) 6/4/93";
4037c9f0a6Schristos #else
41*4496cdcbSchristos __RCSID("$NetBSD: strncasecmp.c,v 1.3 2018/08/16 12:03:10 christos Exp $");
4237c9f0a6Schristos #endif
4337c9f0a6Schristos #endif /* LIBC_SCCS and not lint */
4437c9f0a6Schristos 
4537c9f0a6Schristos #if !defined(_KERNEL) && !defined(_STANDALONE)
4637c9f0a6Schristos #include "namespace.h"
4737c9f0a6Schristos #include <assert.h>
4837c9f0a6Schristos #include <ctype.h>
4937c9f0a6Schristos #include <string.h>
5037c9f0a6Schristos #ifdef __weak_alias
__weak_alias(strcasecmp,_strcasecmp)5137c9f0a6Schristos __weak_alias(strcasecmp,_strcasecmp)
5237c9f0a6Schristos __weak_alias(strncasecmp,_strncasecmp)
5337c9f0a6Schristos #endif
5437c9f0a6Schristos #else
5537c9f0a6Schristos #include <lib/libkern/libkern.h>
5637c9f0a6Schristos #include <machine/limits.h>
5737c9f0a6Schristos #endif
5837c9f0a6Schristos 
5937c9f0a6Schristos int
60a8565cf9Schristos strncasecmp(const char *s1, const char *s2, size_t n)
6137c9f0a6Schristos {
6237c9f0a6Schristos 
6337c9f0a6Schristos 	_DIAGASSERT(s1 != NULL);
6437c9f0a6Schristos 	_DIAGASSERT(s2 != NULL);
6537c9f0a6Schristos 
6637c9f0a6Schristos 	if (n != 0) {
6737c9f0a6Schristos 		const unsigned char *us1 = (const unsigned char *)s1,
6837c9f0a6Schristos 				*us2 = (const unsigned char *)s2;
6937c9f0a6Schristos 
7037c9f0a6Schristos 		do {
7137c9f0a6Schristos 			if (tolower(*us1) != tolower(*us2++))
7237c9f0a6Schristos 				return (tolower(*us1) - tolower(*--us2));
7337c9f0a6Schristos 			if (*us1++ == '\0')
7437c9f0a6Schristos 				break;
7537c9f0a6Schristos 		} while (--n != 0);
7637c9f0a6Schristos 	}
7737c9f0a6Schristos 	return (0);
7837c9f0a6Schristos }
79