xref: /netbsd-src/external/bsd/nvi/dist/clib/memchr.c (revision 2f698edb5c1cb2dcd9e762b0abb50c41dde8b6b7)
1dbd550edSchristos /*-
2dbd550edSchristos  * Copyright (c) 1990, 1993
3dbd550edSchristos  *	The Regents of the University of California.  All rights reserved.
4dbd550edSchristos  *
5dbd550edSchristos  * This code is derived from software contributed to Berkeley by
6dbd550edSchristos  * Chris Torek.
7dbd550edSchristos  *
8dbd550edSchristos  * Redistribution and use in source and binary forms, with or without
9dbd550edSchristos  * modification, are permitted provided that the following conditions
10dbd550edSchristos  * are met:
11dbd550edSchristos  * 1. Redistributions of source code must retain the above copyright
12dbd550edSchristos  *    notice, this list of conditions and the following disclaimer.
13dbd550edSchristos  * 2. Redistributions in binary form must reproduce the above copyright
14dbd550edSchristos  *    notice, this list of conditions and the following disclaimer in the
15dbd550edSchristos  *    documentation and/or other materials provided with the distribution.
16dbd550edSchristos  * 3. All advertising materials mentioning features or use of this software
17dbd550edSchristos  *    must display the following acknowledgement:
18dbd550edSchristos  *	This product includes software developed by the University of
19dbd550edSchristos  *	California, Berkeley and its contributors.
20dbd550edSchristos  * 4. Neither the name of the University nor the names of its contributors
21dbd550edSchristos  *    may be used to endorse or promote products derived from this software
22dbd550edSchristos  *    without specific prior written permission.
23dbd550edSchristos  *
24dbd550edSchristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25dbd550edSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26dbd550edSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27dbd550edSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28dbd550edSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29dbd550edSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30dbd550edSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31dbd550edSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32dbd550edSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33dbd550edSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34dbd550edSchristos  * SUCH DAMAGE.
35dbd550edSchristos  */
36dbd550edSchristos 
37dbd550edSchristos #include "config.h"
38dbd550edSchristos 
39dbd550edSchristos #if defined(LIBC_SCCS) && !defined(lint)
40dbd550edSchristos static const char sccsid[] = "@(#)memchr.c	8.1 (Berkeley) 6/4/93";
41dbd550edSchristos #endif /* LIBC_SCCS and not lint */
42*2f698edbSchristos #else
43*2f698edbSchristos __RCSID("$NetBSD: memchr.c,v 1.2 2014/01/26 21:43:45 christos Exp $");
44*2f698edbSchristos #endif
45dbd550edSchristos 
46dbd550edSchristos #include <string.h>
47dbd550edSchristos 
48dbd550edSchristos /*
49dbd550edSchristos  * PUBLIC: #ifndef HAVE_MEMCHR
50dbd550edSchristos  * PUBLIC: void *memchr __P((const void *, int, size_t));
51dbd550edSchristos  * PUBLIC: #endif
52dbd550edSchristos  */
53dbd550edSchristos void *
memchr(const void * s,register unsigned char c,register size_t n)54dbd550edSchristos memchr(const void *s, register unsigned char c, register size_t n)
55dbd550edSchristos {
56dbd550edSchristos 	if (n != 0) {
57dbd550edSchristos 		register const unsigned char *p = s;
58dbd550edSchristos 
59dbd550edSchristos 		do {
60dbd550edSchristos 			if (*p++ == c)
61dbd550edSchristos 				return ((void *)(p - 1));
62dbd550edSchristos 		} while (--n != 0);
63dbd550edSchristos 	}
64dbd550edSchristos 	return (NULL);
65dbd550edSchristos }
66