diff options
| author | Andy Shevchenko <[email protected]> | 2023-08-04 14:39:07 +0000 |
|---|---|---|
| committer | Stephen Boyd <[email protected]> | 2023-08-05 01:21:50 +0000 |
| commit | 045ad46441a1cf2ff358af0126f8c57f337804fa (patch) | |
| tree | 0d8745d448f22648486c123c18e055aef20be105 /lib/string_helpers.c | |
| parent | Linux 6.5-rc1 (diff) | |
| download | kernel-045ad46441a1cf2ff358af0126f8c57f337804fa.tar.gz kernel-045ad46441a1cf2ff358af0126f8c57f337804fa.zip | |
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 <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Stephen Boyd <[email protected]>
Diffstat (limited to 'lib/string_helpers.c')
| -rw-r--r-- | lib/string_helpers.c | 15 |
1 files changed, 15 insertions, 0 deletions
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 |
