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