strrevcmp.c revision 261363
1314817Sngie/*
2272343Sngie * Copyright (c) 2001 Proofpoint, Inc. and its suppliers.
3272343Sngie *	All rights reserved.
4272343Sngie *
5272343Sngie * By using this file, you agree to the terms and conditions set
6272343Sngie * forth in the LICENSE file which can be found at the top level of
7272343Sngie * the sendmail distribution.
8272343Sngie *
9272343Sngie */
10272343Sngie
11272343Sngie#include <sm/gen.h>
12272343SngieSM_RCSID("@(#)$Id: strrevcmp.c,v 1.6 2013/11/22 20:51:43 ca Exp $")
13272343Sngie
14272343Sngie#include <sm/config.h>
15272343Sngie#include <sm/string.h>
16272343Sngie#include <string.h>
17272343Sngie
18272343Sngie/* strcasecmp.c */
19272343Sngieextern const unsigned char charmap[];
20272343Sngie
21272343Sngie/*
22272343Sngie**  SM_STRREVCASECMP -- compare two strings starting at the end (ignore case)
23314817Sngie**
24272343Sngie**	Parameters:
25272343Sngie**		s1 -- first string.
26272343Sngie**		s2 -- second string.
27272343Sngie**
28272343Sngie**	Returns:
29272343Sngie**		strcasecmp(reverse(s1), reverse(s2))
30272343Sngie*/
31272343Sngie
32272343Sngieint
33272343Sngiesm_strrevcasecmp(s1, s2)
34272343Sngie	const char *s1, *s2;
35272343Sngie{
36272343Sngie	register int i1, i2;
37272343Sngie
38272343Sngie	i1 = strlen(s1) - 1;
39272343Sngie	i2 = strlen(s2) - 1;
40272343Sngie	while (i1 >= 0 && i2 >= 0 &&
41272343Sngie	       charmap[(unsigned char) s1[i1]] ==
42272343Sngie	       charmap[(unsigned char) s2[i2]])
43272343Sngie	{
44272343Sngie		--i1;
45272343Sngie		--i2;
46272343Sngie	}
47272343Sngie	if (i1 < 0)
48272343Sngie	{
49272343Sngie		if (i2 < 0)
50272343Sngie			return 0;
51272343Sngie		else
52272343Sngie			return -1;
53272343Sngie	}
54272343Sngie	else
55272343Sngie	{
56272343Sngie		if (i2 < 0)
57272343Sngie			return 1;
58272343Sngie		else
59272343Sngie			return (charmap[(unsigned char) s1[i1]] -
60272343Sngie				charmap[(unsigned char) s2[i2]]);
61272343Sngie	}
62272343Sngie}
63272343Sngie/*
64272343Sngie**  SM_STRREVCMP -- compare two strings starting at the end
65272343Sngie**
66272343Sngie**	Parameters:
67272343Sngie**		s1 -- first string.
68272343Sngie**		s2 -- second string.
69272343Sngie**
70272343Sngie**	Returns:
71272343Sngie**		strcmp(reverse(s1), reverse(s2))
72272343Sngie*/
73272343Sngie
74272343Sngieint
75272343Sngiesm_strrevcmp(s1, s2)
76272343Sngie	const char *s1, *s2;
77272343Sngie{
78272343Sngie	register int i1, i2;
79272343Sngie
80272343Sngie	i1 = strlen(s1) - 1;
81272343Sngie	i2 = strlen(s2) - 1;
82272343Sngie	while (i1 >= 0 && i2 >= 0 && s1[i1] == s2[i2])
83272343Sngie	{
84272343Sngie		--i1;
85272343Sngie		--i2;
86272343Sngie	}
87272343Sngie	if (i1 < 0)
88272343Sngie	{
89272343Sngie		if (i2 < 0)
90272343Sngie			return 0;
91272343Sngie		else
92272343Sngie			return -1;
93272343Sngie	}
94272343Sngie	else
95272343Sngie	{
96272343Sngie		if (i2 < 0)
97272343Sngie			return 1;
98272343Sngie		else
99272343Sngie			return s1[i1] - s2[i2];
100272343Sngie	}
101272343Sngie}
102272343Sngie