Lephisto/src/lfile.c

49 lines
950 B
C

#include <string.h>
#ifdef LINUX
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#endif
#include "lephisto.h"
#include "log.h"
#include "lfile.h"
// Return lephisto's base path.
static char lephisto_base[128] = "\0";
char* lfile_basePath(void) {
char* home;
if(lephisto_base[0] == '\0') {
#ifdef LINUX
home = getenv("HOME");
#endif
snprintf(lephisto_base, PATH_MAX, "%s/.lephisto/", home);
}
return lephisto_base;
}
// Check if a directory exists, and create it if it doesn't.
// Based on lephisto_base.
int lfile_dirMakeExist(char* path) {
char file[PATH_MAX];
#ifdef LINUX
struct stat buf;
snprintf(file, PATH_MAX, "%s%s", lfile_basePath(), path);
stat(file, &buf);
if(!S_ISDIR(buf.st_mode))
if(mkdir(file, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
WARN("Dir '%s' does not exist and unable to create", path);
return -1;
}
#endif
return 0;
}