xref: /openbsd-src/sys/lib/libkern/memchr.c (revision b5be37d2a6fbef83b90ca99497343fa8dbb0f3fc)
1*b5be37d2Sderaadt /*	$OpenBSD: memchr.c,v 1.6 2014/06/10 04:16:57 deraadt Exp $	*/
2f47c597bSchuck 
3f47c597bSchuck /*-
4f47c597bSchuck  * Copyright (c) 1990 The Regents of the University of California.
5f47c597bSchuck  * All rights reserved.
6f47c597bSchuck  *
7f47c597bSchuck  * This code is derived from software contributed to Berkeley by
8f47c597bSchuck  * Chris Torek.
9f47c597bSchuck  *
10f47c597bSchuck  * Redistribution and use in source and binary forms, with or without
11f47c597bSchuck  * modification, are permitted provided that the following conditions
12f47c597bSchuck  * are met:
13f47c597bSchuck  * 1. Redistributions of source code must retain the above copyright
14f47c597bSchuck  *    notice, this list of conditions and the following disclaimer.
15f47c597bSchuck  * 2. Redistributions in binary form must reproduce the above copyright
16f47c597bSchuck  *    notice, this list of conditions and the following disclaimer in the
17f47c597bSchuck  *    documentation and/or other materials provided with the distribution.
1829295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
19f47c597bSchuck  *    may be used to endorse or promote products derived from this software
20f47c597bSchuck  *    without specific prior written permission.
21f47c597bSchuck  *
22f47c597bSchuck  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23f47c597bSchuck  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24f47c597bSchuck  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25f47c597bSchuck  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26f47c597bSchuck  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27f47c597bSchuck  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28f47c597bSchuck  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29f47c597bSchuck  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30f47c597bSchuck  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31f47c597bSchuck  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32f47c597bSchuck  * SUCH DAMAGE.
33f47c597bSchuck  */
34f47c597bSchuck 
35f47c597bSchuck #include <lib/libkern/libkern.h>
36f47c597bSchuck #define	NULL	((char *)0)
37f47c597bSchuck 
38f47c597bSchuck void *
memchr(const void * s,int c,size_t n)39a26aa419Sderaadt memchr(const void *s, int c, size_t n)
40f47c597bSchuck {
41f47c597bSchuck 	if (n != 0) {
42a26aa419Sderaadt 		const unsigned char *p = s;
43f47c597bSchuck 
44f47c597bSchuck 		do {
45a26aa419Sderaadt 			if (*p++ == (unsigned char)c)
46f47c597bSchuck 				return ((void *)(p - 1));
47f47c597bSchuck 		} while (--n != 0);
48f47c597bSchuck 	}
49f47c597bSchuck 	return (NULL);
50f47c597bSchuck }
51