[PATCH 1/7] lib/string: add strnchrnul()

From: Yury Norov
Date: Sun Jul 21 2019 - 17:28:41 EST


>From Yury Norov <ynorov@xxxxxxxxxxx>

New function works like strchrnul() with a length limited strings.

Reviewed-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
Signed-off-by: Yury Norov <ynorov@xxxxxxxxxxx>
Signed-off-by: Yury Norov <yury.norov@xxxxxxxxx>
---
include/linux/string.h | 1 +
lib/string.c | 17 +++++++++++++++++
2 files changed, 18 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 4deb11f7976bc..ae934d6c50bf5 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -62,6 +62,7 @@ extern char * strchr(const char *,int);
#ifndef __HAVE_ARCH_STRCHRNUL
extern char * strchrnul(const char *,int);
#endif
+extern char * strnchrnul(const char *, size_t, int);
#ifndef __HAVE_ARCH_STRNCHR
extern char * strnchr(const char *, size_t, int);
#endif
diff --git a/lib/string.c b/lib/string.c
index 461fb620f85f3..5b5fee266c193 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -432,6 +432,23 @@ char *strchrnul(const char *s, int c)
EXPORT_SYMBOL(strchrnul);
#endif

+/**
+ * strnchrnul - Find and return a character in a length limited string,
+ * or end of string
+ * @s: The string to be searched
+ * @count: The number of characters to be searched
+ * @c: The character to search for
+ *
+ * Returns pointer to the first occurrence of 'c' in s. If c is not found,
+ * then return a pointer to the last character of the string.
+ */
+char *strnchrnul(const char *s, size_t count, int c)
+{
+ while (count-- && *s && *s != (char)c)
+ s++;
+ return (char *)s;
+}
+
#ifndef __HAVE_ARCH_STRRCHR
/**
* strrchr - Find the last occurrence of a character in a string
--
2.20.1