From c0cd4cdf78886040528b16fad084a14165a16384 Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Sat, 11 Jun 2022 10:14:53 +0100 Subject: Add compgen caching. A list of commands is now stored in $XDG_CACHE_HOME/.cache/tofi-compgen, and regenerated as necessary. --- src/mkdirp.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/mkdirp.c (limited to 'src/mkdirp.c') diff --git a/src/mkdirp.c b/src/mkdirp.c new file mode 100644 index 0000000..14bfb2c --- /dev/null +++ b/src/mkdirp.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include "log.h" +#include "mkdirp.h" +#include "xmalloc.h" + +bool mkdirp(const char *path) +{ + struct stat statbuf; + if (stat(path, &statbuf) == 0) { + /* If the file exists, we don't need to do anything. */ + return true; + } + + /* + * Walk down the path, creating directories as we go. + * This works by repeatedly finding the next / in path, then calling + * mkdir() on the string up to that point. + */ + char *tmp = xstrdup(path); + char *cursor = tmp; + while ((cursor = strchr(cursor + 1, '/')) != NULL) { + *cursor = '\0'; + log_debug("Creating directory %s\n", tmp); + if (mkdir(tmp, 0700) != 0 && errno != EEXIST) { + log_error( + "Error creating file path: %s.\n", + strerror(errno)); + free(tmp); + return false; + } + *cursor = '/'; + } + free(tmp); + return true; +} -- cgit v1.2.3