strrevcmp.c revision 266527
12116Sjkh/*
22116Sjkh * Copyright (c) 2001 Proofpoint, Inc. and its suppliers.
32116Sjkh *	All rights reserved.
42116Sjkh *
52116Sjkh * By using this file, you agree to the terms and conditions set
62116Sjkh * forth in the LICENSE file which can be found at the top level of
72116Sjkh * the sendmail distribution.
82116Sjkh *
92116Sjkh */
102116Sjkh
118870Srgrimes#include <sm/gen.h>
122116SjkhSM_RCSID("@(#)$Id: strrevcmp.c,v 1.6 2013-11-22 20:51:43 ca Exp $")
132116Sjkh
142116Sjkh#include <sm/config.h>
152116Sjkh#include <sm/string.h>
16176207Sbde#include <string.h>
17176207Sbde
182116Sjkh/* strcasecmp.c */
192116Sjkhextern const unsigned char charmap[];
202116Sjkh
212116Sjkh/*
222116Sjkh**  SM_STRREVCASECMP -- compare two strings starting at the end (ignore case)
232116Sjkh**
242116Sjkh**	Parameters:
2597413Salfred**		s1 -- first string.
2697413Salfred**		s2 -- second string.
272116Sjkh**
282116Sjkh**	Returns:
292116Sjkh**		strcasecmp(reverse(s1), reverse(s2))
302116Sjkh*/
312116Sjkh
322116Sjkhint
332116Sjkhsm_strrevcasecmp(s1, s2)
342116Sjkh	const char *s1, *s2;
352116Sjkh{
362116Sjkh	register int i1, i2;
372116Sjkh
382116Sjkh	i1 = strlen(s1) - 1;
392116Sjkh	i2 = strlen(s2) - 1;
402116Sjkh	while (i1 >= 0 && i2 >= 0 &&
412116Sjkh	       charmap[(unsigned char) s1[i1]] ==
42176207Sbde	       charmap[(unsigned char) s2[i2]])
432116Sjkh	{
442116Sjkh		--i1;
452116Sjkh		--i2;
462116Sjkh	}
472116Sjkh	if (i1 < 0)
482116Sjkh	{
492116Sjkh		if (i2 < 0)
502116Sjkh			return 0;
512116Sjkh		else
522116Sjkh			return -1;
532116Sjkh	}
542116Sjkh	else
552116Sjkh	{
562116Sjkh		if (i2 < 0)
572116Sjkh			return 1;
582116Sjkh		else
592116Sjkh			return (charmap[(unsigned char) s1[i1]] -
602116Sjkh				charmap[(unsigned char) s2[i2]]);
612116Sjkh	}
62176207Sbde}
632116Sjkh/*
642116Sjkh**  SM_STRREVCMP -- compare two strings starting at the end
652116Sjkh**
66**	Parameters:
67**		s1 -- first string.
68**		s2 -- second string.
69**
70**	Returns:
71**		strcmp(reverse(s1), reverse(s2))
72*/
73
74int
75sm_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