2014-02-25 08:28:24 +01:00
|
|
|
/* configdir.c
|
2013-08-08 10:15:07 +02:00
|
|
|
*
|
|
|
|
*
|
2014-02-25 08:28:24 +01:00
|
|
|
* Copyright (C) 2014 Toxic All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Toxic.
|
|
|
|
*
|
|
|
|
* Toxic is free software: you can redistribute it and/or modify
|
2013-08-08 10:15:07 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2014-02-25 08:28:24 +01:00
|
|
|
* Toxic is distributed in the hope that it will be useful,
|
2013-08-08 10:15:07 +02:00
|
|
|
* 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
|
2014-02-25 08:28:24 +01:00
|
|
|
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
|
2013-08-08 10:15:07 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
#include <unistd.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
|
2014-08-03 03:01:40 +02:00
|
|
|
#include "toxic.h"
|
2013-08-08 16:00:12 +02:00
|
|
|
#include "configdir.h"
|
2015-11-08 09:57:01 +01:00
|
|
|
#include "misc_tools.h"
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2014-07-21 21:06:56 +02:00
|
|
|
/* get the user's home directory */
|
|
|
|
void get_home_dir(char *home, int size)
|
2013-08-08 10:15:07 +02:00
|
|
|
{
|
2013-08-11 12:52:56 +02:00
|
|
|
struct passwd pwd;
|
|
|
|
struct passwd *pwdbuf;
|
2014-07-21 21:06:56 +02:00
|
|
|
const char *hmstr;
|
2013-08-11 12:52:56 +02:00
|
|
|
char buf[NSS_BUFLEN_PASSWD];
|
|
|
|
|
2014-08-03 03:01:40 +02:00
|
|
|
int 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) {
|
2014-07-21 21:06:56 +02:00
|
|
|
hmstr = pwd.pw_dir;
|
2013-08-11 12:52:56 +02:00
|
|
|
} else {
|
2014-07-21 21:06:56 +02:00
|
|
|
hmstr = getenv("HOME");
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2014-07-21 21:06:56 +02:00
|
|
|
if (hmstr == NULL)
|
|
|
|
return;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2014-07-21 21:06:56 +02:00
|
|
|
snprintf(buf, sizeof(buf), "%s", hmstr);
|
|
|
|
hmstr = buf;
|
2013-08-11 12:52:56 +02:00
|
|
|
}
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2014-07-21 21:06:56 +02:00
|
|
|
snprintf(home, size, "%s", hmstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the user's config directory.
|
|
|
|
*
|
|
|
|
* This is without a trailing slash. Resulting string must be freed.
|
|
|
|
*
|
|
|
|
* @return The users config dir or NULL on error.
|
|
|
|
*/
|
|
|
|
char *get_user_config_dir(void)
|
|
|
|
{
|
2015-01-25 21:53:13 +01:00
|
|
|
char home[NSS_BUFLEN_PASSWD] = {0};
|
2014-07-21 21:06:56 +02:00
|
|
|
get_home_dir(home, sizeof(home));
|
|
|
|
|
2015-11-08 09:57:01 +01:00
|
|
|
char *user_config_dir = NULL;
|
|
|
|
size_t len = 0;
|
2014-07-21 21:06:56 +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
|
|
|
|
2014-07-21 21:06:56 +02:00
|
|
|
if (user_config_dir == NULL)
|
2013-08-11 12:52:56 +02:00
|
|
|
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
|
|
|
|
2015-11-08 09:57:01 +01:00
|
|
|
const char *tmp = getenv("XDG_CONFIG_HOME");
|
2014-04-19 23:58:13 +02:00
|
|
|
|
2015-11-08 09:57:01 +01:00
|
|
|
if (tmp == NULL) {
|
2013-08-19 01:21:36 +02:00
|
|
|
len = strlen(home) + strlen("/.config") + 1;
|
|
|
|
user_config_dir = malloc(len);
|
|
|
|
|
2014-07-21 21:06:56 +02:00
|
|
|
if (user_config_dir == NULL)
|
2013-08-19 01:21:36 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
snprintf(user_config_dir, len, "%s/.config", home);
|
2013-08-29 17:06:05 +02:00
|
|
|
} else {
|
|
|
|
user_config_dir = strdup(tmp);
|
2013-08-08 16:00:12 +02:00
|
|
|
}
|
|
|
|
|
2013-08-11 12:52:56 +02:00
|
|
|
# endif /* __APPLE__ */
|
|
|
|
|
|
|
|
return user_config_dir;
|
2013-08-08 16:00:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-08-03 03:01:40 +02:00
|
|
|
* Creates the config and chatlog directories.
|
2013-08-08 16:00:12 +02:00
|
|
|
*/
|
2014-08-03 03:01:40 +02:00
|
|
|
int create_user_config_dirs(char *path)
|
2013-08-16 19:11:09 +02:00
|
|
|
{
|
|
|
|
struct stat buf;
|
2014-08-03 03:01:40 +02:00
|
|
|
int mkdir_err = mkdir(path, 0700);
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2014-08-03 03:01:40 +02:00
|
|
|
if (mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode)))
|
2013-08-16 19:11:09 +02:00
|
|
|
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);
|
2014-08-03 03:01:40 +02:00
|
|
|
char *logpath = malloc(strlen(path) + strlen(LOGDIR) + 1);
|
|
|
|
|
|
|
|
if (fullpath == NULL || logpath == NULL)
|
2015-07-04 07:19:16 +02:00
|
|
|
exit_toxic_err("failed in load_data_structures", FATALERR_MEMORY);
|
2014-08-03 03:01:40 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
strcpy(fullpath, path);
|
|
|
|
strcat(fullpath, CONFIGDIR);
|
|
|
|
|
2014-08-03 03:01:40 +02:00
|
|
|
strcpy(logpath, path);
|
|
|
|
strcat(logpath, LOGDIR);
|
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
mkdir_err = mkdir(fullpath, 0700);
|
|
|
|
|
|
|
|
if (mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
|
|
|
|
free(fullpath);
|
2014-08-03 03:01:40 +02:00
|
|
|
free(logpath);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir_err = mkdir(logpath, 0700);
|
|
|
|
|
|
|
|
if (mkdir_err && (errno != EEXIST || stat(logpath, &buf) || !S_ISDIR(buf.st_mode))) {
|
|
|
|
free(fullpath);
|
|
|
|
free(logpath);
|
2013-08-16 19:11:09 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2014-08-03 03:01:40 +02:00
|
|
|
free(logpath);
|
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
|
|
|
}
|