1 #include <stdlib.h>
2 #include <ctype.h>
3 #include <string.h>
4 
5 #include "config.h"
6 #include "utils.h"
7 
8 #ifdef USE_OUR_CASECMP
9 
10 /* Code contributed by Nelson Beebe */
11 /**********************************************************************/
12 /****************************** strcasecmp ****************************/
13 /**********************************************************************/
14 
15 
16 /***********************************************************************
17  Compare strings (ignoring case), and return:
18 	s1>s2:	>0
19 	s1==s2:  0
20 	s1<s2:	<0
21 ***********************************************************************/
22 
23 /* toupper() is supposed to work for all letters, but PCC-20 does it
24 incorrectly if the argument is not already lowercase; this definition
25 fixes that. */
26 
27 #define TOUPPER(c) (islower((int)(c)) ? toupper((int)(c)) : (c))
28 
29 int
30 #if HAVE_STDC
our_strcasecmp(register const char * s1,register const char * s2)31 our_strcasecmp(
32 register const char *s1,
33 register const char *s2
34 )
35 #else /* NOT HAVE_STDC */
36 our_strcasecmp(s1, s2)
37 register const char *s1;
38 register const char *s2;
39 #endif /* HAVE_STDC */
40 {
41     while ((*s1) && (TOUPPER(*s1) == TOUPPER(*s2)))
42     {
43 	s1++;
44 	s2++;
45     }
46     return((int)(TOUPPER(*s1) - TOUPPER(*s2)));
47 }
48 
49 #ifdef TOUPPER
50 #undef TOUPPER
51 #endif /* TOUPPER */
52 
53 
54 /**********************************************************************/
55 /****************************** strncasecmp ***************************/
56 /**********************************************************************/
57 
58 
59 /***********************************************************************
60 Compare strings ignoring case, stopping after n characters, or at
61 end-of-string, whichever comes first.
62 ***********************************************************************/
63 
64 int
65 #if HAVE_STDC
our_strncasecmp(const char * s1,const char * s2,size_t n)66 our_strncasecmp(
67 const char	*s1,
68 const char	*s2,
69 size_t		n
70 )
71 #else /* NOT HAVE_STDC */
72 our_strncasecmp(s1,s2,n)
73 const char	*s1;
74 const char	*s2;
75 size_t		n;
76 #endif /* HAVE_STDC */
77 {
78     int	   c1;
79     int	   c2;
80     int	   result;
81 
82     for (; (n > 0) && *s1 && *s2; ++s1, ++s2, --n)
83     {
84 	c1 = 0xff & (islower((int)(*s1)) ? (int)*s1 : tolower((int)(*s1)));
85 	c2 = 0xff & (islower((int)(*s2)) ? (int)*s2 : tolower((int)(*s2)));
86 	if (c1 < c2)
87 	    return (-1);
88 	else if (c1 > c2)
89 	    return (1);
90     }
91     if (n <= 0)		   /* first n characters match */
92 	result = 0;
93     else if (*s1 == '\0')
94 	result = ((*s2 == '\0') ? 0 : -1);
95     else /* (*s2 == '\0') */
96 	result = 1;
97 
98     return (result);
99 }
100 
101 #endif /* end USE_OUR_CASECMP */
102