Create save folder on startup

This commit is contained in:
Nunuhara Cabbage
2019-12-22 14:24:09 -08:00
parent d7ca25ade8
commit df4656e929
3 changed files with 46 additions and 0 deletions
+43
View File
@@ -18,7 +18,11 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>
#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;
}
+1
View File
@@ -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 */
+2
View File
@@ -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[])