diff options
author | Li Zefan <lizf@cn.fujitsu.com> | 2010-01-13 21:53:55 -0500 |
---|---|---|
committer | Steven Rostedt <rostedt@goodmis.org> | 2010-01-14 22:38:09 -0500 |
commit | d5f1fb53353edc38da326445267c1df0c9676df2 (patch) | |
tree | 42def644ebd0d343f2eba9e625f4b9b1ea8a36f0 | |
parent | a3291c14ecf0a995e30d993b7f2cae031de98727 (diff) |
lib: Introduce strnstr()
It differs strstr() in that it limits the length to be searched
in the first string.
Signed-off-by: Li Zefan <lizf@cn.fujitsu.com>
LKML-Reference: <4B4E8743.6030805@cn.fujitsu.com>
Acked-by: Frederic Weisbecker <fweisbec@gmail.com>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
-rw-r--r-- | include/linux/string.h | 5 | ||||
-rw-r--r-- | lib/string.c | 27 |
2 files changed, 30 insertions, 2 deletions
diff --git a/include/linux/string.h b/include/linux/string.h index 651839a2a755..a716ee2a8adb 100644 --- a/include/linux/string.h +++ b/include/linux/string.h | |||
@@ -72,7 +72,10 @@ static inline __must_check char *strstrip(char *str) | |||
72 | } | 72 | } |
73 | 73 | ||
74 | #ifndef __HAVE_ARCH_STRSTR | 74 | #ifndef __HAVE_ARCH_STRSTR |
75 | extern char * strstr(const char *,const char *); | 75 | extern char * strstr(const char *, const char *); |
76 | #endif | ||
77 | #ifndef __HAVE_ARCH_STRNSTR | ||
78 | extern char * strnstr(const char *, const char *, size_t); | ||
76 | #endif | 79 | #endif |
77 | #ifndef __HAVE_ARCH_STRLEN | 80 | #ifndef __HAVE_ARCH_STRLEN |
78 | extern __kernel_size_t strlen(const char *); | 81 | extern __kernel_size_t strlen(const char *); |
diff --git a/lib/string.c b/lib/string.c index 9f75b4ec50b8..a1cdcfcc42d0 100644 --- a/lib/string.c +++ b/lib/string.c | |||
@@ -667,7 +667,7 @@ EXPORT_SYMBOL(memscan); | |||
667 | */ | 667 | */ |
668 | char *strstr(const char *s1, const char *s2) | 668 | char *strstr(const char *s1, const char *s2) |
669 | { | 669 | { |
670 | int l1, l2; | 670 | size_t l1, l2; |
671 | 671 | ||
672 | l2 = strlen(s2); | 672 | l2 = strlen(s2); |
673 | if (!l2) | 673 | if (!l2) |
@@ -684,6 +684,31 @@ char *strstr(const char *s1, const char *s2) | |||
684 | EXPORT_SYMBOL(strstr); | 684 | EXPORT_SYMBOL(strstr); |
685 | #endif | 685 | #endif |
686 | 686 | ||
687 | #ifndef __HAVE_ARCH_STRNSTR | ||
688 | /** | ||
689 | * strnstr - Find the first substring in a length-limited string | ||
690 | * @s1: The string to be searched | ||
691 | * @s2: The string to search for | ||
692 | * @len: the maximum number of characters to search | ||
693 | */ | ||
694 | char *strnstr(const char *s1, const char *s2, size_t len) | ||
695 | { | ||
696 | size_t l1 = len, l2; | ||
697 | |||
698 | l2 = strlen(s2); | ||
699 | if (!l2) | ||
700 | return (char *)s1; | ||
701 | while (l1 >= l2) { | ||
702 | l1--; | ||
703 | if (!memcmp(s1, s2, l2)) | ||
704 | return (char *)s1; | ||
705 | s1++; | ||
706 | } | ||
707 | return NULL; | ||
708 | } | ||
709 | EXPORT_SYMBOL(strnstr); | ||
710 | #endif | ||
711 | |||
687 | #ifndef __HAVE_ARCH_MEMCHR | 712 | #ifndef __HAVE_ARCH_MEMCHR |
688 | /** | 713 | /** |
689 | * memchr - Find a character in an area of memory. | 714 | * memchr - Find a character in an area of memory. |