strcasecmp.c revision 26180
1234285Sdim/*
2234285Sdim * Copyright (c) 1987 Regents of the University of California.
3234285Sdim * All rights reserved.
4234285Sdim *
5234285Sdim * Redistribution and use in source and binary forms are permitted
6234285Sdim * provided that this notice is preserved and that due credit is given
7234285Sdim * to the University of California at Berkeley. The name of the University
8234285Sdim * may not be used to endorse or promote products derived from this
9234285Sdim * software without specific written prior permission. This software
10234285Sdim * is provided ``as is'' without express or implied warranty.
11234285Sdim */
12234285Sdim
13234285Sdim#if defined(LIBC_SCCS) && !defined(lint)
14234285Sdimstatic const char sccsid[] = "@(#)strcasecmp.c	5.5 (Berkeley) 11/24/87";
15234285Sdim#endif /* LIBC_SCCS and not lint */
16234285Sdim
17243830Sdim#include <sys/types.h>
18263508Sdim
19234285Sdim#include "interface.h"
20249423Sdim
21234285Sdim/*
22249423Sdim * This array is designed for mapping upper and lower case letter
23249423Sdim * together for a case independent comparison.  The mappings are
24234285Sdim * based upon ascii character sequences.
25234285Sdim */
26234285Sdimstatic u_char charmap[] = {
27234285Sdim	'\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
28234285Sdim	'\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
29251662Sdim	'\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
30251662Sdim	'\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
31234285Sdim	'\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
32251662Sdim	'\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
33251662Sdim	'\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
34251662Sdim	'\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
35243830Sdim	'\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
36249423Sdim	'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
37251662Sdim	'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
38251662Sdim	'\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
39249423Sdim	'\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
40251662Sdim	'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
41234285Sdim	'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
42234285Sdim	'\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
43234285Sdim	'\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
44234285Sdim	'\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
45234285Sdim	'\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
46234285Sdim	'\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
47234285Sdim	'\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
48234285Sdim	'\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
49234285Sdim	'\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
50234285Sdim	'\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
51234285Sdim	'\300', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
52234285Sdim	'\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
53234285Sdim	'\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
54243830Sdim	'\370', '\371', '\372', '\333', '\334', '\335', '\336', '\337',
55243830Sdim	'\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
56243830Sdim	'\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
57234285Sdim	'\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
58243830Sdim	'\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
59243830Sdim};
60243830Sdim
61243830Sdimint
62234285Sdimstrcasecmp(s1, s2)
63234285Sdim	const char *s1, *s2;
64234285Sdim{
65234285Sdim	register u_char	*cm = charmap,
66234285Sdim			*us1 = (u_char *)s1,
67234285Sdim			*us2 = (u_char *)s2;
68234285Sdim
69234285Sdim	while (cm[*us1] == cm[*us2++])
70234285Sdim		if (*us1++ == '\0')
71234285Sdim			return(0);
72234285Sdim	return(cm[*us1] - cm[*--us2]);
73234285Sdim}
74243830Sdim
75239462Sdimint
76239462Sdimstrncasecmp(s1, s2, n)
77234285Sdim	const char *s1, *s2;
78234285Sdim	register int n;
79234285Sdim{
80249423Sdim	register u_char	*cm = charmap,
81249423Sdim			*us1 = (u_char *)s1,
82263508Sdim			*us2 = (u_char *)s2;
83234285Sdim
84234285Sdim	while (--n >= 0 && cm[*us1] == cm[*us2++])
85234285Sdim		if (*us1++ == '\0')
86234285Sdim			return(0);
87234285Sdim	return(n < 0 ? 0 : cm[*us1] - cm[*--us2]);
88249423Sdim}
89249423Sdim