From df4656e929af218de016a11e8544aec50f28d294 Mon Sep 17 00:00:00 2001 From: Nunuhara Cabbage Date: Sun, 22 Dec 2019 14:23:43 -0800 Subject: [PATCH] Create save folder on startup --- file.c | 43 +++++++++++++++++++++++++++++++++++++++++++ file.h | 1 + system4.c | 2 ++ 3 files changed, 46 insertions(+) diff --git a/file.c b/file.c index 901c6f9..b2097fa 100644 --- a/file.c +++ b/file.c @@ -18,7 +18,11 @@ #include #include #include +#include +#include +#include #include +#include #include "system4.h" void *file_read(const char *path, size_t *len_out) @@ -55,3 +59,42 @@ bool file_exists(const char *path) { return access(path, F_OK) != -1; } + +// Adapted from http://stackoverflow.com/a/2336245/119527 +int mkdir_p(const char *path) +{ + const size_t len = strlen(path); + char _path[PATH_MAX]; + char *p; + + errno = 0; + + // Copy string so its mutable + if (len > sizeof(_path)-1) { + errno = ENAMETOOLONG; + return -1; + } + strcpy(_path, path); + + // Iterate the string + for (p = _path + 1; *p; p++) { + if (*p == '/') { + // Temporarily truncate + *p = '\0'; + + if (mkdir(_path, S_IRWXU) != 0) { + if (errno != EEXIST) + return -1; + } + + *p = '/'; + } + } + + if (mkdir(_path, S_IRWXU) != 0) { + if (errno != EEXIST) + return -1; + } + + return 0; +} diff --git a/file.h b/file.h index 6898c93..7a95ab8 100644 --- a/file.h +++ b/file.h @@ -22,5 +22,6 @@ void *file_read(const char *path, size_t *len_out); bool file_exists(const char *path); +int mkdir_p(const char *path); #endif /* SYSTEM4_FILE_H */ diff --git a/system4.c b/system4.c index a5115d0..527be0d 100644 --- a/system4.c +++ b/system4.c @@ -24,6 +24,7 @@ #include "ain.h" #include "ald.h" #include "debugger.h" +#include "file.h" #include "ini.h" #include "utfsjis.h" @@ -189,6 +190,7 @@ static void config_init(void) char *new_save_dir = get_save_path(config.save_dir); free(config.save_dir); config.save_dir = new_save_dir; + mkdir_p(new_save_dir); } int main(int argc, char *argv[])