1164640Sflz/*	$OpenBSD: memrchr.c,v 1.2 2007/11/27 16:22:12 martynas Exp $	*/
298186Sgordon
378344Sobrien/*
4157473Sflz * Copyright (c) 2007 Todd C. Miller <Todd.Miller@courtesan.com>
578344Sobrien *
678344Sobrien * Permission to use, copy, modify, and distribute this software for any
778344Sobrien * purpose with or without fee is hereby granted, provided that the above
878344Sobrien * copyright notice and this permission notice appear in all copies.
978344Sobrien *
1078344Sobrien * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1178344Sobrien * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1278344Sobrien * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1378344Sobrien * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1478344Sobrien * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1578344Sobrien * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1678344Sobrien * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1778344Sobrien *
1878344Sobrien * $FreeBSD$
1978344Sobrien */
2078344Sobrien
2178344Sobrien#include <string.h>
2278344Sobrien
2378344Sobrien/*
2478344Sobrien * Reverse memchr()
2578344Sobrien * Find the last occurrence of 'c' in the buffer 's' of size 'n'.
2678344Sobrien */
2778344Sobrienvoid *
2878344Sobrienmemrchr(const void *s, int c, size_t n)
2978344Sobrien{
3078344Sobrien	const unsigned char *cp;
3178344Sobrien
3278344Sobrien	if (n != 0) {
3378344Sobrien		cp = (unsigned char *)s + n;
3478344Sobrien		do {
35169668Smtm			if (*(--cp) == (unsigned char)c)
36157473Sflz				return((void *)cp);
3778344Sobrien		} while (--n != 0);
3898186Sgordon	}
3998186Sgordon	return(NULL);
4098186Sgordon}
41131550Scperciva