From 24b514827d8fb6a11521da3f29b80b25c488e8c6 Mon Sep 17 00:00:00 2001 From: Christian Seiler Date: Thu, 12 Sep 2013 01:44:41 +0200 Subject: [PATCH] utils: Add lxc_append_paths to join two paths. Signed-off-by: Christian Seiler Signed-off-by: Serge Hallyn --- src/lxc/utils.c | 19 +++++++++++++++++++ src/lxc/utils.h | 1 + 2 files changed, 20 insertions(+) diff --git a/src/lxc/utils.c b/src/lxc/utils.c index 2e665858f..78b234d98 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -658,6 +658,25 @@ char **lxc_normalize_path(const char *path) return components; } +char *lxc_append_paths(const char *first, const char *second) +{ + size_t len = strlen(first) + strlen(second) + 1; + const char *pattern = "%s%s"; + char *result = NULL; + + if (second[0] != '/') { + len += 1; + pattern = "%s/%s"; + } + + result = calloc(1, len); + if (!result) + return NULL; + + snprintf(result, len, pattern, first, second); + return result; +} + bool lxc_string_in_list(const char *needle, const char *haystack, char _sep) { char *token, *str, *saveptr = NULL; diff --git a/src/lxc/utils.h b/src/lxc/utils.h index 9776d188c..ba7cfb3ef 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -219,6 +219,7 @@ extern char *lxc_string_join(const char *sep, const char **parts, bool use_as_pr * foo//bar -> { foo, bar, NULL } */ extern char **lxc_normalize_path(const char *path); +extern char *lxc_append_paths(const char *first, const char *second); /* Note: the following two functions use strtok(), so they will never * consider an empty element, even if two delimiters are next to * each other.