Try to create the base directory of the secret key

This commit is contained in:
Frank Denis 2016-08-01 00:11:05 +02:00
parent e8a6730eae
commit 1139d9497c
3 changed files with 84 additions and 12 deletions

View file

@ -3,8 +3,11 @@
# include <sys/types.h>
# include <sys/fcntl.h>
# include <sys/stat.h>
#elif defined(_WIN32)
# include <direct.h>
#endif
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
@ -63,6 +66,17 @@ xmalloc(size_t size)
return pnt;
}
char *
xstrdup(const char *str)
{
char *clone;
if ((clone = strdup(str)) == NULL) {
exit_err("strdup()");
}
return clone;
}
void *
xsodium_malloc(size_t size)
{
@ -182,3 +196,57 @@ fopen_create_useronly(const char *file)
return fopen(file, "w");
#endif
}
int
basedir_create_useronly(const char *file)
{
const char *basename;
char *dir;
int ret = -1;
dir = xstrdup(file);
basename = file_basename(dir);
if (basename != dir) {
dir[basename - dir - 1] = 0;
}
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
if (*dir == 0 || mkdir(dir, 0700) == 0 || errno == EEXIST) {
ret = 0;
}
#elif defined(_WIN32)
if (*dir == 0 || _mkdir(dir) == 0 || errno == EEXIST) {
ret = 0;
}
free(dir);
#endif
return ret;
}
char *get_home_dir(void)
{
char *dir;
#ifdef _WIN32
const char *hd
const char *hp;
#endif
if ((dir = getenv("HOME")) != NULL) {
return xstrdup(dir);
}
#ifdef _WIN32
if ((dir = getenv("USERPROFILE")) != NULL) {
return xstrdup(dir);
}
if ((dir = getenv("USERPROFILE")) != NULL) {
return xstrdup(dir);
}
if ((hd = getenv("HOMEDRIVE")) != NULL &&
(hp = getenv("HOMEPATH")) != NULL) {
if (asprintf(&dir, "%s%s", hd, hp) < 0) {
exit_err("asprintf()");
}
return dir;
}
#endif
return NULL;
}