/*
 * Copyright (c) 2005 Sendmail, Inc. and its suppliers.
 *	All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 */

#include "sm/generic.h"
SM_RCSID("@(#)$Id: strlchr.c,v 1.2 2007/11/14 06:03:08 ca Exp $")
#include "sm/assert.h"
#include "sm/error.h"
#include "sm/ctype.h"
#include "sm/str.h"
#include "sm/str-int.h"

/*
**  SM_STR_LCHR -- find last occurrence of a char in str
**
**	Parameters:
**		str -- string in which to find delim
**		delims -- list of delimiter (e.g., "+" or "-")
**		pidx -- index in string at which delim appears first
**			only valid for SM_SUCCESS
**
**	Returns:
**		usual error code
*/

sm_ret_T
sm_str_lchr(sm_str_P str, const uchar *delims, uint *pidx)
{
	sm_ret_T ret;
	uchar ch;
	uint len;
	int i;

	SM_IS_BUF(str);
	SM_REQUIRE(pidx != NULL);

	ret = sm_error_perm(SM_EM_STR, SM_E_NOTFOUND);
	if (NULL == delims || '\0' == *delims)
		return ret;

	len = sm_str_getlen(str);

	/* search for delimiter */
	for (i = len - 1; i >= 0; i--) {
		ch = sm_str_rd_elem(str, i);
		if (strchr((const char *)delims, ch) != NULL) {
			*pidx = i;
			ret = SM_SUCCESS;
			break;
		}
	}
	return ret;
}


syntax highlighted by Code2HTML, v. 0.9.1