1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-01 16:47:46 +02:00

Attempt to get and create a proper directory for config storage.

This commit is contained in:
Simon Levermann 2013-08-08 10:15:07 +02:00
parent 90acd2dfef
commit c7463f0de2
3 changed files with 137 additions and 1 deletions

91
configdir.c Normal file
View File

@ -0,0 +1,91 @@
/*
* 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>
#ifdef _win32
#include <shlobj.h>
#endif
#ifdef __APPLE__
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#endif
char *get_user_config_dir(void)
{
char *user_config_dir;
#ifdef _win32
char appdata[MAX_PATH];
HRESULT result = SHGetFolderPath(
NULL,
CSIDL_APPDATA, // TODO: Maybe use CSIDL_LOCAL_APPDATA instead? Not sure.
NULL,
SHGFP_TYPE_CURRENT,
appdata
)
if (!result) return NULL;
user_config_dir = malloc(strlen(appdata) + strlen(CONFIGDIR) + 1);
if (user_config_dir) {
strcpy(user_config_dir, appdata);
strcat(user_config_dir, CONFIGDIR);
}
return user_config_dir;
#elif defined __APPLE__
struct passwd *pass = getpwuid(getuid());
if (!pass) return NULL;
char *home = pass->pw_dir;
user_config_dir = malloc(strlen(home) + strlen("/Library/Application Support") + strlen(CONFIGDIR) + 1);
if(user_config_dir) {
strcpy(user_config_dir, home);
strcat(user_config_dir, "/Library/Application Support");
strcat(user_config_dir, CONFIGDIR);
}
return user_config_dir;
#else
if (getenv("XDG_CONFIG_HOME")) {
user_config_dir = malloc(strlen(getenv("XDG_CONFIG_HOME")) + strlen(CONFIGDIR) + 1);
if (user_config_dir) {
strcpy(user_config_dir, getenv("XDG_CONFIG_HOME"));
strcat(user_config_dir, CONFIGDIR);
}
} else {
user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + strlen(CONFIGDIR) + 1);
if (user_config_dir) {
strcpy(user_config_dir, getenv("HOME"));
strcat(user_config_dir, "/.config");
strcat(user_config_dir, CONFIGDIR);
}
}
return user_config_dir;
#endif
}

27
configdir.h Normal file
View File

@ -0,0 +1,27 @@
/*
* 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/>.
*
*/
#ifdef _win32
#define CONFIGDIR "\\toxic\\"
#else
#define CONFIGDIR "/toxic/"
#endif
char *get_user_config_dir(void);

20
main.c
View File

@ -11,6 +11,7 @@
#include "../../core/Messenger.h"
#include "../../core/network.h"
#include "configdir.h"
#include "windows.h"
extern ToxWindow new_prompt();
@ -331,7 +332,24 @@ int main(int argc, char *argv[])
{
int ch;
int f_flag = 0;
char *filename = "data";
char *configdir = 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;
if(mkdir_err == -1) {
filename = default_file;
} else {
filename = malloc(strlen(configdir) + strlen(default_file) + 1);
strcpy(filename, configdir);
strcat(filename, default_file);
}
ToxWindow* a;
int i = 0;