mirror of
https://github.com/nunuhara/xsystem4.git
synced 2026-10-07 22:38:11 +03:00
Create save folder on startup
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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[])
|
||||
|
||||
Reference in New Issue
Block a user