2013-08-08 10:15:07 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Tox project All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Tox.
|
|
|
|
*
|
|
|
|
* Tox is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tox is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2013-08-08 16:00:12 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
2013-08-08 10:15:07 +02:00
|
|
|
|
2013-08-08 16:00:12 +02:00
|
|
|
#ifdef WIN32
|
2013-08-08 10:15:07 +02:00
|
|
|
#include <shlobj.h>
|
2013-08-08 16:00:12 +02:00
|
|
|
#include <direct.h>
|
2013-08-11 12:52:56 +02:00
|
|
|
#else /* WIN32 */
|
2013-08-08 10:15:07 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <pwd.h>
|
2013-08-11 12:52:56 +02:00
|
|
|
#endif /* WIN32 */
|
2013-08-08 10:15:07 +02:00
|
|
|
|
2013-08-08 16:00:12 +02:00
|
|
|
#include "configdir.h"
|
|
|
|
|
2013-08-11 12:52:56 +02:00
|
|
|
/**
|
|
|
|
* @brief Get the users config directory.
|
|
|
|
*
|
|
|
|
* This is without a trailing slash.
|
|
|
|
*
|
|
|
|
* @return The users config dir or NULL on error.
|
2013-08-08 16:00:12 +02:00
|
|
|
*/
|
2013-08-08 10:15:07 +02:00
|
|
|
char *get_user_config_dir(void)
|
|
|
|
{
|
2013-08-11 12:52:56 +02:00
|
|
|
char *user_config_dir;
|
|
|
|
#ifdef WIN32
|
|
|
|
char appdata[MAX_PATH];
|
|
|
|
BOOL ok;
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2013-08-11 12:52:56 +02:00
|
|
|
ok = SHGetSpecialFolderPathA(NULL, appdata, CSIDL_PROFILE, TRUE);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-11 12:52:56 +02:00
|
|
|
if (!ok) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2013-08-11 12:52:56 +02:00
|
|
|
user_config_dir = strdup(appdata);
|
|
|
|
|
|
|
|
return user_config_dir;
|
|
|
|
|
|
|
|
#else /* WIN32 */
|
|
|
|
|
|
|
|
#ifndef NSS_BUFLEN_PASSWD
|
|
|
|
#define NSS_BUFLEN_PASSWD 4096
|
|
|
|
#endif /* NSS_BUFLEN_PASSWD */
|
|
|
|
|
|
|
|
struct passwd pwd;
|
|
|
|
struct passwd *pwdbuf;
|
|
|
|
const char *home;
|
|
|
|
char buf[NSS_BUFLEN_PASSWD];
|
|
|
|
size_t len;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-11 12:52:56 +02:00
|
|
|
if (rc == 0) {
|
|
|
|
home = pwd.pw_dir;
|
|
|
|
} else {
|
|
|
|
home = getenv("HOME");
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-11 12:52:56 +02:00
|
|
|
if (home == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-11 12:52:56 +02:00
|
|
|
/* env variables can be tainted */
|
|
|
|
snprintf(buf, sizeof(buf), "%s", home);
|
|
|
|
home = buf;
|
|
|
|
}
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2013-08-11 12:52:56 +02:00
|
|
|
# if defined(__APPLE__)
|
|
|
|
len = strlen(home) + strlen("/Library/Application Support") + 1;
|
|
|
|
user_config_dir = malloc(len);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-11 12:52:56 +02:00
|
|
|
if (user_config_dir == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2013-08-11 12:52:56 +02:00
|
|
|
snprintf(user_config_dir, len, "%s/Library/Application Support", home);
|
|
|
|
# else /* __APPLE__ */
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-19 01:21:36 +02:00
|
|
|
if (!(user_config_dir = getenv("XDG_CONFIG_HOME"))) {
|
|
|
|
len = strlen(home) + strlen("/.config") + 1;
|
|
|
|
user_config_dir = malloc(len);
|
|
|
|
|
|
|
|
if (user_config_dir == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(user_config_dir, len, "%s/.config", home);
|
2013-08-08 16:00:12 +02:00
|
|
|
}
|
|
|
|
|
2013-08-11 12:52:56 +02:00
|
|
|
# endif /* __APPLE__ */
|
|
|
|
|
|
|
|
return user_config_dir;
|
|
|
|
#undef NSS_BUFLEN_PASSWD
|
|
|
|
#endif /* WIN32 */
|
2013-08-08 16:00:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Creates the config directory.
|
|
|
|
*/
|
|
|
|
int create_user_config_dir(char *path)
|
2013-08-16 19:11:09 +02:00
|
|
|
{
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
int mkdir_err;
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
#ifdef WIN32
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
|
|
|
|
strcpy(fullpath, path);
|
|
|
|
strcat(fullpath, CONFIGDIR);
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
mkdir_err = _mkdir(fullpath);
|
|
|
|
struct __stat64 buf;
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
if (mkdir_err && (errno != EEXIST || _wstat64(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
|
|
|
|
free(fullpath);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
#else
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
mkdir_err = mkdir(path, 0700);
|
|
|
|
struct stat buf;
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
if (mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
|
|
|
|
strcpy(fullpath, path);
|
|
|
|
strcat(fullpath, CONFIGDIR);
|
|
|
|
|
|
|
|
mkdir_err = mkdir(fullpath, 0700);
|
|
|
|
|
|
|
|
if (mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
|
|
|
|
free(fullpath);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
#endif
|
2013-08-08 16:36:16 +02:00
|
|
|
free(fullpath);
|
2013-08-16 19:11:09 +02:00
|
|
|
return 0;
|
2013-08-08 16:36:16 +02:00
|
|
|
}
|