summaryrefslogtreecommitdiffstats
path: root/libmount/src/utils.c
diff options
context:
space:
mode:
authorKarel Zak2013-07-02 11:58:58 +0200
committerKarel Zak2013-07-02 11:58:58 +0200
commit40b278648066eb0a8db7c99c804e06b2f938d37e (patch)
treebab73d08492d3ecd70765f9723fa844dc3bc6757 /libmount/src/utils.c
parentlibmount: be more restrictive about valid tag names (diff)
downloadkernel-qcow2-util-linux-40b278648066eb0a8db7c99c804e06b2f938d37e.tar.gz
kernel-qcow2-util-linux-40b278648066eb0a8db7c99c804e06b2f938d37e.tar.xz
kernel-qcow2-util-linux-40b278648066eb0a8db7c99c804e06b2f938d37e.zip
libmount: add a generic append_string() function
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libmount/src/utils.c')
-rw-r--r--libmount/src/utils.c38
1 files changed, 38 insertions, 0 deletions
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, "[<pattern>] list /{etc,proc}/filesystems" },
{ "--starts-with", test_startswith, "<string> <prefix>" },
{ "--ends-with", test_endswith, "<string> <prefix>" },
+ { "--append-string", test_appendstr, "<string> <appendix>" },
{ "--mountpoint", test_mountpoint, "<path>" },
{ "--fs-root", test_fsroot, "<path>" },
{ "--cd-parent", test_chdir, "<path>" },