diff options
author | Simon Levermann <simon@levermaenner.de> | 2013-08-08 16:00:12 +0200 |
---|---|---|
committer | Simon Levermann <simon@levermaenner.de> | 2013-08-08 16:00:12 +0200 |
commit | 97e178db3aa9485ab49c646071305164cb5a7681 (patch) | |
tree | be7b8aeca43937550db9264762f8941632a82085 | |
parent | d7e2cca854cbc3d0f2418fd95f7033d6b352b587 (diff) |
Implement proper config directories.
-rw-r--r-- | testing/toxic/CMakeLists.txt | 3 | ||||
-rw-r--r-- | testing/toxic/configdir.c | 167 | ||||
-rw-r--r-- | testing/toxic/configdir.h | 8 | ||||
-rw-r--r-- | testing/toxic/main.c | 38 |
4 files changed, 143 insertions, 73 deletions
diff --git a/testing/toxic/CMakeLists.txt b/testing/toxic/CMakeLists.txt index f30d8e9e..bfcf04bf 100644 --- a/testing/toxic/CMakeLists.txt +++ b/testing/toxic/CMakeLists.txt | |||
@@ -7,7 +7,8 @@ add_executable(${exe_name} | |||
7 | main.c | 7 | main.c |
8 | prompt.c | 8 | prompt.c |
9 | friendlist.c | 9 | friendlist.c |
10 | chat.c) | 10 | chat.c |
11 | configdir.c) | ||
11 | 12 | ||
12 | target_link_libraries(${exe_name} | 13 | target_link_libraries(${exe_name} |
13 | curses) | 14 | curses) |
diff --git a/testing/toxic/configdir.c b/testing/toxic/configdir.c index c9f7de85..7d11d020 100644 --- a/testing/toxic/configdir.c +++ b/testing/toxic/configdir.c | |||
@@ -21,71 +21,126 @@ | |||
21 | #include <string.h> | 21 | #include <string.h> |
22 | #include <stdlib.h> | 22 | #include <stdlib.h> |
23 | #include <stdio.h> | 23 | #include <stdio.h> |
24 | #include <sys/types.h> | ||
25 | #include <sys/stat.h> | ||
26 | #include <errno.h> | ||
24 | 27 | ||
25 | #ifdef _win32 | 28 | #ifdef WIN32 |
26 | #include <shlobj.h> | 29 | #include <shlobj.h> |
30 | #include <direct.h> | ||
27 | #endif | 31 | #endif |
28 | 32 | ||
29 | #ifdef __APPLE__ | 33 | #ifdef __APPLE__ |
30 | #include <sys/types.h> | ||
31 | #include <unistd.h> | 34 | #include <unistd.h> |
32 | #include <pwd.h> | 35 | #include <pwd.h> |
33 | #endif | 36 | #endif |
34 | 37 | ||
38 | #include "configdir.h" | ||
39 | |||
40 | /* | ||
41 | * Retrieves a correct configuration directory, depending on the OS used, with a trailing slash | ||
42 | */ | ||
35 | char *get_user_config_dir(void) | 43 | char *get_user_config_dir(void) |
36 | { | 44 | { |
37 | char *user_config_dir; | 45 | char *user_config_dir; |
38 | 46 | ||
39 | #ifdef _win32 | 47 | #ifdef WIN32 |
40 | 48 | ||
41 | char appdata[MAX_PATH]; | 49 | char appdata[MAX_PATH]; |
42 | HRESULT result = SHGetFolderPath( | 50 | HRESULT result = SHGetFolderPath( |
43 | NULL, | 51 | NULL, |
44 | CSIDL_APPDATA, // TODO: Maybe use CSIDL_LOCAL_APPDATA instead? Not sure. | 52 | CSIDL_APPDATA, |
45 | NULL, | 53 | NULL, |
46 | SHGFP_TYPE_CURRENT, | 54 | SHGFP_TYPE_CURRENT, |
47 | appdata | 55 | appdata |
48 | ) | 56 | ) |
49 | if (!result) return NULL; | 57 | if (!result) return NULL; |
50 | 58 | ||
51 | user_config_dir = malloc(strlen(appdata) + strlen(CONFIGDIR) + 1); | 59 | user_config_dir = malloc(strlen(appdata) + 1); |
52 | if (user_config_dir) { | 60 | if (user_config_dir) { |
53 | strcpy(user_config_dir, appdata); | 61 | strcpy(user_config_dir, appdata); |
54 | strcat(user_config_dir, CONFIGDIR); | 62 | } |
55 | } | 63 | return user_config_dir; |
56 | return user_config_dir; | 64 | |
57 | 65 | #elif defined __APPLE__ | |
58 | #elif defined __APPLE__ | 66 | |
59 | 67 | struct passwd *pass = getpwuid(getuid()); | |
60 | struct passwd *pass = getpwuid(getuid()); | 68 | if (!pass) return NULL; |
61 | if (!pass) return NULL; | 69 | char *home = pass->pw_dir; |
62 | char *home = pass->pw_dir; | 70 | user_config_dir = malloc(strlen(home) + strlen("/Library/Application Support") + 1); |
63 | user_config_dir = malloc(strlen(home) + strlen("/Library/Application Support") + strlen(CONFIGDIR) + 1); | 71 | |
64 | 72 | if(user_config_dir) { | |
65 | if(user_config_dir) { | 73 | strcpy(user_config_dir, home); |
66 | strcpy(user_config_dir, home); | 74 | strcat(user_config_dir, "/Library/Application Support"); |
67 | strcat(user_config_dir, "/Library/Application Support"); | 75 | } |
68 | strcat(user_config_dir, CONFIGDIR); | 76 | return user_config_dir; |
69 | } | 77 | |
70 | return user_config_dir; | 78 | #else |
71 | 79 | ||
72 | #else | 80 | if (getenv("XDG_CONFIG_HOME")) { |
73 | 81 | user_config_dir = malloc(strlen(getenv("XDG_CONFIG_HOME")) + 1); | |
74 | if (getenv("XDG_CONFIG_HOME")) { | 82 | if (user_config_dir) { |
75 | user_config_dir = malloc(strlen(getenv("XDG_CONFIG_HOME")) + strlen(CONFIGDIR) + 1); | 83 | strcpy(user_config_dir, getenv("XDG_CONFIG_HOME")); |
76 | if (user_config_dir) { | 84 | } |
77 | strcpy(user_config_dir, getenv("XDG_CONFIG_HOME")); | 85 | } else { |
78 | strcat(user_config_dir, CONFIGDIR); | 86 | user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + 1); |
79 | } | 87 | if (user_config_dir) { |
80 | } else { | 88 | strcpy(user_config_dir, getenv("HOME")); |
81 | user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + strlen(CONFIGDIR) + 1); | 89 | strcat(user_config_dir, "/.config"); |
82 | if (user_config_dir) { | 90 | } |
83 | strcpy(user_config_dir, getenv("HOME")); | 91 | } |
84 | strcat(user_config_dir, "/.config"); | 92 | return user_config_dir; |
85 | strcat(user_config_dir, CONFIGDIR); | 93 | |
86 | } | 94 | #endif |
87 | } | 95 | } |
88 | return user_config_dir; | 96 | |
89 | 97 | /* | |
90 | #endif | 98 | * Creates the config directory. |
99 | */ | ||
100 | int create_user_config_dir(char *path) | ||
101 | { | ||
102 | |||
103 | int mkdir_err; | ||
104 | |||
105 | #ifdef WIN32 | ||
106 | |||
107 | char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); | ||
108 | strcpy(fullpath, path); | ||
109 | strcat(fullpath, CONFIGDIR); | ||
110 | |||
111 | mkdir_err = _mkdir(fullpath); | ||
112 | |||
113 | if (mkdir_err) { | ||
114 | if(errno != EEXIST) return -1; | ||
115 | struct _stat buf; | ||
116 | if(_wstat64(fullpath, &buf)) return -1; | ||
117 | if(!S_ISDIR(buf.st_mode)) return -1; | ||
118 | } | ||
119 | |||
120 | #else | ||
121 | |||
122 | mkdir_err = mkdir(path, 0700); | ||
123 | struct stat buf; | ||
124 | |||
125 | if(mkdir_err) { | ||
126 | if(errno != EEXIST) return -1; | ||
127 | if(stat(path, &buf)) return -1; | ||
128 | if(!S_ISDIR(buf.st_mode)) return -1; | ||
129 | } | ||
130 | |||
131 | char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); | ||
132 | strcpy(fullpath, path); | ||
133 | strcat(fullpath, CONFIGDIR); | ||
134 | |||
135 | mkdir_err = mkdir(fullpath, 0700); | ||
136 | |||
137 | if(mkdir_err) { | ||
138 | if(errno != EEXIST) return -1; | ||
139 | if(stat(fullpath, &buf)) return -1; | ||
140 | if(!S_ISDIR(buf.st_mode)) return -1; | ||
141 | } | ||
142 | |||
143 | #endif | ||
144 | |||
145 | return 0; | ||
91 | } \ No newline at end of file | 146 | } \ No newline at end of file |
diff --git a/testing/toxic/configdir.h b/testing/toxic/configdir.h index 441ffdab..d9837d77 100644 --- a/testing/toxic/configdir.h +++ b/testing/toxic/configdir.h | |||
@@ -23,5 +23,11 @@ | |||
23 | #else | 23 | #else |
24 | #define CONFIGDIR "/toxic/" | 24 | #define CONFIGDIR "/toxic/" |
25 | #endif | 25 | #endif |
26 | |||
27 | #ifndef S_ISDIR | ||
28 | #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) | ||
29 | #endif | ||
30 | |||
31 | char *get_user_config_dir(void); | ||
26 | 32 | ||
27 | char *get_user_config_dir(void); \ No newline at end of file | 33 | int create_user_config_dir(char *path); \ No newline at end of file |
diff --git a/testing/toxic/main.c b/testing/toxic/main.c index ec439c84..cb0d9e19 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c | |||
@@ -3,11 +3,19 @@ | |||
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 | ||
@@ -332,26 +340,19 @@ int main(int argc, char *argv[]) | |||
332 | { | 340 | { |
333 | int ch; | 341 | int ch; |
334 | int f_flag = 0; | 342 | int f_flag = 0; |
335 | char *configdir = get_user_config_dir(); | 343 | char *user_config_dir = get_user_config_dir(); |
336 | char *default_file = "data"; | ||
337 | int mkdir_err | ||
338 | #ifdef _win32 | ||
339 | mkdir_err = _mkdir(configdir); | ||
340 | #else | ||
341 | mkdir_err = mkdir(configdir, 0700); | ||
342 | #endif | ||
343 | |||
344 | char *filename; | 344 | char *filename; |
345 | if(mkdir_err == -1) { | 345 | int config_err = create_user_config_dir(user_config_dir); |
346 | filename = default_file; | 346 | if(config_err) { |
347 | filename = "data"; | ||
347 | } else { | 348 | } else { |
348 | filename = malloc(strlen(configdir) + strlen(default_file) + 1); | 349 | filename = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); |
349 | strcpy(filename, configdir); | 350 | strcpy(filename, user_config_dir); |
350 | strcat(filename, default_file); | 351 | strcat(filename, CONFIGDIR); |
352 | strcat(filename, "data"); | ||
351 | } | 353 | } |
352 | 354 | ||
353 | ToxWindow* a; | 355 | ToxWindow* a; |
354 | |||
355 | int i = 0; | 356 | int i = 0; |
356 | for (i = 0; i < argc; ++i) { | 357 | for (i = 0; i < argc; ++i) { |
357 | if (argv[i] == NULL) | 358 | if (argv[i] == NULL) |
@@ -378,6 +379,13 @@ int main(int argc, char *argv[]) | |||
378 | "defaulting to 'data' for a keyfile...\n"); | 379 | "defaulting to 'data' for a keyfile...\n"); |
379 | attroff(COLOR_PAIR(3) | A_BOLD); | 380 | attroff(COLOR_PAIR(3) | A_BOLD); |
380 | } | 381 | } |
382 | |||
383 | if(config_err) { | ||
384 | attron(COLOR_PAIR(3) | A_BOLD); | ||
385 | wprintw(prompt->window, "Unable to determine configuration directory!\n" | ||
386 | "defaulting to 'data' for a keyfile...\n"); | ||
387 | attroff(COLOR_PAIR(3) | A_BOLD); | ||
388 | } | ||
381 | 389 | ||
382 | while(true) { | 390 | while(true) { |
383 | /* Update tox */ | 391 | /* Update tox */ |