From 6675cec4599216f3b5942cbd83b48dd41b8c7f01 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Sat, 12 Aug 2000 15:50:50 +0000 Subject: mkpath() - behaves like mkdir -p. Autologging now uses it to create log directories. git-svn-id: http://svn.irssi.org/repos/irssi/trunk@591 dbcabf3a-b0e7-0310-adc4-f8d773084564 --- src/core/misc.c | 33 +++++++++++++++++++++++++++++++++ src/core/misc.h | 3 +++ 2 files changed, 36 insertions(+) (limited to 'src/core') diff --git a/src/core/misc.c b/src/core/misc.c index b1e2521a..e3762e2f 100644 --- a/src/core/misc.c +++ b/src/core/misc.c @@ -337,6 +337,39 @@ int regexp_match(const char *str, const char *regexp) return ret == 0; } +/* Create the directory and all it's parent directories */ +int mkpath(const char *path, int mode) +{ + struct stat statbuf; + const char *p; + char *dir; + + g_return_val_if_fail(path != NULL, -1); + + p = g_path_skip_root((char *) path); + for (;;) { + if (*p != G_DIR_SEPARATOR && *p != '\0') { + p++; + continue; + } + + dir = g_strndup(path, (int) (p-path)); + if (stat(dir, &statbuf) != 0) { + if (mkdir(dir, mode) == -1) { + g_free(dir); + return -1; + } + } + g_free(dir); + + if (*p++ == '\0') + break; + } + + return 0; +} + +/* convert ~/ to $HOME */ char *convert_home(const char *path) { return *path == '~' && (*(path+1) == '/' || *(path+1) == '\0') ? diff --git a/src/core/misc.h b/src/core/misc.h index 08917208..3dc50dc1 100644 --- a/src/core/misc.h +++ b/src/core/misc.h @@ -41,6 +41,9 @@ char *stristr_full(const char *data, const char *key); /* easy way to check if regexp matches */ int regexp_match(const char *str, const char *regexp); +/* Create the directory and all it's parent directories */ +int mkpath(const char *path, int mode); +/* convert ~/ to $HOME */ char *convert_home(const char *path); /* Case-insensitive string hash functions */ -- cgit v1.2.3