xref: /freebsd/sys/libkern/strcasecmp.c (revision df57947f083046d50552e99b91074927d2458708)
1c0587701SJoel Dahl /*-
2*df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3*df57947fSPedro F. Giffuni  *
4c812ca43SPawel Jakub Dawidek  * Copyright (c) 1987, 1993
5c812ca43SPawel Jakub Dawidek  *	The Regents of the University of California.  All rights reserved.
6c812ca43SPawel Jakub Dawidek  *
7c812ca43SPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without
8c812ca43SPawel Jakub Dawidek  * modification, are permitted provided that the following conditions
9c812ca43SPawel Jakub Dawidek  * are met:
10c812ca43SPawel Jakub Dawidek  * 1. Redistributions of source code must retain the above copyright
11c812ca43SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer.
12c812ca43SPawel Jakub Dawidek  * 2. Redistributions in binary form must reproduce the above copyright
13c812ca43SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer in the
14c812ca43SPawel Jakub Dawidek  *    documentation and/or other materials provided with the distribution.
15c812ca43SPawel Jakub Dawidek  * 3. All advertising materials mentioning features or use of this software
16c812ca43SPawel Jakub Dawidek  *    must display the following acknowledgement:
17c812ca43SPawel Jakub Dawidek  *	This product includes software developed by the University of
18c812ca43SPawel Jakub Dawidek  *	California, Berkeley and its contributors.
19c812ca43SPawel Jakub Dawidek  * 4. Neither the name of the University nor the names of its contributors
20c812ca43SPawel Jakub Dawidek  *    may be used to endorse or promote products derived from this software
21c812ca43SPawel Jakub Dawidek  *    without specific prior written permission.
22c812ca43SPawel Jakub Dawidek  *
23c812ca43SPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24c812ca43SPawel Jakub Dawidek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25c812ca43SPawel Jakub Dawidek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26c812ca43SPawel Jakub Dawidek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27c812ca43SPawel Jakub Dawidek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28c812ca43SPawel Jakub Dawidek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29c812ca43SPawel Jakub Dawidek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30c812ca43SPawel Jakub Dawidek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31c812ca43SPawel Jakub Dawidek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32c812ca43SPawel Jakub Dawidek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33c812ca43SPawel Jakub Dawidek  * SUCH DAMAGE.
34c812ca43SPawel Jakub Dawidek  */
35c812ca43SPawel Jakub Dawidek 
36c812ca43SPawel Jakub Dawidek #include <sys/cdefs.h>
37c812ca43SPawel Jakub Dawidek __FBSDID("$FreeBSD$");
38c812ca43SPawel Jakub Dawidek 
39c812ca43SPawel Jakub Dawidek #include <sys/param.h>
40c812ca43SPawel Jakub Dawidek #include <sys/ctype.h>
41c812ca43SPawel Jakub Dawidek #include <sys/libkern.h>
42c812ca43SPawel Jakub Dawidek 
43c812ca43SPawel Jakub Dawidek int
44c812ca43SPawel Jakub Dawidek strcasecmp(const char *s1, const char *s2)
45c812ca43SPawel Jakub Dawidek {
46c812ca43SPawel Jakub Dawidek 	const u_char *us1 = (const u_char *)s1, *us2 = (const u_char *)s2;
47c812ca43SPawel Jakub Dawidek 
4857169160SPawel Jakub Dawidek 	while (tolower(*us1) == tolower(*us2)) {
49c812ca43SPawel Jakub Dawidek 		if (*us1++ == '\0')
50c812ca43SPawel Jakub Dawidek 			return (0);
5157169160SPawel Jakub Dawidek 		us2++;
52c812ca43SPawel Jakub Dawidek 	}
5357169160SPawel Jakub Dawidek 	return (tolower(*us1) - tolower(*us2));
54c812ca43SPawel Jakub Dawidek }
55c812ca43SPawel Jakub Dawidek 
56c812ca43SPawel Jakub Dawidek int
57c812ca43SPawel Jakub Dawidek strncasecmp(const char *s1, const char *s2, size_t n)
58c812ca43SPawel Jakub Dawidek {
59c812ca43SPawel Jakub Dawidek 
60c812ca43SPawel Jakub Dawidek 	if (n != 0) {
61c812ca43SPawel Jakub Dawidek 		const u_char *us1 = (const u_char *)s1;
62c812ca43SPawel Jakub Dawidek 		const u_char *us2 = (const u_char *)s2;
63c812ca43SPawel Jakub Dawidek 
64c812ca43SPawel Jakub Dawidek 		do {
6557169160SPawel Jakub Dawidek 			if (tolower(*us1) != tolower(*us2))
6657169160SPawel Jakub Dawidek 				return (tolower(*us1) - tolower(*us2));
67c812ca43SPawel Jakub Dawidek 			if (*us1++ == '\0')
68c812ca43SPawel Jakub Dawidek 				break;
6957169160SPawel Jakub Dawidek 			us2++;
70c812ca43SPawel Jakub Dawidek 		} while (--n != 0);
71c812ca43SPawel Jakub Dawidek 	}
72c812ca43SPawel Jakub Dawidek 	return (0);
73c812ca43SPawel Jakub Dawidek }
74