xref: /titanic_50/usr/src/cmd/sendmail/libsm/strrevcmp.c (revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c)
1 /*
2  * Copyright (c) 2001 Sendmail, 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
12 
13 #include <sm/gen.h>
14 SM_RCSID("@(#)$Id: strrevcmp.c,v 1.2 2001/08/27 22:21:51 gshapiro Exp $")
15 
16 #include <sm/config.h>
17 #include <sm/string.h>
18 #include <string.h>
19 
20 /* strcasecmp.c */
21 extern const unsigned char charmap[];
22 
23 /*
24 **  SM_STRREVCASECMP -- compare two strings starting at the end (ignore case)
25 **
26 **	Parameters:
27 **		s1 -- first string.
28 **		s2 -- second string.
29 **
30 **	Returns:
31 **		strcasecmp(reverse(s1), reverse(s2))
32 */
33 
34 int
35 sm_strrevcasecmp(s1, s2)
36 	const char *s1, *s2;
37 {
38 	register int i1, i2;
39 
40 	i1 = strlen(s1) - 1;
41 	i2 = strlen(s2) - 1;
42 	while (i1 >= 0 && i2 >= 0 &&
43 	       charmap[(unsigned char) s1[i1]] ==
44 	       charmap[(unsigned char) s2[i2]])
45 	{
46 		--i1;
47 		--i2;
48 	}
49 	if (i1 < 0)
50 	{
51 		if (i2 < 0)
52 			return 0;
53 		else
54 			return -1;
55 	}
56 	else
57 	{
58 		if (i2 < 0)
59 			return 1;
60 		else
61 			return (charmap[(unsigned char) s1[i1]] -
62 				charmap[(unsigned char) s2[i2]]);
63 	}
64 }
65 /*
66 **  SM_STRREVCMP -- compare two strings starting at the end
67 **
68 **	Parameters:
69 **		s1 -- first string.
70 **		s2 -- second string.
71 **
72 **	Returns:
73 **		strcmp(reverse(s1), reverse(s2))
74 */
75 
76 int
77 sm_strrevcmp(s1, s2)
78 	const char *s1, *s2;
79 {
80 	register int i1, i2;
81 
82 	i1 = strlen(s1) - 1;
83 	i2 = strlen(s2) - 1;
84 	while (i1 >= 0 && i2 >= 0 && s1[i1] == s2[i2])
85 	{
86 		--i1;
87 		--i2;
88 	}
89 	if (i1 < 0)
90 	{
91 		if (i2 < 0)
92 			return 0;
93 		else
94 			return -1;
95 	}
96 	else
97 	{
98 		if (i2 < 0)
99 			return 1;
100 		else
101 			return s1[i1] - s2[i2];
102 	}
103 }
104