From 40b278648066eb0a8db7c99c804e06b2f938d37e Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 2 Jul 2013 11:58:58 +0200 Subject: libmount: add a generic append_string() function Signed-off-by: Karel Zak --- libmount/src/utils.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'libmount/src/utils.c') diff --git a/libmount/src/utils.c b/libmount/src/utils.c index 6c5171eca..165576464 100644 --- a/libmount/src/utils.c +++ b/libmount/src/utils.c @@ -54,6 +54,31 @@ int startswith(const char *s, const char *sx) return !strncmp(s, sx, off); } +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 = b ? strlen(b) : 0; + + 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 does not accessible of empty */ @@ -1082,6 +1107,18 @@ int test_endswith(struct libmnt_test *ts, int argc, char *argv[]) return 0; } +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; +} + int test_mountpoint(struct libmnt_test *ts, int argc, char *argv[]) { char *path = canonicalize_path(argv[1]), @@ -1177,6 +1214,7 @@ 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, "" }, { "--fs-root", test_fsroot, "" }, { "--cd-parent", test_chdir, "" }, -- cgit v1.2.3-55-g7522