]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/path: add ul_path_get_abspath()
authorKarel Zak <kzak@redhat.com>
Thu, 17 May 2018 11:58:30 +0000 (13:58 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 21 Jun 2018 11:07:46 +0000 (13:07 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/path.h
lib/path.c

index 8b7d568e7f0d45346dca915c75629d16d534b24e..ebdb58d05ddf2d6c7905bd9109a86ab96de0e27f 100644 (file)
@@ -39,6 +39,9 @@ void *ul_path_get_dialect(struct path_cxt *pc);
 int ul_path_set_enoent_redirect(struct path_cxt *pc, int (*func)(struct path_cxt *, const char *, int *));
 int ul_path_get_dirfd(struct path_cxt *pc);
 
+char *ul_path_get_abspath(struct path_cxt *pc, char *buf, size_t bufsz, const char *path, ...)
+                               __attribute__ ((__format__ (__printf__, 4, 5)));
+
 int ul_path_access(struct path_cxt *pc, int mode, const char *path);
 int ul_path_accessf(struct path_cxt *pc, int mode, const char *path, ...)
                                __attribute__ ((__format__ (__printf__, 3, 4)));
index b1d0be9d1b66a3608277c1098c297816bd08f965..63f19af9a5cf8ebcab863957cde81b489915e6be 100644 (file)
@@ -176,7 +176,6 @@ static const char *get_absdir(struct path_cxt *pc)
        return pc->path_buffer;
 }
 
-
 int ul_path_get_dirfd(struct path_cxt *pc)
 {
        assert(pc);
@@ -209,6 +208,39 @@ static const char *ul_path_mkpath(struct path_cxt *pc, const char *path, va_list
        return pc->path_buffer;
 }
 
+char *ul_path_get_abspath(struct path_cxt *pc, char *buf, size_t bufsz, const char *path, ...)
+{
+       if (path) {
+               int rc;
+               va_list ap;
+               const char *tail = NULL;
+
+               va_start(ap, path);
+               tail = ul_path_mkpath(pc, path, ap);
+               va_end(ap);
+
+               rc = snprintf(buf, bufsz, "%s/%s/%s",
+                               pc->prefix ? pc->prefix : "",
+                               pc->dir_path,
+                               tail);
+
+               if ((size_t)rc >= bufsz) {
+                       errno = ENAMETOOLONG;
+                       return NULL;
+               }
+       } else {
+               const char *tmp = get_absdir(pc);
+
+               if (!tmp)
+                       return NULL;
+               strncpy(buf, tmp, bufsz);
+               buf[bufsz - 1] = '\0';
+       }
+
+       return buf;
+}
+
+
 int ul_path_access(struct path_cxt *pc, int mode, const char *path)
 {
        int dir, rc;