summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-08-08 11:37:58 -0700
committerirungentoo <irungentoo@gmail.com>2013-08-08 11:37:58 -0700
commit249f552e6798c16227196b40ec931ec36fd362c5 (patch)
tree574093ef7c6a9c824cbae9d617a1afab81f688c4
parent1fb2085c6716dcedc0a3547bf78584bdf20a7b40 (diff)
parent320c5a6708fba36c48c62ec0dca8ad2d5b865b5d (diff)
Merge pull request #397 from leeroy2098/merge
Implement proper configuration directories for toxic
-rw-r--r--testing/toxic/CMakeLists.txt3
-rw-r--r--testing/toxic/configdir.c136
-rw-r--r--testing/toxic/configdir.h33
-rw-r--r--testing/toxic/main.c31
4 files changed, 200 insertions, 3 deletions
diff --git a/testing/toxic/CMakeLists.txt b/testing/toxic/CMakeLists.txt
index 38f02dc6..13b8692d 100644
--- a/testing/toxic/CMakeLists.txt
+++ b/testing/toxic/CMakeLists.txt
@@ -10,7 +10,8 @@ add_executable(${exe_name}
10 main.c 10 main.c
11 prompt.c 11 prompt.c
12 friendlist.c 12 friendlist.c
13 chat.c) 13 chat.c
14 configdir.c)
14 15
15target_link_libraries(${exe_name} 16target_link_libraries(${exe_name}
16 curses) 17 curses)
diff --git a/testing/toxic/configdir.c b/testing/toxic/configdir.c
new file mode 100644
index 00000000..d91d0804
--- /dev/null
+++ b/testing/toxic/configdir.c
@@ -0,0 +1,136 @@
1/*
2 * Copyright (C) 2013 Tox project All Rights Reserved.
3 *
4 * This file is part of Tox.
5 *
6 * Tox is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Tox is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <string.h>
22#include <stdlib.h>
23#include <stdio.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <errno.h>
27
28#ifdef WIN32
29#include <shlobj.h>
30#include <direct.h>
31#endif
32
33#ifdef __APPLE__
34#include <unistd.h>
35#include <pwd.h>
36#endif
37
38#include "configdir.h"
39
40/*
41 * Retrieves a correct configuration directory, depending on the OS used, with a trailing slash
42 */
43char *get_user_config_dir(void)
44{
45 char *user_config_dir;
46
47 #ifdef WIN32
48
49 char appdata[MAX_PATH];
50 HRESULT result = SHGetFolderPath(
51 NULL,
52 CSIDL_APPDATA,
53 NULL,
54 SHGFP_TYPE_CURRENT,
55 appdata
56 )
57 if (!result) return NULL;
58
59 user_config_dir = strdup(appdata);
60
61 return user_config_dir;
62
63 #elif defined __APPLE__
64
65 struct passwd *pass = getpwuid(getuid());
66 if (!pass) return NULL;
67 char *home = pass->pw_dir;
68 user_config_dir = malloc(strlen(home) + strlen("/Library/Application Support") + 1);
69
70 if(user_config_dir) {
71 strcpy(user_config_dir, home);
72 strcat(user_config_dir, "/Library/Application Support");
73 }
74 return user_config_dir;
75
76 #else
77
78 if (getenv("XDG_CONFIG_HOME")) {
79 user_config_dir = strdup(getenv("XDG_CONFIG_HOME"));
80 } else {
81 user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + 1);
82 if (user_config_dir) {
83 strcpy(user_config_dir, getenv("HOME"));
84 strcat(user_config_dir, "/.config");
85 }
86 }
87 return user_config_dir;
88
89 #endif
90}
91
92/*
93 * Creates the config directory.
94 */
95int create_user_config_dir(char *path)
96{
97
98 int mkdir_err;
99
100 #ifdef WIN32
101
102 char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
103 strcpy(fullpath, path);
104 strcat(fullpath, CONFIGDIR);
105
106 mkdir_err = _mkdir(fullpath);
107 struct __stat64 buf;
108 if (mkdir_err && (errno != EEXIST || _wstat64(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
109 free(fullpath);
110 return -1;
111 }
112
113 #else
114
115 mkdir_err = mkdir(path, 0700);
116 struct stat buf;
117
118 if(mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) {
119 return -1;
120 }
121
122 char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
123 strcpy(fullpath, path);
124 strcat(fullpath, CONFIGDIR);
125
126 mkdir_err = mkdir(fullpath, 0700);
127
128 if(mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
129 free(fullpath);
130 return -1;
131 }
132
133 #endif
134
135 return 0;
136}
diff --git a/testing/toxic/configdir.h b/testing/toxic/configdir.h
new file mode 100644
index 00000000..fad949cf
--- /dev/null
+++ b/testing/toxic/configdir.h
@@ -0,0 +1,33 @@
1/*
2 * Copyright (C) 2013 Tox project All Rights Reserved.
3 *
4 * This file is part of Tox.
5 *
6 * Tox is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Tox is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#ifdef _win32
22#define CONFIGDIR "\\toxic\\"
23#else
24#define CONFIGDIR "/toxic/"
25#endif
26
27#ifndef S_ISDIR
28#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
29#endif
30
31char *get_user_config_dir(void);
32
33int create_user_config_dir(char *path);
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index 7fa9e964..162cce68 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -3,14 +3,23 @@
3 */ 3 */
4 4
5#include <curses.h> 5#include <curses.h>
6#include <errno.h>
6#include <stdio.h> 7#include <stdio.h>
7#include <stdlib.h> 8#include <stdlib.h>
8#include <stdbool.h> 9#include <stdbool.h>
9#include <stdint.h> 10#include <stdint.h>
10 11
12#ifdef _win32
13#include <direct.h>
14#else
15#include <sys/stat.h>
16#include <sys/types.h>
17#endif
18
11#include "../../core/Messenger.h" 19#include "../../core/Messenger.h"
12#include "../../core/network.h" 20#include "../../core/network.h"
13 21
22#include "configdir.h"
14#include "windows.h" 23#include "windows.h"
15 24
16extern ToxWindow new_prompt(); 25extern ToxWindow new_prompt();
@@ -334,9 +343,19 @@ int main(int argc, char *argv[])
334{ 343{
335 int ch; 344 int ch;
336 int f_flag = 0; 345 int f_flag = 0;
337 char *filename = "data"; 346 char *user_config_dir = get_user_config_dir();
347 char *filename;
348 int config_err = create_user_config_dir(user_config_dir);
349 if(config_err) {
350 filename = "data";
351 } else {
352 filename = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
353 strcpy(filename, user_config_dir);
354 strcat(filename, CONFIGDIR);
355 strcat(filename, "data");
356 }
357
338 ToxWindow* a; 358 ToxWindow* a;
339
340 int i = 0; 359 int i = 0;
341 for (i = 0; i < argc; ++i) { 360 for (i = 0; i < argc; ++i) {
342 if (argv[i] == NULL) 361 if (argv[i] == NULL)
@@ -354,6 +373,7 @@ int main(int argc, char *argv[])
354 init_term(); 373 init_term();
355 init_tox(); 374 init_tox();
356 load_data(filename); 375 load_data(filename);
376 free(filename);
357 init_windows(); 377 init_windows();
358 init_window_status(); 378 init_window_status();
359 379
@@ -363,6 +383,13 @@ int main(int argc, char *argv[])
363 "defaulting to 'data' for a keyfile...\n"); 383 "defaulting to 'data' for a keyfile...\n");
364 attroff(COLOR_PAIR(3) | A_BOLD); 384 attroff(COLOR_PAIR(3) | A_BOLD);
365 } 385 }
386
387 if(config_err) {
388 attron(COLOR_PAIR(3) | A_BOLD);
389 wprintw(prompt->window, "Unable to determine configuration directory!\n"
390 "defaulting to 'data' for a keyfile...\n");
391 attroff(COLOR_PAIR(3) | A_BOLD);
392 }
366 393
367 while(true) { 394 while(true) {
368 /* Update tox */ 395 /* Update tox */