From 1713bd21e6a8005734d0086a1b9eab2812939ba5 Mon Sep 17 00:00:00 2001 From: "Anthony G. Basile" Date: Wed, 27 May 2015 09:38:31 -0400 Subject: src/shared/path-util.c: import prefix_root() from upstream Signed-off-by: Anthony G. Basile --- src/shared/path-util.c | 34 ++++++++++++++++++++++++++++++++++ src/shared/path-util.h | 1 + 2 files changed, 35 insertions(+) (limited to 'src') diff --git a/src/shared/path-util.c b/src/shared/path-util.c index 665b171263..0744563976 100644 --- a/src/shared/path-util.c +++ b/src/shared/path-util.c @@ -539,3 +539,37 @@ bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool upd return changed; } + +char *prefix_root(const char *root, const char *path) { + char *n, *p; + size_t l; + + /* If root is passed, prefixes path with it. Otherwise returns + * it as is. */ + + assert(path); + + /* First, drop duplicate prefixing slashes from the path */ + while (path[0] == '/' && path[1] == '/') + path++; + + if (isempty(root) || path_equal(root, "/")) + return strdup(path); + + l = strlen(root) + 1 + strlen(path) + 1; + + n = new(char, l); + if (!n) + return NULL; + + p = stpcpy(n, root); + + while (p > n && p[-1] == '/') + p--; + + if (path[0] != '/') + *(p++) = '/'; + + strcpy(p, path); + return n; +} diff --git a/src/shared/path-util.h b/src/shared/path-util.h index 56d1c52ae2..0123c2609c 100644 --- a/src/shared/path-util.h +++ b/src/shared/path-util.h @@ -38,3 +38,4 @@ int fd_is_mount_point(int fd); int path_is_mount_point(const char *path, bool allow_symlink); bool paths_check_timestamp(const char* const* paths, usec_t *paths_ts_usec, bool update); +char *prefix_root(const char *root, const char *path); -- cgit v1.2.3-54-g00ecf