1*497a5275Schristos /* $NetBSD: memrchr.c,v 1.2 2009/04/11 21:42:16 christos Exp $ */
23cf907e2Schristos
33cf907e2Schristos /*-
43cf907e2Schristos * Copyright (c) 2008 The NetBSD Foundation, Inc.
53cf907e2Schristos * All rights reserved.
63cf907e2Schristos *
73cf907e2Schristos * This code is derived from software contributed to The NetBSD Foundation
83cf907e2Schristos * by Christos Zoulas.
93cf907e2Schristos *
103cf907e2Schristos * Redistribution and use in source and binary forms, with or without
113cf907e2Schristos * modification, are permitted provided that the following conditions
123cf907e2Schristos * are met:
133cf907e2Schristos * 1. Redistributions of source code must retain the above copyright
143cf907e2Schristos * notice, this list of conditions and the following disclaimer.
153cf907e2Schristos * 2. Redistributions in binary form must reproduce the above copyright
163cf907e2Schristos * notice, this list of conditions and the following disclaimer in the
173cf907e2Schristos * documentation and/or other materials provided with the distribution.
183cf907e2Schristos *
193cf907e2Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
203cf907e2Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
213cf907e2Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
223cf907e2Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
233cf907e2Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
243cf907e2Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
253cf907e2Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
263cf907e2Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
273cf907e2Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
283cf907e2Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
293cf907e2Schristos * POSSIBILITY OF SUCH DAMAGE.
303cf907e2Schristos */
313cf907e2Schristos #include <sys/cdefs.h>
323cf907e2Schristos #if defined(LIBC_SCCS) && !defined(lint)
33*497a5275Schristos __RCSID("$NetBSD: memrchr.c,v 1.2 2009/04/11 21:42:16 christos Exp $");
343cf907e2Schristos #endif /* LIBC_SCCS and not lint */
353cf907e2Schristos
363cf907e2Schristos #include <assert.h>
373cf907e2Schristos #include <string.h>
383cf907e2Schristos
393cf907e2Schristos void *
memrchr(const void * s,int c,size_t n)403cf907e2Schristos memrchr(const void *s, int c, size_t n)
413cf907e2Schristos {
423cf907e2Schristos _DIAGASSERT(s != NULL);
433cf907e2Schristos
443cf907e2Schristos if (n != 0) {
45*497a5275Schristos const unsigned char *p = (const unsigned char *)s + n;
463cf907e2Schristos const unsigned char cmp = c;
473cf907e2Schristos
483cf907e2Schristos do {
49*497a5275Schristos if (*--p == cmp)
50*497a5275Schristos return __UNCONST(p);
513cf907e2Schristos } while (--n != 0);
523cf907e2Schristos }
533cf907e2Schristos return NULL;
543cf907e2Schristos }
55