]> git.ozlabs.org Git - ccan/blobdiff - ccan/tal/path/path.c
tal/path: handle weird case of path_join("")
[ccan] / ccan / tal / path / path.c
index e6e94dbe080dca24fcdb5ec02a4701381029647b..c7a134e3c9d9981699f9c95e4ad10fde71b9fa7d 100644 (file)
@@ -1,7 +1,6 @@
 /* Licensed under BSD-MIT - see LICENSE file for details */
 #include <ccan/tal/path/path.h>
 #include <ccan/str/str.h>
-#include <ccan/take/take.h>
 #include <ccan/tal/str/str.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -50,10 +49,10 @@ char *path_join(const tal_t *ctx, const char *base, const char *a)
                goto out;
 
        len = strlen(base);
-       ret = tal_dup(ctx, char, base, len, 1 + strlen(a) + 1);
+       ret = tal_dup_arr(ctx, char, base, len, 1 + strlen(a) + 1);
        if (!ret)
                goto out;
-       if (ret[len-1] != PATH_SEP)
+       if (len != 0 && ret[len-1] != PATH_SEP)
                ret[len++] = PATH_SEP;
        strcpy(ret + len, a);
 
@@ -244,8 +243,10 @@ char *path_rel(const tal_t *ctx, const char *from, const char *to)
 
        /* This frees to if we're supposed to take it. */
        cto = path_canon(tmpctx, to);
-       if (!cto)
+       if (!cto) {
+               ret = NULL;
                goto out;
+       }
 
        /* How much is in common? */
        for (common = i = 0; cfrom[i] && cto[i]; i++) {
@@ -323,7 +324,9 @@ fail_take_to:
                        goto fail;
        }
 
-       ret[len] = '\0';
+       if (ret)
+               ret[len] = '\0';
+
 out:
        if (taken(linkname))
                tal_free(linkname);
@@ -461,7 +464,7 @@ char *path_basename(const tal_t *ctx, const char *path)
 static char *fixed_string(const tal_t *ctx,
                          const char *str, const char *path)
 {
-       char *ret = tal_dup(ctx, char, path, 0, strlen(str)+1);
+       char *ret = tal_dup_arr(ctx, char, path, 0, strlen(str)+1);
        if (ret)
                strcpy(ret, str);
        return ret;