From deb76abd8d91057302a2a2df27a92c76bad44b24 Mon Sep 17 00:00:00 2001 From: Allanis Date: Mon, 10 Mar 2014 22:56:34 +0000 Subject: [PATCH] [Add] Create ~/.lephisto/conf if not found. Though, leave it empty. --- src/conf.c | 3 ++- src/lfile.c | 29 +++++++++++++++++++++++++++++ src/lfile.h | 2 ++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/conf.c b/src/conf.c index afb185c..9c15f4d 100644 --- a/src/conf.c +++ b/src/conf.c @@ -283,8 +283,9 @@ int conf_loadConfig(const char* file) { } } else { /* Failed to load the config file.. */ - DEBUG("Config file '%s' not found.", file); lua_close(L); + DEBUG("Config file '%s' not found.", file); + lfile_touch(file); return 1; } lua_close(L); diff --git a/src/lfile.c b/src/lfile.c index 3dec4bb..d3ac36f 100644 --- a/src/lfile.c +++ b/src/lfile.c @@ -244,3 +244,32 @@ char** lfile_readDir(int* lfiles, const char* path, ...) { return files; } +/** + * @brief Tries to create the file if it doesn't exist. + * @param path Path of the file to create. + */ +int lfile_touch(const char* path, ...) { + char file[PATH_MAX]; + va_list ap; + size_t l; + FILE* f; + + l = 0; + if(path == NULL) return -1; + else { /* Get the message. */ + va_start(ap, path); + vsnprintf(file, PATH_MAX-l, path, ap); + l = strlen(file); + va_end(ap); + } + + /* Try to open the file, C89 cmpliant, but not as precise as stat. */ + f = fopen(file, "a+"); + if(f == NULL) { + WARN("Unable to touch file '%s': %s", file, strerror(errno)); + return -1; + } + fclose(f); + return 0; +} + diff --git a/src/lfile.h b/src/lfile.h index 287a46e..1f9026c 100644 --- a/src/lfile.h +++ b/src/lfile.h @@ -10,3 +10,5 @@ int lfile_fileExists(const char* path, ...); char** lfile_readDir(int* lfiles, const char* path, ...); +int lfile_touch(const char* path, ...); +