memchr.c revision 165903
192108Sphk/*-
292108Sphk * Copyright (c) 1990, 1993
392108Sphk *	The Regents of the University of California.  All rights reserved.
492108Sphk *
592108Sphk * This code is derived from software contributed to Berkeley by
692108Sphk * Chris Torek.
792108Sphk *
892108Sphk * Redistribution and use in source and binary forms, with or without
992108Sphk * modification, are permitted provided that the following conditions
1092108Sphk * are met:
1192108Sphk * 1. Redistributions of source code must retain the above copyright
1292108Sphk *    notice, this list of conditions and the following disclaimer.
1392108Sphk * 2. Redistributions in binary form must reproduce the above copyright
1492108Sphk *    notice, this list of conditions and the following disclaimer in the
1592108Sphk *    documentation and/or other materials provided with the distribution.
1692108Sphk * 4. Neither the name of the University nor the names of its contributors
1792108Sphk *    may be used to endorse or promote products derived from this software
1892108Sphk *    without specific prior written permission.
1992108Sphk *
2092108Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2192108Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2292108Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2392108Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2492108Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2592108Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2692108Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2792108Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2892108Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2992108Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3092108Sphk * SUCH DAMAGE.
3192108Sphk */
3292108Sphk
3392108Sphk#if defined(LIBC_SCCS) && !defined(lint)
3492108Sphkstatic char sccsid[] = "@(#)memchr.c	8.1 (Berkeley) 6/4/93";
3592108Sphk#endif /* LIBC_SCCS and not lint */
3692108Sphk#include <sys/cdefs.h>
3792108Sphk__FBSDID("$FreeBSD: head/lib/libc/string/memchr.c 165903 2007-01-09 00:28:16Z imp $");
38104064Sphk
39104064Sphk#include <string.h>
40104064Sphk
4192108Sphkvoid *
4292108Sphkmemchr(s, c, n)
4392108Sphk	const void *s;
44105542Sphk	unsigned char c;
4592108Sphk	size_t n;
4692108Sphk{
4792108Sphk	if (n != 0) {
48113712Sphk		const unsigned char *p = s;
49113712Sphk
50113712Sphk		do {
51113713Sphk			if (*p++ == c)
52113713Sphk				return ((void *)(p - 1));
53113713Sphk		} while (--n != 0);
54113712Sphk	}
55113712Sphk	return (NULL);
5692108Sphk}
5792108Sphk