]> Sergey Matveev's repositories - nnn.git/commitdiff
Add xdirname() again and give dirname(3) another chance
authorlostd <lostd@2f30.org>
Wed, 22 Oct 2014 14:02:15 +0000 (17:02 +0300)
committerlostd <lostd@2f30.org>
Wed, 22 Oct 2014 14:02:15 +0000 (17:02 +0300)
noice.c

diff --git a/noice.c b/noice.c
index 7a6d67516b967b2c905b0209c4cd80a773082fbc..e804e686e188a26250e2158a1eadabf79f3d0626 100644 (file)
--- a/noice.c
+++ b/noice.c
@@ -118,6 +118,25 @@ xrealpath(const char *path)
        return p;
 }
 
+char *
+xdirname(const char *path)
+{
+       char *p, *tmp;
+
+       /* Some implementations of dirname(3) may modify `path' and some
+        * return a pointer inside `path` and we cannot free(3) the
+        * original string if we lose track of it. */
+       tmp = xstrdup(path);
+       p = dirname(tmp);
+       free(tmp);
+       if (p == NULL)
+               printerr(1, "dirname");
+
+       /* Make sure this is a malloc(3)-ed string */
+       p = xstrdup(p);
+       return p;
+}
+
 void
 spawn(const char *file, const char *arg)
 {
@@ -532,7 +551,7 @@ nochange:
                        if (strcmp(path, "/") == 0) {
                                goto nochange;
                        } else {
-                               dir = dirname(path);
+                               dir = xdirname(path);
                                tmp = xmalloc(strlen(dir) + 1);
                                strlcpy(tmp, dir, strlen(dir) + 1);
                                free(path);