xref: /openbsd-src/lib/libc/string/strcasecmp.c (revision 9b9d2a55a62c8e82206c25f94fcc7f4e2765250e)
1*9b9d2a55Sguenther /*	$OpenBSD: strcasecmp.c,v 1.7 2015/08/31 02:53:57 guenther Exp $	*/
2a2260c1dSmillert 
3df930be7Sderaadt /*
4a2260c1dSmillert  * Copyright (c) 1987, 1993
5a2260c1dSmillert  *	The Regents of the University of California.  All rights reserved.
6df930be7Sderaadt  *
7df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
8df930be7Sderaadt  * modification, are permitted provided that the following conditions
9df930be7Sderaadt  * are met:
10df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
11df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
12df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
13df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
14df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
156580fee3Smillert  * 3. Neither the name of the University nor the names of its contributors
16df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
17df930be7Sderaadt  *    without specific prior written permission.
18df930be7Sderaadt  *
19df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df930be7Sderaadt  * SUCH DAMAGE.
30df930be7Sderaadt  */
31df930be7Sderaadt 
32df930be7Sderaadt #include <string.h>
33df930be7Sderaadt 
34df930be7Sderaadt typedef unsigned char u_char;
35df930be7Sderaadt 
36df930be7Sderaadt /*
37df930be7Sderaadt  * This array is designed for mapping upper and lower case letter
38df930be7Sderaadt  * together for a case independent comparison.  The mappings are
39df930be7Sderaadt  * based upon ascii character sequences.
40df930be7Sderaadt  */
41df930be7Sderaadt static const u_char charmap[] = {
42df930be7Sderaadt 	'\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
43df930be7Sderaadt 	'\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
44df930be7Sderaadt 	'\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
45df930be7Sderaadt 	'\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
46df930be7Sderaadt 	'\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
47df930be7Sderaadt 	'\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
48df930be7Sderaadt 	'\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
49df930be7Sderaadt 	'\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
50df930be7Sderaadt 	'\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
51df930be7Sderaadt 	'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
52df930be7Sderaadt 	'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
53df930be7Sderaadt 	'\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
54df930be7Sderaadt 	'\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
55df930be7Sderaadt 	'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
56df930be7Sderaadt 	'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
57df930be7Sderaadt 	'\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
58df930be7Sderaadt 	'\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
59df930be7Sderaadt 	'\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
60df930be7Sderaadt 	'\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
61df930be7Sderaadt 	'\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
62df930be7Sderaadt 	'\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
63df930be7Sderaadt 	'\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
64df930be7Sderaadt 	'\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
65df930be7Sderaadt 	'\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
66df930be7Sderaadt 	'\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
67df930be7Sderaadt 	'\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
68df930be7Sderaadt 	'\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
69df930be7Sderaadt 	'\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
70df930be7Sderaadt 	'\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
71df930be7Sderaadt 	'\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
72df930be7Sderaadt 	'\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
73df930be7Sderaadt 	'\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
74df930be7Sderaadt };
75df930be7Sderaadt 
76df930be7Sderaadt int
strcasecmp(const char * s1,const char * s2)7786b54bb2Sderaadt strcasecmp(const char *s1, const char *s2)
78df930be7Sderaadt {
7986b54bb2Sderaadt 	const u_char *cm = charmap;
8086b54bb2Sderaadt 	const u_char *us1 = (const u_char *)s1;
8186b54bb2Sderaadt 	const u_char *us2 = (const u_char *)s2;
82df930be7Sderaadt 
83df930be7Sderaadt 	while (cm[*us1] == cm[*us2++])
84df930be7Sderaadt 		if (*us1++ == '\0')
85df930be7Sderaadt 			return (0);
86df930be7Sderaadt 	return (cm[*us1] - cm[*--us2]);
87df930be7Sderaadt }
88*9b9d2a55Sguenther DEF_WEAK(strcasecmp);
89df930be7Sderaadt 
90df930be7Sderaadt int
strncasecmp(const char * s1,const char * s2,size_t n)9186b54bb2Sderaadt strncasecmp(const char *s1, const char *s2, size_t n)
92df930be7Sderaadt {
93df930be7Sderaadt 	if (n != 0) {
9486b54bb2Sderaadt 		const u_char *cm = charmap;
9586b54bb2Sderaadt 		const u_char *us1 = (const u_char *)s1;
9686b54bb2Sderaadt 		const u_char *us2 = (const u_char *)s2;
97df930be7Sderaadt 
98df930be7Sderaadt 		do {
99df930be7Sderaadt 			if (cm[*us1] != cm[*us2++])
100df930be7Sderaadt 				return (cm[*us1] - cm[*--us2]);
101df930be7Sderaadt 			if (*us1++ == '\0')
102df930be7Sderaadt 				break;
103df930be7Sderaadt 		} while (--n != 0);
104df930be7Sderaadt 	}
105df930be7Sderaadt 	return (0);
106df930be7Sderaadt }
107*9b9d2a55Sguenther DEF_WEAK(strncasecmp);
108