xref: /csrg-svn/lib/libc/string/strcasecmp.c (revision 31745)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #if defined(LIBC_SCCS) && !defined(lint)
8 static char sccsid[] = "@(#)strcasecmp.c	5.1 (Berkeley) 07/02/87";
9 #endif LIBC_SCCS and not lint
10 
11 #include <ctype.h>
12 
13 /*
14  * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
15  * case insensitive
16  */
17 strcasecmp(s1, s2)
18 	register char	*s1, *s2;
19 {
20 	register char	c1, c2;
21 
22 	for (;; ++s1, ++s2) {
23 		c2 = isupper(*s2) ? tolower(*s2) : *s2;
24 		c1 = isupper(*s1) ? tolower(*s1) : *s1;
25 		if (c1 != c2)
26 			return(c1 - c2);
27 		if (!c1)
28 			return(0);
29 	}
30 }
31 
32 /*
33  * Compare strings (at most n bytes):  s1>s2: >0  s1==s2: 0  s1<s2: <0
34  * case insensitive
35  */
36 strcasencmp(s1, s2, n)
37 	register char	*s1, *s2;
38 	register int	n;
39 {
40 	register char	c1, c2;
41 
42 	for (; n; ++s1, ++s2, --n) {
43 		c2 = isupper(*s2) ? tolower(*s2) : *s2;
44 		c1 = isupper(*s1) ? tolower(*s1) : *s1;
45 		if (c1 != c2)
46 			return(c1 - c2);
47 		if (!c1)
48 			return(0);
49 	}
50 	return(0);
51 }
52