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