xref: /freebsd/contrib/sendmail/libsm/strrevcmp.c (revision ee7b0571c2c18bdec848ed2044223cc88db29bd8)
1 /*
2  * Copyright (c) 2001 Proofpoint, Inc. and its suppliers.
3  *	All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  *
9  */
10 
11 #include <sm/gen.h>
12 SM_RCSID("@(#)$Id: strrevcmp.c,v 1.6 2013-11-22 20:51:43 ca Exp $")
13 
14 #include <sm/config.h>
15 #include <sm/string.h>
16 #include <string.h>
17 
18 /* strcasecmp.c */
19 extern const unsigned char charmap[];
20 
21 /*
22 **  SM_STRREVCASECMP -- compare two strings starting at the end (ignore case)
23 **
24 **	Parameters:
25 **		s1 -- first string.
26 **		s2 -- second string.
27 **
28 **	Returns:
29 **		strcasecmp(reverse(s1), reverse(s2))
30 */
31 
32 int
sm_strrevcasecmp(s1,s2)33 sm_strrevcasecmp(s1, s2)
34 	const char *s1, *s2;
35 {
36 	register int i1, i2;
37 
38 	i1 = strlen(s1) - 1;
39 	i2 = strlen(s2) - 1;
40 	while (i1 >= 0 && i2 >= 0 &&
41 	       charmap[(unsigned char) s1[i1]] ==
42 	       charmap[(unsigned char) s2[i2]])
43 	{
44 		--i1;
45 		--i2;
46 	}
47 	if (i1 < 0)
48 	{
49 		if (i2 < 0)
50 			return 0;
51 		else
52 			return -1;
53 	}
54 	else
55 	{
56 		if (i2 < 0)
57 			return 1;
58 		else
59 			return (charmap[(unsigned char) s1[i1]] -
60 				charmap[(unsigned char) s2[i2]]);
61 	}
62 }
63 /*
64 **  SM_STRREVCMP -- compare two strings starting at the end
65 **
66 **	Parameters:
67 **		s1 -- first string.
68 **		s2 -- second string.
69 **
70 **	Returns:
71 **		strcmp(reverse(s1), reverse(s2))
72 */
73 
74 int
sm_strrevcmp(s1,s2)75 sm_strrevcmp(s1, s2)
76 	const char *s1, *s2;
77 {
78 	register int i1, i2;
79 
80 	i1 = strlen(s1) - 1;
81 	i2 = strlen(s2) - 1;
82 	while (i1 >= 0 && i2 >= 0 && s1[i1] == s2[i2])
83 	{
84 		--i1;
85 		--i2;
86 	}
87 	if (i1 < 0)
88 	{
89 		if (i2 < 0)
90 			return 0;
91 		else
92 			return -1;
93 	}
94 	else
95 	{
96 		if (i2 < 0)
97 			return 1;
98 		else
99 			return s1[i1] - s2[i2];
100 	}
101 }
102