diff options
author | Karel Zak | 2013-07-02 11:58:58 +0200 |
---|---|---|
committer | Karel Zak | 2013-07-02 11:58:58 +0200 |
commit | 40b278648066eb0a8db7c99c804e06b2f938d37e (patch) | |
tree | bab73d08492d3ecd70765f9723fa844dc3bc6757 | |
parent | libmount: be more restrictive about valid tag names (diff) | |
download | kernel-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>
-rw-r--r-- | libmount/src/mountP.h | 1 | ||||
-rw-r--r-- | libmount/src/utils.c | 38 |
2 files changed, 39 insertions, 0 deletions
diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h index 7b0848f92..c6b70e29f 100644 --- a/libmount/src/mountP.h +++ b/libmount/src/mountP.h @@ -137,6 +137,7 @@ extern int startswith(const char *s, const char *sx) extern char *stripoff_last_component(char *path); extern int mnt_valid_tagname(const char *tagname); +extern int append_string(char **a, const char *b); extern int is_file_empty(const char *name); 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>" }, |