From: Michael Tremer Date: Wed, 31 Aug 2022 15:44:12 +0000 (+0000) Subject: util: Add function to convert paths into absolute X-Git-Tag: 0.9.28~350 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e538e0c7ed067f448bc5d9863ca427d272679a8b;p=pakfire.git util: Add function to convert paths into absolute Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index 97d514ed2..944cd5e6a 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -55,6 +55,7 @@ int __pakfire_unhexlify(unsigned char* dst, const size_t l, const char* src); __pakfire_path_join(dest, sizeof(dest), first, second) int __pakfire_path_join(char* dest, const size_t length, const char* first, const char* second); +const char* pakfire_path_abspath(const char* path); const char* pakfire_path_relpath(const char* root, const char* path); // File stuff diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index 640e02725..a01913dbd 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -86,6 +86,29 @@ int __pakfire_path_join(char* dest, const size_t length, return __pakfire_string_format(dest, length, "%s/%s", first, second); } +const char* pakfire_path_abspath(const char* path) { + static char buffer[PATH_MAX]; + int r; + + // Check input + if (!path) { + errno = EINVAL; + return NULL; + } + + // Return path if already absolute + if (*path == '/') + return path; + + // Otherwise prepend a / + r = pakfire_string_format(buffer, "/%s", path); + if (r) + return NULL; + + // Return a reference to the buffer + return buffer; +} + const char* pakfire_path_relpath(const char* root, const char* path) { if (pakfire_string_startswith(path, root)) return path + strlen(root);