From 045ad46441a1cf2ff358af0126f8c57f337804fa Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Fri, 4 Aug 2023 17:39:07 +0300 Subject: lib/string_helpers: Add kstrdup_and_replace() helper Duplicate a NULL-terminated string and replace all occurrences of the old character with a new one. In other words, provide functionality of kstrdup() + strreplace(). Signed-off-by: Andy Shevchenko Link: https://lore.kernel.org/r/20230804143910.15504-2-andriy.shevchenko@linux.intel.com Signed-off-by: Stephen Boyd --- lib/string_helpers.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lib') diff --git a/lib/string_helpers.c b/lib/string_helpers.c index d3b1dd718daf..9982344cca34 100644 --- a/lib/string_helpers.c +++ b/lib/string_helpers.c @@ -719,6 +719,21 @@ char *kstrdup_quotable_file(struct file *file, gfp_t gfp) } EXPORT_SYMBOL_GPL(kstrdup_quotable_file); +/* + * Returns duplicate string in which the @old characters are replaced by @new. + */ +char *kstrdup_and_replace(const char *src, char old, char new, gfp_t gfp) +{ + char *dst; + + dst = kstrdup(src, gfp); + if (!dst) + return NULL; + + return strreplace(dst, old, new); +} +EXPORT_SYMBOL_GPL(kstrdup_and_replace); + /** * kasprintf_strarray - allocate and fill array of sequential strings * @gfp: flags for the slab allocator -- cgit