From: Daan De Meyer Date: Fri, 23 Sep 2022 10:40:13 +0000 (+0200) Subject: basic: Add strgrowpad0() X-Git-Tag: v252-rc1~98^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2812017cfb5a427cb5ee06bbe559274e77939f8b;p=thirdparty%2Fsystemd.git basic: Add strgrowpad0() --- diff --git a/src/basic/string-util.c b/src/basic/string-util.c index 128aea99c02..17d35fe1a45 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -521,6 +521,19 @@ char* strshorten(char *s, size_t l) { return s; } +int strgrowpad0(char **s, size_t l) { + assert(s); + + char *q = realloc(*s, l); + if (!q) + return -ENOMEM; + *s = q; + + size_t sz = strlen(*s); + memzero(*s + sz, l - sz); + return 0; +} + char *strreplace(const char *text, const char *old_string, const char *new_string) { size_t l, old_len, new_len; char *t, *ret = NULL; diff --git a/src/basic/string-util.h b/src/basic/string-util.h index 1dd46f7f203..0703c848f0a 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -152,6 +152,8 @@ char *cellescape(char *buf, size_t len, const char *s); char* strshorten(char *s, size_t l); +int strgrowpad0(char **s, size_t l); + char *strreplace(const char *text, const char *old_string, const char *new_string); char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]);