[PATCH 03/14] string: implement strstarts along with strends

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Apr 12 23:34:51 BST 2021


Both can be useful when parsing file paths. They are added to different
files, because only one of them is available in upstream
<linux/string.h>. The other we add to <string.h>, which contains more
barebox-specific string functions.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 include/linux/string.h | 10 ++++++++++
 include/string.h       |  1 +
 lib/string.c           |  9 +++++++++
 3 files changed, 20 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 58e2a4d2ea66..55bc905c0e6b 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -136,4 +136,14 @@ extern int kstrtobool(const char *s, bool *res);
 
 int match_string(const char * const *array, size_t n, const char *string);
 
+/**
+ * strstarts - does @str start with @prefix?
+ * @str: string to examine
+ * @prefix: prefix to look for.
+ */
+static inline bool strstarts(const char *str, const char *prefix)
+{
+	return strncmp(str, prefix, strlen(prefix)) == 0;
+}
+
 #endif /* _LINUX_STRING_H_ */
diff --git a/include/string.h b/include/string.h
index ef0b5e199eea..d423bee6fba5 100644
--- a/include/string.h
+++ b/include/string.h
@@ -7,6 +7,7 @@
 int strtobool(const char *str, int *val);
 char *strsep_unescaped(char **, const char *);
 char *stpcpy(char *dest, const char *src);
+bool strends(const char *str, const char *postfix);
 
 void *__default_memset(void *, int, __kernel_size_t);
 void *__nokasan_default_memset(void *, int, __kernel_size_t);
diff --git a/lib/string.c b/lib/string.c
index dbb66fe4d2cc..bad186586fac 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -866,6 +866,15 @@ int strtobool(const char *str, int *val)
 }
 EXPORT_SYMBOL(strtobool);
 
+bool strends(const char *str, const char *postfix)
+{
+	if (strlen(str) < strlen(postfix))
+		return false;
+
+	return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
+}
+EXPORT_SYMBOL(strends);
+
 /**
  * match_string - matches given string in an array
  * @array:	array of strings
-- 
2.29.2




More information about the barebox mailing list