1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-22 22:13:02 +01:00

Implement proper config directories.

This commit is contained in:
Simon Levermann 2013-08-08 16:00:12 +02:00
parent c7463f0de2
commit c3c144ddd0
4 changed files with 135 additions and 65 deletions

View File

@ -7,7 +7,8 @@ add_executable(${exe_name}
main.c main.c
prompt.c prompt.c
friendlist.c friendlist.c
chat.c) chat.c
configdir.c)
target_link_libraries(${exe_name} target_link_libraries(${exe_name}
curses) curses)

View File

@ -21,71 +21,126 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#ifdef _win32 #ifdef WIN32
#include <shlobj.h> #include <shlobj.h>
#include <direct.h>
#endif #endif
#ifdef __APPLE__ #ifdef __APPLE__
#include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <pwd.h> #include <pwd.h>
#endif #endif
#include "configdir.h"
/*
* Retrieves a correct configuration directory, depending on the OS used, with a trailing slash
*/
char *get_user_config_dir(void) char *get_user_config_dir(void)
{ {
char *user_config_dir; char *user_config_dir;
#ifdef _win32 #ifdef WIN32
char appdata[MAX_PATH]; char appdata[MAX_PATH];
HRESULT result = SHGetFolderPath( HRESULT result = SHGetFolderPath(
NULL, NULL,
CSIDL_APPDATA, // TODO: Maybe use CSIDL_LOCAL_APPDATA instead? Not sure. CSIDL_APPDATA,
NULL, NULL,
SHGFP_TYPE_CURRENT, SHGFP_TYPE_CURRENT,
appdata appdata
) )
if (!result) return NULL; if (!result) return NULL;
user_config_dir = malloc(strlen(appdata) + strlen(CONFIGDIR) + 1); user_config_dir = malloc(strlen(appdata) + 1);
if (user_config_dir) { if (user_config_dir) {
strcpy(user_config_dir, appdata); strcpy(user_config_dir, appdata);
strcat(user_config_dir, CONFIGDIR); }
} return user_config_dir;
return user_config_dir;
#elif defined __APPLE__ #elif defined __APPLE__
struct passwd *pass = getpwuid(getuid()); struct passwd *pass = getpwuid(getuid());
if (!pass) return NULL; if (!pass) return NULL;
char *home = pass->pw_dir; char *home = pass->pw_dir;
user_config_dir = malloc(strlen(home) + strlen("/Library/Application Support") + strlen(CONFIGDIR) + 1); user_config_dir = malloc(strlen(home) + strlen("/Library/Application Support") + 1);
if(user_config_dir) { if(user_config_dir) {
strcpy(user_config_dir, home); strcpy(user_config_dir, home);
strcat(user_config_dir, "/Library/Application Support"); strcat(user_config_dir, "/Library/Application Support");
strcat(user_config_dir, CONFIGDIR); }
} return user_config_dir;
return user_config_dir;
#else #else
if (getenv("XDG_CONFIG_HOME")) { if (getenv("XDG_CONFIG_HOME")) {
user_config_dir = malloc(strlen(getenv("XDG_CONFIG_HOME")) + strlen(CONFIGDIR) + 1); user_config_dir = malloc(strlen(getenv("XDG_CONFIG_HOME")) + 1);
if (user_config_dir) { if (user_config_dir) {
strcpy(user_config_dir, getenv("XDG_CONFIG_HOME")); strcpy(user_config_dir, getenv("XDG_CONFIG_HOME"));
strcat(user_config_dir, CONFIGDIR); }
} } else {
} else { user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + 1);
user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + strlen(CONFIGDIR) + 1); if (user_config_dir) {
if (user_config_dir) { strcpy(user_config_dir, getenv("HOME"));
strcpy(user_config_dir, getenv("HOME")); strcat(user_config_dir, "/.config");
strcat(user_config_dir, "/.config"); }
strcat(user_config_dir, CONFIGDIR); }
} return user_config_dir;
}
return user_config_dir;
#endif #endif
}
/*
* Creates the config directory.
*/
int create_user_config_dir(char *path)
{
int mkdir_err;
#ifdef WIN32
char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
strcpy(fullpath, path);
strcat(fullpath, CONFIGDIR);
mkdir_err = _mkdir(fullpath);
if (mkdir_err) {
if(errno != EEXIST) return -1;
struct _stat buf;
if(_wstat64(fullpath, &buf)) return -1;
if(!S_ISDIR(buf.st_mode)) return -1;
}
#else
mkdir_err = mkdir(path, 0700);
struct stat buf;
if(mkdir_err) {
if(errno != EEXIST) return -1;
if(stat(path, &buf)) return -1;
if(!S_ISDIR(buf.st_mode)) return -1;
}
char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
strcpy(fullpath, path);
strcat(fullpath, CONFIGDIR);
mkdir_err = mkdir(fullpath, 0700);
if(mkdir_err) {
if(errno != EEXIST) return -1;
if(stat(fullpath, &buf)) return -1;
if(!S_ISDIR(buf.st_mode)) return -1;
}
#endif
return 0;
} }

View File

@ -23,5 +23,11 @@
#else #else
#define CONFIGDIR "/toxic/" #define CONFIGDIR "/toxic/"
#endif #endif
#ifndef S_ISDIR
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif
char *get_user_config_dir(void); char *get_user_config_dir(void);
int create_user_config_dir(char *path);

38
main.c
View File

@ -3,11 +3,19 @@
*/ */
#include <curses.h> #include <curses.h>
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#ifdef _win32
#include <direct.h>
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif
#include "../../core/Messenger.h" #include "../../core/Messenger.h"
#include "../../core/network.h" #include "../../core/network.h"
@ -332,26 +340,19 @@ int main(int argc, char *argv[])
{ {
int ch; int ch;
int f_flag = 0; int f_flag = 0;
char *configdir = get_user_config_dir(); char *user_config_dir = get_user_config_dir();
char *default_file = "data";
int mkdir_err
#ifdef _win32
mkdir_err = _mkdir(configdir);
#else
mkdir_err = mkdir(configdir, 0700);
#endif
char *filename; char *filename;
if(mkdir_err == -1) { int config_err = create_user_config_dir(user_config_dir);
filename = default_file; if(config_err) {
filename = "data";
} else { } else {
filename = malloc(strlen(configdir) + strlen(default_file) + 1); filename = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
strcpy(filename, configdir); strcpy(filename, user_config_dir);
strcat(filename, default_file); strcat(filename, CONFIGDIR);
strcat(filename, "data");
} }
ToxWindow* a; ToxWindow* a;
int i = 0; int i = 0;
for (i = 0; i < argc; ++i) { for (i = 0; i < argc; ++i) {
if (argv[i] == NULL) if (argv[i] == NULL)
@ -378,6 +379,13 @@ int main(int argc, char *argv[])
"defaulting to 'data' for a keyfile...\n"); "defaulting to 'data' for a keyfile...\n");
attroff(COLOR_PAIR(3) | A_BOLD); attroff(COLOR_PAIR(3) | A_BOLD);
} }
if(config_err) {
attron(COLOR_PAIR(3) | A_BOLD);
wprintw(prompt->window, "Unable to determine configuration directory!\n"
"defaulting to 'data' for a keyfile...\n");
attroff(COLOR_PAIR(3) | A_BOLD);
}
while(true) { while(true) {
/* Update tox */ /* Update tox */