Skip to content

Commit 045ad46

Browse files
andy-shevbebarino
authored andcommitted
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 <andriy.shevchenko@linux.intel.com> Link: https://lore.kernel.org/r/20230804143910.15504-2-andriy.shevchenko@linux.intel.com Signed-off-by: Stephen Boyd <sboyd@kernel.org>
1 parent 06c2afb commit 045ad46

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

include/linux/string_helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ char *kstrdup_quotable(const char *src, gfp_t gfp);
109109
char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
110110
char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
111111

112+
char *kstrdup_and_replace(const char *src, char old, char new, gfp_t gfp);
113+
112114
char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n);
113115
void kfree_strarray(char **array, size_t n);
114116

lib/string_helpers.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,21 @@ char *kstrdup_quotable_file(struct file *file, gfp_t gfp)
719719
}
720720
EXPORT_SYMBOL_GPL(kstrdup_quotable_file);
721721

722+
/*
723+
* Returns duplicate string in which the @old characters are replaced by @new.
724+
*/
725+
char *kstrdup_and_replace(const char *src, char old, char new, gfp_t gfp)
726+
{
727+
char *dst;
728+
729+
dst = kstrdup(src, gfp);
730+
if (!dst)
731+
return NULL;
732+
733+
return strreplace(dst, old, new);
734+
}
735+
EXPORT_SYMBOL_GPL(kstrdup_and_replace);
736+
722737
/**
723738
* kasprintf_strarray - allocate and fill array of sequential strings
724739
* @gfp: flags for the slab allocator

0 commit comments

Comments
 (0)