From 2e03758dc56ccfdec4ca3401136d2701a219b943 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 5 Aug 2021 15:42:15 +0200 Subject: [PATCH] lib/strutils: add strappend() Signed-off-by: Karel Zak --- include/strutils.h | 3 +++ lib/strutils.c | 25 +++++++++++++++++++++++++ libmount/src/fs.c | 2 +- libmount/src/mountP.h | 1 - libmount/src/tab.c | 4 ++-- libmount/src/utils.c | 38 -------------------------------------- 6 files changed, 31 insertions(+), 42 deletions(-) diff --git a/include/strutils.h b/include/strutils.h index e7edd852e3..f671b3ba69 100644 --- a/include/strutils.h +++ b/include/strutils.h @@ -382,6 +382,9 @@ extern char *strnconcat(const char *s, const char *suffix, size_t b); extern char *strconcat(const char *s, const char *suffix); extern char *strfconcat(const char *s, const char *format, ...) __attribute__ ((__format__ (__printf__, 2, 3))); + +extern int strappend(char **a, const char *b); + extern const char *split(const char **state, size_t *l, const char *separator, int quoted); extern int skip_fline(FILE *fp); diff --git a/lib/strutils.c b/lib/strutils.c index 794203abd1..3f29654fe6 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -992,6 +992,31 @@ char *strfconcat(const char *s, const char *format, ...) return res; } +int strappend(char **a, const char *b) +{ + size_t al, bl; + char *tmp; + + if (!a) + return -EINVAL; + if (!b || !*b) + return 0; + if (!*a) { + *a = strdup(b); + return !*a ? -ENOMEM : 0; + } + + al = strlen(*a); + bl = strlen(b); + + tmp = realloc(*a, al + bl + 1); + if (!tmp) + return -ENOMEM; + *a = tmp; + memcpy((*a) + al, b, bl + 1); + return 0; +} + static size_t strcspn_escaped(const char *s, const char *reject) { int escaped = 0; diff --git a/libmount/src/fs.c b/libmount/src/fs.c index 8b739d6b34..b44d310ee2 100644 --- a/libmount/src/fs.c +++ b/libmount/src/fs.c @@ -1369,7 +1369,7 @@ int mnt_fs_append_comment(struct libmnt_fs *fs, const char *comm) if (!fs) return -EINVAL; - return append_string(&fs->comment, comm); + return strappend(&fs->comment, comm); } /** diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h index a34c953baf..b98d28875a 100644 --- a/libmount/src/mountP.h +++ b/libmount/src/mountP.h @@ -95,7 +95,6 @@ extern int mnt_run_test(struct libmnt_test *tests, int argc, char *argv[]); /* utils.c */ extern int mnt_valid_tagname(const char *tagname); -extern int append_string(char **a, const char *b); extern const char *mnt_statfs_get_fstype(struct statfs *vfs); extern int is_file_empty(const char *name); diff --git a/libmount/src/tab.c b/libmount/src/tab.c index 5fd4608646..110560176e 100644 --- a/libmount/src/tab.c +++ b/libmount/src/tab.c @@ -309,7 +309,7 @@ int mnt_table_append_intro_comment(struct libmnt_table *tb, const char *comm) { if (!tb) return -EINVAL; - return append_string(&tb->comm_intro, comm); + return strappend(&tb->comm_intro, comm); } /** @@ -350,7 +350,7 @@ int mnt_table_append_trailing_comment(struct libmnt_table *tb, const char *comm) { if (!tb) return -EINVAL; - return append_string(&tb->comm_tail, comm); + return strappend(&tb->comm_tail, comm); } /** diff --git a/libmount/src/utils.c b/libmount/src/utils.c index 35afce32c0..c321a98811 100644 --- a/libmount/src/utils.c +++ b/libmount/src/utils.c @@ -32,31 +32,6 @@ #include "statfs_magic.h" #include "sysfs.h" -int append_string(char **a, const char *b) -{ - size_t al, bl; - char *tmp; - - assert(a); - - if (!b || !*b) - return 0; - if (!*a) { - *a = strdup(b); - return !*a ? -ENOMEM : 0; - } - - al = strlen(*a); - bl = strlen(b); - - tmp = realloc(*a, al + bl + 1); - if (!tmp) - return -ENOMEM; - *a = tmp; - memcpy((*a) + al, b, bl + 1); - return 0; -} - /* * Return 1 if the file is not accessible or empty */ @@ -1205,18 +1180,6 @@ static int test_endswith(struct libmnt_test *ts, int argc, char *argv[]) return 0; } -static int test_appendstr(struct libmnt_test *ts, int argc, char *argv[]) -{ - char *str = strdup(argv[1]); - const char *ap = argv[2]; - - append_string(&str, ap); - printf("new string: '%s'\n", str); - - free(str); - return 0; -} - static int test_mountpoint(struct libmnt_test *ts, int argc, char *argv[]) { char *path = canonicalize_path(argv[1]), @@ -1343,7 +1306,6 @@ int main(int argc, char *argv[]) { "--filesystems", test_filesystems, "[] list /{etc,proc}/filesystems" }, { "--starts-with", test_startswith, " " }, { "--ends-with", test_endswith, " " }, - { "--append-string", test_appendstr, " " }, { "--mountpoint", test_mountpoint, "" }, { "--cd-parent", test_chdir, "" }, { "--kernel-cmdline",test_kernel_cmdline, "