1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 14:43:02 +01:00
toxic/src/configdir.c

147 lines
3.5 KiB
C
Raw Normal View History

2014-02-25 08:28:24 +01:00
/* configdir.c
*
*
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
* 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,
* 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/>.
*
*/
#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>
#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"
2014-07-21 21:06:56 +02:00
/* get the user's home directory */
void get_home_dir(char *home, int size)
{
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)
{
char home[NSS_BUFLEN_PASSWD];
get_home_dir(home, sizeof(home));
char *user_config_dir;
size_t len;
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
const char *tmp;
if (!(tmp = getenv("XDG_CONFIG_HOME"))) {
len = strlen(home) + strlen("/.config") + 1;
user_config_dir = malloc(len);
2014-07-21 21:06:56 +02:00
if (user_config_dir == NULL)
return NULL;
snprintf(user_config_dir, len, "%s/.config", home);
} 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)
exit_toxic_err("failed in load_data_structures", FATALERR_MEMORY);
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);
free(fullpath);
2013-08-16 19:11:09 +02:00
return 0;
}