]> git.ipfire.org Git - pakfire.git/commitdiff
util: Add function to convert paths into absolute
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 31 Aug 2022 15:44:12 +0000 (15:44 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 31 Aug 2022 15:44:12 +0000 (15:44 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/util.h
src/libpakfire/util.c

index 97d514ed2b335897c6ca341e53d30f1f207f4896..944cd5e6a6590198307952dc42e53f5c970308c4 100644 (file)
@@ -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
index 640e027258832c7422fda247e58fb532cae03c08..a01913dbdc7705bd0efce37a5d5f9c624aa02635 100644 (file)
@@ -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);