From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754138AbbBFEAS (ORCPT ); Thu, 5 Feb 2015 23:00:18 -0500 Received: from zeniv.linux.org.uk ([195.92.253.2]:51298 "EHLO ZenIV.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752896AbbBFEAR (ORCPT ); Thu, 5 Feb 2015 23:00:17 -0500 From: Al Viro To: Steven Rostedt Cc: linux-kernel@vger.kernel.org Subject: [RFC][PATCH v2 2/7] implement memmem() Date: Fri, 6 Feb 2015 04:00:10 +0000 Message-Id: <1423195215-25134-2-git-send-email-viro@ZenIV.linux.org.uk> X-Mailer: git-send-email 1.7.7.6 In-Reply-To: <20150205224632.GW29656@ZenIV.linux.org.uk> References: <20150205224632.GW29656@ZenIV.linux.org.uk> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Al Viro Signed-off-by: Al Viro --- include/linux/string.h | 1 + lib/string.c | 34 ++++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/include/linux/string.h b/include/linux/string.h index 2e22a2e..87f9fba 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -75,6 +75,7 @@ extern char * strstr(const char *, const char *); #endif #ifndef __HAVE_ARCH_STRNSTR extern char * strnstr(const char *, const char *, size_t); +extern void * memmem(const void *, size_t, const void *, size_t); #endif #ifndef __HAVE_ARCH_STRLEN extern __kernel_size_t strlen(const char *); diff --git a/lib/string.c b/lib/string.c index 1006330..2035dbe 100644 --- a/lib/string.c +++ b/lib/string.c @@ -740,27 +740,37 @@ EXPORT_SYMBOL(strstr); #endif #ifndef __HAVE_ARCH_STRNSTR + /** - * strnstr - Find the first substring in a length-limited string + * memmem - Find the first length-limited substring in a length-limited string * @s1: The string to be searched + * @len1: the maximum number of characters to search * @s2: The string to search for - * @len: the maximum number of characters to search + * @len2: the length of the string being searched */ -char *strnstr(const char *s1, const char *s2, size_t len) +void *memmem(const void *s1, size_t len1, const void *s2, size_t len2) { - size_t l2; - - l2 = strlen(s2); - if (!l2) - return (char *)s1; - while (len >= l2) { - len--; - if (!memcmp(s1, s2, l2)) - return (char *)s1; + if (!len2) + return (void *)s1; + while (len1 >= len2) { + len1--; + if (!memcmp(s1, s2, len2)) + return (void *)s1; s1++; } return NULL; } +EXPORT_SYMBOL(memmem); +/** + * strnstr - Find the first substring in a length-limited string + * @s1: The string to be searched + * @s2: The string to search for + * @len: the maximum number of characters to search + */ +char *strnstr(const char *s1, const char *s2, size_t len) +{ + return memmem(s1, len, s2, strlen(s2)); +} EXPORT_SYMBOL(strnstr); #endif -- 2.1.4