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