summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2021-05-03 13:48:48 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2021-05-03 13:55:29 +0200
commit015d5be8d66386fb9386f565b9d0e04ca78f8061 (patch)
tree108aae241706ca2d76f1e2b0c56e805609568d0a
parent7a1ae4f032053ed1c2acbfc7b5cf59aa1539e61e (diff)
downloadbarebox-015d5be8d66386fb9386f565b9d0e04ca78f8061.tar.gz
barebox-015d5be8d66386fb9386f565b9d0e04ca78f8061.tar.xz
string: implement strstarts along with strends
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@pengutronix.de> Link: https://lore.barebox.org/20210503114901.13095-4-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--include/linux/string.h10
-rw-r--r--include/string.h1
-rw-r--r--lib/string.c9
3 files changed, 20 insertions, 0 deletions
diff --git a/include/linux/string.h b/include/linux/string.h
index 58e2a4d2ea..55bc905c0e 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 ef0b5e199e..d423bee6fb 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 dbb66fe4d2..bad186586f 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