memmem.c revision 149466
1149466Sandre/*- 2149466Sandre * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com> 3149466Sandre * 4149466Sandre * Redistribution and use in source and binary forms, with or without 5149466Sandre * modification, are permitted provided that the following conditions 6149466Sandre * are met: 7149466Sandre * 1. Redistributions of source code must retain the above copyright 8149466Sandre * notice, this list of conditions and the following disclaimer. 9149466Sandre * 2. Redistributions in binary form must reproduce the above copyright 10149466Sandre * notice, this list of conditions and the following disclaimer in the 11149466Sandre * documentation and/or other materials provided with the distribution. 12149466Sandre * 3. The name of the author may not be used to endorse or promote 13149466Sandre * products derived from this software without specific prior written 14149466Sandre * permission. 15149466Sandre * 16149466Sandre * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17149466Sandre * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18149466Sandre * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19149466Sandre * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20149466Sandre * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21149466Sandre * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22149466Sandre * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23149466Sandre * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24149466Sandre * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25149466Sandre * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26149466Sandre * SUCH DAMAGE. 27149466Sandre */ 28149466Sandre 29149466Sandre#include <sys/cdefs.h> 30149466Sandre__FBSDID("$FreeBSD: head/lib/libc/string/memmem.c 149466 2005-08-25 18:26:58Z andre $"); 31149466Sandre 32149466Sandre#include <string.h> 33149466Sandre 34149466Sandre/* 35149466Sandre * Find the first occurrence of the byte string s in byte string l. 36149466Sandre */ 37149466Sandre 38149466Sandrevoid * 39149466Sandrememmem(l, l_len, s, s_len) 40149466Sandre const void *l; size_t l_len; 41149466Sandre const void *s; size_t s_len; 42149466Sandre{ 43149466Sandre register char *cur, *last; 44149466Sandre const char *cl = (const char *)l; 45149466Sandre const char *cs = (const char *)s; 46149466Sandre 47149466Sandre /* we need something to compare */ 48149466Sandre if (l_len == 0 || s_len == 0) 49149466Sandre return NULL; 50149466Sandre 51149466Sandre /* "s" must be smaller or equal to "l" */ 52149466Sandre if (l_len < s_len) 53149466Sandre return NULL; 54149466Sandre 55149466Sandre /* special case where s_len == 1 */ 56149466Sandre if (s_len == 1) 57149466Sandre return memchr(l, (int)*cs, l_len); 58149466Sandre 59149466Sandre /* the last position where its possible to find "s" in "l" */ 60149466Sandre last = (char *)cl + l_len - s_len; 61149466Sandre 62149466Sandre for (cur = (char *)cl; cur <= last; cur++) 63149466Sandre if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0) 64149466Sandre return cur; 65149466Sandre 66149466Sandre return NULL; 67149466Sandre} 68