]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/path: don't use extra '/'
authorKarel Zak <kzak@redhat.com>
Thu, 24 May 2018 12:06:11 +0000 (14:06 +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>
lib/path.c

index 11cab3aaf852ced45ce18f1bf5b5558f440713b5..98455accffd2947c3c6e6a0dcc13718e5f0be9d6 100644 (file)
@@ -167,11 +167,18 @@ int ul_path_set_enoent_redirect(struct path_cxt *pc, int (*func)(struct path_cxt
 static const char *get_absdir(struct path_cxt *pc)
 {
        int rc;
+       const char *dirpath;
 
        if (!pc->prefix)
                return pc->dir_path;
 
-       rc = snprintf(pc->path_buffer, sizeof(pc->path_buffer), "%s/%s", pc->prefix, pc->dir_path);
+       dirpath = pc->dir_path;
+       if (!dirpath)
+               return pc->prefix;
+       if (*dirpath == '/')
+               dirpath++;
+
+       rc = snprintf(pc->path_buffer, sizeof(pc->path_buffer), "%s/%s", pc->prefix, dirpath);
        if (rc < 0)
                return NULL;
        if ((size_t)rc >= sizeof(pc->path_buffer)) {
@@ -219,16 +226,21 @@ char *ul_path_get_abspath(struct path_cxt *pc, char *buf, size_t bufsz, const ch
        if (path) {
                int rc;
                va_list ap;
-               const char *tail = NULL;
+               const char *tail = NULL, *dirpath = pc->dir_path;
 
                va_start(ap, path);
                tail = ul_path_mkpath(pc, path, ap);
                va_end(ap);
 
+               if (dirpath && *dirpath == '/')
+                       dirpath++;
+               if (tail && *tail == '/')
+                       tail++;
+
                rc = snprintf(buf, bufsz, "%s/%s/%s",
                                pc->prefix ? pc->prefix : "",
-                               pc->dir_path,
-                               tail);
+                               dirpath ? dirpath : "",
+                               tail ? tail : "");
 
                if ((size_t)rc >= bufsz) {
                        errno = ENAMETOOLONG;