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