aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gmid.c35
-rw-r--r--gmid.h1
2 files changed, 26 insertions, 10 deletions
diff --git a/gmid.c b/gmid.c
index 2467a1b..1204596 100644
--- a/gmid.c
+++ b/gmid.c
@@ -20,6 +20,7 @@
#include <errno.h>
#include <fcntl.h>
+#include <libgen.h>
#include <limits.h>
#include <pwd.h>
#include <signal.h>
@@ -44,11 +45,28 @@ dummy_handler(int signo)
return;
}
-/* XXX: create recursively */
-void
-mkdirs(const char *path)
+/* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
+static void
+pdirname(const char *path, char *dn)
+{
+ char p[PATH_MAX+1];
+ char *t;
+
+ strlcpy(p, path, sizeof(p));
+ t = dirname(p);
+ memmove(dn, t, strlen(t)+1);
+}
+
+static void
+mkdirs(const char *path, mode_t mode)
{
- if (mkdir(path, 0755) == -1 && errno != EEXIST)
+ char dname[PATH_MAX+1];
+
+ pdirname(path, dname);
+ if (!strcmp(dname, "/"))
+ return;
+ mkdirs(dname, mode);
+ if (mkdir(path, mode) != 0 && errno != EEXIST)
fatal("can't mkdir %s: %s", path, strerror(errno));
}
@@ -64,13 +82,12 @@ data_dir(void)
errx(1, "XDG_DATA_HOME and HOME both empty");
if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
err(1, "asprintf");
- mkdirs(t);
- return t;
+ } else {
+ if (asprintf(&t, "%s/gmid", xdg) == -1)
+ err(1, "asprintf");
}
- if (asprintf(&t, "%s/gmid", xdg) == -1)
- err(1, "asprintf");
- mkdirs(t);
+ mkdirs(t, 0755);
return t;
}
diff --git a/gmid.h b/gmid.h
index 8188bf9..17a1315 100644
--- a/gmid.h
+++ b/gmid.h
@@ -230,7 +230,6 @@ enum imsg_type {
};
/* gmid.c */
-void mkdirs(const char*);
char *data_dir(void);
void load_local_cert(const char*, const char*);
void load_vhosts(void);